c/c++:未找到头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17475268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c/c++ : header file not found
提问by NAVEEN
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
.
一些头文件存在于/src/dir1/
(例如:a.h
,b.h
,c.h
等等)。我的源文件存在于/src/dir2/file.cpp
. 我使用了一些存在的头文件,/src/dir1/
但在编译过程中出现了类似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
).
然后我更改了包含路径,如#include "../src/dir1/a.h"
,然后错误消失了,file.cpp
但我not found
在/src/dir1
. 因为我包含了头文件 say a.h
,它a.h
包含了一些其他的头文件,这些头文件存在于/src/dir1/
(比如b.h
并且c.h
存在于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/
?
如何添加头文件 ( a.h
)/src/dir2/file.cpp
以便它不应该要求修改存在于 中的头文件中的包含路径/src/dir1/
?
Note: I am using scons
to build.
注意:我正在使用scons
构建。
回答by Joni
You can add directories to the include file search path using the -I
command line parameter of gcc
:
您可以使用以下-I
命令行参数将目录添加到包含文件搜索路径gcc
:
gcc -I/src/dir1 file.cpp
回答by Ben Brammer
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"
没有找到,因为它不存在。你有一个额外的间接级别。“/src/foo/”中的文件将包含“/src/bar/”中的文件,其中“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"
换句话说,在您的示例中,相对于 dir1 或 dir2 没有“../src/”。关系是“dir1/../dir2”或“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.
要亲自查看,请将 dir1 设为您的当前目录 (chdir /src/dir1) 并比较“ls ..”和“ls ../src”之间的差异。第二个 ls 将不起作用,但第一个将起作用。
Make sense? hope that helps
有道理?希望有帮助
回答by LogicG8
How do I get SCons to find my #include files?
如何让 SCons 找到我的 #include 文件?
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:
如果您的程序在各个目录中都有#include 文件,则必须以某种方式告诉 SCons 它应该在哪些目录中查找 #include 文件。为此,您可以将 CPPPATH 变量设置为包含要搜索的 .h 文件的目录列表:
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.
SCons 将向编译命令行添加正确的 -I 选项,或任何适用于您正在使用的 C 或 C++ 编译器的类似选项。这使得基于 SCons 的构建配置可移植。
Note specifically that you should not set the include directories directly in the CCFLAGS variable, as you might initially expect:
请特别注意,您不应直接在 CCFLAGS 变量中设置包含目录,正如您最初预期的那样:
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.
这将使程序正确编译,但 SCons 不会在“inc”子目录中找到依赖项,并且如果这些 #include 文件中的任何一个发生更改,则不会重新构建程序。