2015-12-21 64 views
1

我想学习不同的编译技巧。请考虑下面的代码片段:在编译中设置头文件路径?

#include <header.h> 
    main() 
    { 
    execute me; 

    } 

现在我编译使用此代码: -

gcc hello.c -I /home/example 

该文件头被搜索到的/usr/include目录的/ etc编译期间我所看到的路径,但我已经把header.h /home/example/header.h路径 所以这是不是能找到头文件。

但如果现在我有头文件中按以下方式则是能找到头文件。

#include "header.h" 

所以我想知道是否有任何方式,我将包括使用<>选项,我也能我使用命令行(使用-I或任何选项),得头道头文件?

如有什么不明确的评论。

+1

如果你已经使用'-I',GCC将搜索它,不管你如何包括它给出的路径。你是怎么决定的:“但是我放置了header.h /home/example/header.h路径,所以这找不到头文件”。您是否收到错误,表示无法找到? –

+0

而且也看到这个帖子:http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename –

+0

是否行为有所不同,如果你把'-I '在源文件之前? ('gcc -I/home/example hello.c')?在Mac OS X 10.11.2上使用GCC 5.3.0似乎并不重要。但是,指定'-I'指令的顺序无关紧要。添加'-H'选项会告诉你什么? –

回答

1

包含带有这些<>符号的头文件实际上告诉编译器在通用目录中搜索它,并且包含这些“”符号告诉编译器在本地项目目录中进行搜索。

0

你确定它没有找到头?因为这个证明确实是的工作。你有没有在-I之后拼错目录?

$ echo '#error here!' > header.h 
$ cat test.c 
#include <header.h> 
int main(void) 
{ 
    return 0; 
} 
$ gcc -I $PWD test.c 
In file included from test.c:1:0: 
/home/user/header.h:1:2: error: #error here! 
#error here! 
^

同样的结果为#include "header"