问题
Some header files are present in /src/dir1/
(eg: a.h
, b.h
, c.h
etc). My source file is present in /src/dir2/file.cpp
. I used some header files that are present in /src/dir1/
but during compilation I got errors like header file not found
.
Then I changed the include path like #include "../src/dir1/a.h"
, then error is gone in file.cpp
but I get not found
error in the headers files that are present in /src/dir1
. Because I included the header file say a.h
, that a.h
included some other header files that are present in /src/dir1/
(say b.h
and c.h
present in a.h
).
How to add the header file (a.h
) in /src/dir2/file.cpp
so that it should not ask to modify the include path in the header files that are present in /src/dir1/
?
Note: I am using scons
to build.
回答1:
You can add directories to the include file search path using the -I
command line parameter of gcc
:
gcc -I/src/dir1 file.cpp
回答2:
SCons FAQ:
How do I get SCons to find my #include files?
If your program has #include files in various directories, SCons must somehow be told in which directories it should look for the #include files. You do this by setting the CPPPATH variable to the list of directories that contain .h files that you want to search for:
env = Environment(CPPPATH='inc')
env.Program('foo', 'foo.c')
SCons will add to the compilation command line(s) the right -I options, or whatever similar options are appropriate for the C or C++ compiler you're using. This makes your SCons-based build configuration portable.
Note specifically that you should not set the include directories directly in the CCFLAGS variable, as you might initially expect:
env = Environment(CCFLAGS='-Iinc') # THIS IS INCORRECT!
env.Program('foo', 'foo.c')
This will make the program compile correctly, but SCons will not find the dependencies in the "inc" subdirectory and the program will not be rebuilt if any of those #include files change.
回答3:
It's not found because it's not there. You have one extra level of indirection. A file in "/src/foo/" would include a file in "/src/bar/" with "include ../bar/the_file"
In other words, in your example, there is no "../src/" relative to dir1 or dir2. The relationship is "dir1/../dir2" or "dir1/../../src/dir2"
To see this for yourself, make dir1 your current directory (chdir /src/dir1) and compare the difference betwee "ls .." and "ls ../src". The second ls will not work but the first one will.
Make sense? hope that helps
来源:https://stackoverflow.com/questions/17475268/c-c-header-file-not-found