Can I use a preprocessor variable in #include directive? - c++

This is what I'm trying to do:
$ c++ -D GENERATED=build/generated-content main.cpp
My main.cpp file:
#include "GENERATED/header.h"
void f() { /* something */ }
Currently this code fails to compile. How should I fix it? And whether it's possible at all?

It seems you want to use different headers depending on some "compilation profile".
Instead of the -Dsolution, I would rather suggest using the -I directive to specify the include directories.
Given you have the following file tree:
/
debug/
header.h
release/
header.h
main.cpp:
#include "header.h"
/* some instructions, not relevant here */
And in your Makefile (or whatever tool you use), just specify the proper include directory to use, depending on whatever reason you want:
g++ -I debug main.cpp // Debug mode
g++ -I release main.cpp // Release mode
Important foot note: I don't know if you intended to use this as a debug/release switch. However, doing so would be weird: the interface (the included .h files) shouldn't change between release and debug. If you ever need this, the usual way is to enable/disable some parts of the code using defines, mostly in .c (and .cpp) files.

You can't do that directly, but this is possible:
c++ -D "GENERATED=\"build/generated-content\"" main.cpp
#define INCLUDE_NAME() GENERATED "/header.h"
#include INCLUDE_NAME()
EDIT: This solution does not work (see the comments below).
Essentially, any #include statement not conforming to the 'normal' syntax (either #include "" or #include <>) but being of the form #include SOMETHING will cause SOMETHING to be preprocessed, after which it has to conform to one of the 'normal' syntaxes. SOMETHING can be any sequence of pp-tokens.
However in this case, the result (as generated by GCC) is...
#include "build/generated-content" "/header.h"
... which does not conform to one of the 'normal' syntaxes.

I don't believe that is covered in the C++ Standard, so it would be up to the individual compiler vender whether or not to support it. I don't think any do.
However, just about every compiler allows you to specify a search directory for headers. It's usually the /i option.
So it would be:
$ c++ -i build/generated-content main.cpp
My main.cpp file:
#include "header.h"
void f() { /* something */ }

Related

How do I include a class from another project in my current project?

I have a class called Timer that contains, obviously, code to track running time. I want to include this class in another project but I can't figure out how.
I have tried using
#include "Timer.h"
as well as using the file path to the project with the timer class, i.e.
#include "/users/user/projects/TimerProject/timer.h"
But that hasn't worked either, it tells me the file can’t be found. Is there something I am missing here?
Yes. You need to tell your C++ compiler where to search for include files. For gcc or clang, this is the -I command-line switch. So, for example:
g++ -o foo foo.cpp -I/users/user/projects/TimerProject/
and this will allow you to use:
#include <Timer.h>
Using double-quotes around the include name tells the compiler: "Search the same directory as the including file first, then search the include folders the compiler knows about". So if you have a foo.h next to your foo.cpp, you could use:
#include "foo.h"
without adding anything to the include path for it.
Finally: Files are case-sensitive on many operating systems. In your example, you have Timer.h and timer.h - make sure you use the correct spelling!
See also:
What is the difference between #include <filename> and #include "filename"?

Multiple includes with #include in C++

