I am working with C++ in org babel and would like to know how to format the includes so that I can list more than one.
Here's what I'm trying (that fails)
#+BEGIN_SRC C++ :includes <cstdio> :includes <iostream> :includes <string>
using namespace std;
printf("Hello ");
cout << "world\n";
#+END_SRC
In this case both printf and cout are undeclared in this scope. I can drop the unnecessary :includes <string> from the headers and the cout doesn't kick up an error, so it seems like only the last :includes counts. I have tried loading multiple includes into the same line using nothing, commas and spaces as delimiters and I always get an error about extra tokens at the end of the include directive. I have also tried using :includes+ in case that worked on the header line, but it didn't.
I'm fairly certain that what I'm trying to do should be possible because it says in the documentation that
:includes
(C & CC+ only) accepts either a single string name, or a list of names of files to #include in the execution of the code block
Org-version: 8.2.7-4-g880362-elpa
EDIT:
It's worth noting that other header arguments can just be strung together (i.e. :results raw drawer will produce unformatted results in a drawer), so there's a decent chance that this is a bug. Using :include <cstdio> <iostream> produces a compiler error that there's an extra token at the end of the #include <cstdio> line.
EDIT 2:
Turns out it's actually a bug in org, so it's been submitted.
After some experimenting, during which I noticed that some error messages look fairly lispy, and using the superficial knowledge I have of Lisp, I found the answer:
#+begin_src C++ :includes '("<math.h>" "<iostream>" "<algorithm>")
// Freely use symbols from those headers
#+end_src
Another issue that I'm having is that I can only get it to execute if I capitalize C++ but I only get syntax highlighting if I use minuscules ("c++"). Pure joy.
Related
I have a messaging function that takes a variable amount of arguments because it supports string formatting in the same way printf does. My plan was to then define message code and messages with a single #define and be able to call this function by passing it a single argument for something that is tightly related.
Here is the message macro:
#define MSG(a, b, ...) message(__FILE__, __LINE__, a, b, __VA_ARGS__);
And here is an example of the message code and string define:
#define MSG_INIT 0000,"%s INITIALIZED SUCCESSFULLY"
The issue is occurring when I try to make a call in the form of:
MSG(MSG_INIT);
My IDE is giving me an error along the lines of:
'Expands to message("file.c", 1, 0000, "INITIALIZED SUCCESSFULLY",);
where the last ',' is causing the issue.
I was expecting to see:
'Expands to message("file.c", 1, 0000, "INITIALIZED SUCCESSFULLY");
If I change the call to:
MSG(MSG_INIT,NULL);
All is good but I'd rather not pass the NULL because the point of the MSG_INIT define in the first place was to pass less parameters. There are messages that make use of the __VA_ARGS__ (Example: #define MSG_CONNECT 0001,"CONNECTION TO [%s] ESTABLISHED" with matching call MSG(MSG_CONEST, server_ip);) so getting rid of that is not an option. I am just looking for the best way to fix this problem and I'm aware a lot of people suggest staying away from the preprocessor but this was the best solution I thought of.
I'm aware a lot of people suggest staying away from the preprocessor ...
I wonder why that is. Could it possibly be for the same reason you had to ask this question? :-)
Honestly, the pre-processor should be limited pretty much to including header files and doing conditional compilation. Anything else that it used to be used for, such as:
#define RC_OK 0
#define RC_NOMEM 1
#define HALF_OF(x) x / 2
is far better done with enumerations or functions.
The days of function-like macros should be put behind us, especially since modern compilers can quite easily out-optimise most hand-crafted code, and without subtle errors creeping in such as with that HALF_OF macro above (do not use it, I specifically made it buggy to illustrate a point).
It's really just a text substitution thing, one which understands the lexical elements, but not the full grammar of C. The fact that you have to perform what I call "macro gymnastics" to get it to do anything other than simple stuff is reason enough to steer clear. Use functions for this, it'll make your life a lot easier.
And apologies for not solving your specific issue, I just think any solution using the pre-processor is going to be half-baked at best. But there's plenty of precedent on SO for answering the question "How do I X?" with "Don't X, it's better to Y." Think of writing an accounting package in assembler, an operating system in object-oriented COBOL, or anything in Pascal :-)
But, if you really wanted to stick to macros against my advice, you could try starting with something like:
#include <stdio.h>
#define MSG(fmtStr, ...) printf("%s:%d> " fmtStr, __FILE__, __LINE__, __VA_ARGS__)
#define MSG_INIT "Initialised %s\n"
#define MSG_TERM "Terminated %s with values %d and %d\n"
int main(void) {
MSG("%f\n", 42.3);
MSG(MSG_INIT, "something");
MSG(MSG_TERM, "something else", 7, 42);
}
I think that's portable, and it allow you to put the heavy lifting into MSG and using a separate format string and parameters for the rest of the stuff:
testprog.c:8> 42.300000
testprog.c:9> Initialised something
testprog.c:10> Terminated something else with values 7 and 42
I'm trying to concatenate a word in the source code with the expansion of a preprocessor macro. Basically I have foo somewhere in the code, and with a #define EXPANSION bar I want to obtain foobar. However, I'm struggling to find a way to do this, which works with all compilers. For the moment I would be happy if it works with gfortran and ifort.
According to its documentation, the gfortran preprocessor is a C preprocessor running in "traditional mode", which does not have the ## token paste operator. However, the same effect can be obtained with an empty C-style /**/ comment. The ifort preprocessor seems to behave more like the normal C preprocessor, so normal token pasting does the trick in ifort. Unfortunately the empty /**/ comment does not work in ifort, as the comment is replaced by a single space instead.
Here is a little example:
#define EXPANSION bar
#define CAT(x,y) PASTE(x,y)
#define PASTE(x,y) x ## y
foo/**/EXPANSION
CAT(foo,EXPANSION)
For which gfortran produces:
foobar
foo ## bar
While ifort gives me:
foo bar
foobar
Of course I could choose the right way by checking the predefined macros for both compilers:
#ifdef __GFORTRAN__
foo/**/EXPANSION
#else
CAT(foo,EXPANSION)
#endif
This works for both of them, but it's rather ugly to have the preprocessor conditional for every expansion. I would much rather avoid this and have some macro magic only once in the beginning.
I have seen this answer to another question, which would probably allow me to work around this issue, but I would rather find a solution that does not invoke the preprocessor separately.
I'm not too familiar with the C preprocessor. Maybe there is a simple way to do what I want. Any ideas?
EDIT: I've already tried something like this:
#define EXPANSION bar
#define CAT(x,y) PASTE(x,y)
#ifdef __GFORTRAN__
#define PASTE(x,y) x/**/y
#else
#define PASTE(x,y) x ## y
#endif
CAT(foo,EXPANSION)
Unfortunately this does not work in gfortran where it produces fooEXPANSION. I'm not entirely sure how this works, but apparently the expansion of the CAT macro prevents the expansion of EXPANSION in the same line. I suspect that this is a feature of the "traditional" C preprocessor ...
I have done some research and it seems that basically most Fortran compilers (i.e. Intel and PGI) use a relatively normal C-preprocessor with a token pasting operator. We only need a special treatment for gfortran, which uses a C preprocessor in traditional mode without a token pasting operator.
Thanks to an entry on c-faq.com I found this definition of a CAT macro that works with all compilers I tested so far:
#ifdef __GFORTRAN__
#define PASTE(a) a
#define CAT(a,b) PASTE(a)b
#else
#define PASTE(a) a ## b
#define CAT(a,b) PASTE(a,b)
#endif
Another solution (that still uses the /**/ trick) was posted by Jonathan Leffler in this answer to a related question.
I'm creating a latex document that will describe various C++ syscall functions. I need to include their prototypes, descriptions, return values, and common uses. I've been told that to include code, I need to use the listings package, but I am using code within sentences and tables, so the commands for listings gets crowded and unreadable.
The first table without bold characters and titles for the columns.
Here's my code.
\begin{tabular}{l|l}
\begin{lstlisting}
void perror(const char* s)
\end{lstlisting}
&
\begin{lstlisting}
stdio.h, errno.h
\end{lstlisting}
\\
& prints argument message \begin{lstlisting}
s
\end{lstlisting}
\ based on global int
\begin{lstlisting}
errno
\end{lstlisting}
\end{tabular}
The code looks really messy and is difficult to read. I tried to fix this with a macro but it did not work. I'm programming in shareLatex right now.
\newcommand{\lstcode}[1]{
\begin{lstlisting}
#1
\end{lstlisting} }
I like the idea of simply using backticks for code in the middle of sentences. Even individually, the perror is bulky as code and I'd love to make the table look more like a table in my code too. How do I:
include code in a way that doesn't clutter up my table? (perror's declaration)
include code in the middle of a sentence? (s and errno)
Problem with creating a newenvironment in LaTeX
Macros have, thankfully, been created before for lstlisting and require that a special environment is made for them.
\lstnewenvironment
{⟨name⟩}[⟨number⟩][⟨opt. default arg.⟩]
{\lstset{label=#1}}
{⟨ending code⟩}
EDIT: After trying this, I couldn't use it. I switched to \texttt{} instead for its easiness of use. Any other choices would be cool, but for now this is good.
I configured my cedet almost the same with http://alexott.net/en/writings/emacs-devenv/EmacsCedet.html.
Thanks alexott , most of the time it works well, but I found that it can not well parse the tm struct in /usr/include/time.h.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void){
struct tm times;
FILE file;
}
When using M-x semantic-ia-fast-jump, the struct FILE is correct, but semantic finds the struct tm in wchar.h, not in time.h.
The problem seems to be there is a forward declaration in wchar.h for the struct tm.
In my copy of time.h, it appears that the symbol __BEGIN_NAMESPACE_STD is showing up in front of the struct declaration, and is befuddling the parser. You can fix that quickly by just adding that and __END_NAMESPACE_STD to the variable semantic-lex-c-preprocessor-symbol-map as mapping to empty. Then delete your semanticdb cache files (in ~/.semanticdb) related to time.h, or just everything in /usr/include, and restart emacs. time.h should get reparsed, and things should work ok for time.h after that... unless you want to use std::tm or something.
I'm abusing the C preprocessor for my build system to produce a "readme" plain-text file and a web page from the same source file. The construction is something like this:
The actual definitions are in data.h:
#define WEBSITE "http://example.com"
Note that the // in the URL must be quoted, or else it will be treated as the start of a comment. A similar problem occurs when using a , in the argument; the quotes are necessary, or else the comma would be treated as an argument separator.
Using this header, a file readme.txt.pp is run through the C preprocessor:
#include "data.h"
Visit the website at WEBSITE!
Of course, the preprocessor output is:
Visit the website at "http://example.com"!
The quotes appear in the output. Is there any way, or workaround, to get this code to give the output:
Visit the website at http://example.com!
I'm using Visual C++ 2008. I know that the preprocessor is not the ideal tool for this job; suggestions that use other built-in VC++ features are also welcome. (I tried XML with XSLT, but it is impossible to include one XML file into another, which was a show-stopper.)
Regarding XSLT, have a look at the document() function to read from multiple source documents.
I don't think there's any way to remove the quotes from the value of WEBSITE, since they are there in the definition of the macro. You might consider using the m4 macro processor instead of the C preprocessor.
Probably being late for Thomas, this might, however, still be useful for anyone lately stumbling over this question like me...
Try this:
#define DUMMY
#define WEBSITE http:/DUMMY/example.com
So the line comment disappears, and the preprocessor resolves DUMMY to nothing.
Try disabling the C++ style comments if possible. I don't know how that works in VS, but using a GCC compiler I can pass the -std=c89 flag to gcc to disable C++ style comments and hence making
#define WEBSITE http://example.com
possible.