I have a question regarding the #include preprocessor directive in C++.
In my program I would like to include a header file from another directory. For this I have used the full path, example:
#include "full/path/to/my/header.hpp"
Now it turns out that header.hpp itself has an include (let's say #include "otherheader.hpp". During compilation, the compiler complains that it cannot find this other header.
What is the best way to handle this problem, considering the fact that I also don't want to write the full path for every header file, especially including those that are only needed "further down the tree"?
You should use your compiler's -I option.
Example with g++:
g++ -I full/path/to/my/
Then in your code you can simply put:
#include "header.hpp"
More information on search path.
Most compilers allow you to specify the root for additional include directories when compiling. For example, in Visual C++ you specify the -I flag. It's also the same for gcc. For example:
compiler -Ipath/to/headers myfile.c
Don't specify full path to includes. Specify include directories to the compiler instead and then just #include <foo> knowing that foo will be found in /some/path/foo because you told the compiler to search /some/path/ for includes. For many compilers this is done with the -I option.

Use stdlib functions without including stdlib.h

I am compiling a large number of files which use srand() and rand() without including stdlib.h.
I'm aware that this is bad practice but, as I cannot change the files that I am compiling, inserting the necessary include statement in each file is not an option.
How can I configure my compiler to allow implicit inclusion of stdlib functions? Also, is there a way to implicitly use the std namespace in the same way?
Edit: Using g++
Edit: Looks like this is the answer (to the first part, at least). To compile a file as if stdlib.h is included, use the option -include stdlib.h
As you've now mentioned that you're using GCC, you can just use the -include flag. Other compilers probably have equivalents.
If you don't have such a flag for your compiler, you could use the following not-entirely-serious solution, that should work nevertheless:
nice.cc
#include <stdlib.h>
#include "naughty.cc"
(where naughty.cc is the original source file).
Of course, with a suitable build system, you could automatically generate the nice wrapper files.
Since you are already embracing bad practice, how about:
cat > foo.c << EOF
#include <stdlib.h>
#include "file-to-compile.c"
EOF
${CC} foo.c
You're using GCC, so you can use the -include option (from the manual):
Process file as if #include "file" appeared as the first line of the primary source file.
For example:
g++ -include stdlib.h foo.c

Including and linking files in C and C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add a directory to C header include path?
I have run into this problem several times, and I thought it would be good to get an explanation from an experienced C or C++ programmer. In the example below I use a sample library name, but the actual name could be anything.
Say I want to run the following MWE:
#include <stdio.h>
#include <mylib/mylib.h> /* Also try mylib2/mylib/mylib.h */
int main (int argc, char **argv) {
printf ("Hello, World!\n");
return 0;
}
Further, assume that I have a Linux distribution that, instead of placing mylib.h in /usr/include/mylib/, chooses the directory /usr/include/mylib2/mylib/. So the straightforward command:
$ gcc test.c
would fail with the error:
fatal error: mylib.h: No such file or directory
But I might try to fix the include statement by writing:
#include <mylib2/mylib.h>
This gets me past the first error. But internally, mylib.h refers to other header files in the "usual" way: #include <mylib/anotherheader.h>. So now, even after I have fixed the original include statement in the MWE as #include <mylib2/mylib/mylib.h> the build breaks because mylib.h is attempting to include #include <mylib/anotherheader.h> instead of #include <mylib2/mylib/anotherheader.h>.
What is the standard way around this problem? There must be a simple compilation flag that I am missing. Thank you in advance.
You set up the include path so that it contains /usr/include/mylib2, usually with a command line option to the compiler like -I/usr/include/mylib2.
Note that usually an header includes related files from the same package using the
#include "another.h"
syntax, so that it is first searched in a relative to where it is installed. If your library had done this, there would be no problem with
#include <mylib2/mylib/mylib.h>
but even if it was the case you don't want your code to depend on where the libraries you use are installed. That dependence should be kept in the build system.
See the -I compiler flag on gcc and g++ it allows you to specify additional include paths:
http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
In this case you could specify:
-I /usr/include/mylib2
and #include "mylib/mylib.h" everywhere
Also, this is not part of your question but I thought I would throw it in there anyways, but there is a slight difference between include statements with quotes, and angle brackets:
http://msdn.microsoft.com/en-us/library/36k2cdd4(v=vs.80).aspx
I would recommend the following pattern:
/usr/libs/lib15 - is the directory where your library resides.
In the code:
#include <stdio.h>
#include <mylib1.h> // Header from your lib.
#include <mylib2.h> // Other header from your lib.
#include <mygraphs/defines.h> // Headers in your lib have even subdirectories.
In the command line:
cl -I /usr/libs/lib15 .....
You specify the search path for your headers.

in include if i use "test.h" is the same with "path/test.h"?

I am working in ubuntu under c++ language.
I have a question: i use #include"header.h". Is this the same with /path/header.h? I ask you this question because as I've seen is not the same thing. Need some explications.
I ask you this question because I've downloaded and install gsoap on my computer. I added all the necessary dependencies in a folder and I've tried to run the app without installing gsoap ...on a different computer. I had some errors..i forgot to add stdsoap2.h file...I will add it today..in my folder..
The answer is it depends:
If you have "path/" added to your include path then including only "header.h" will work because then compiler already knows the path to lookup for your header files, if not
then you have to include entire path "path/header.h" so the compiler knows where to look for the header file.
If header.h is in the directory path/, then #include "header.h" will work for those header and source files (which #include header.h which happen to be in the same directory as header.h (path/).
On the other hand, if you are #include-ing header.h in a file that is in a different directory than path/, then the above way would not work. To make it work, you can try 2 different approaches:
#include the complete path to header.h. Your #include will look something like:
#include "path/header.h"
Include the path/ directory to the makefile. This will get g++ to look for header.h in those directories as well. This can be done like so (in the makefile):
g++ <some parameters> -Ipath/ -c main.cpp -o main.o (assuming header.h is called from within main.cpp). If you choose this way, then the #include will also change, like so:
#include <header.h>. Note the use of the -I flag as a parameter to g++. That flag tells g++ to look into additional directories as well.
No, they are not the same, conceptually. The results, however, could be the same. It depends on how you tell your compiler to find headers (the -I flag in g++). If you would compile with -I /path/, then you'd find /path/header.h with #include "header.h". If you do not use that include path flag, then you'd have to write #include "/path/header.h".