我的文件夹结构:
--home
|---an_awesome_lib
| |---Core
| |---src
| | |---header_of_lib.h
| |
| |---build
| |---libawesome.so
|
|---framework
|---project
|---my_program_header.h
在my_program_header.h中,我有以下一行:
#include <an_awesome_lib/Core/src/header_of_lib.h>
我告诉g ++:
-I</home/an_awesome_lib/Core/src/header_of_ib.h> -lawesome -L/home/an_awesome_lib/build
[当我写#include<home/an_awesome_lib/Core/src/header_of_lib.h>
时,编译器不会抛出错误。但是我不想这样做。我要……像#include <an_awesome_lib/Core/src/header_of_lib.h>
,这是一个简单的问题,已经经常被问到。但是,我看不到给g ++的路径与代码中#include中的路径之间的关系。
我读过那里,但没有完整的解释将所有内容放在一起。
What is the difference between #include <filename> and #include "filename"?
https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
How do I include a path to libraries in g++
Using (relative) paths to shortcut in include statements in C++
我希望您有一个一般的解释,可以将其放在上下文中。以下要点仅是一些建议,目的是使我想知道的更加清楚。
-I
指定一个包含目录,而不是文件。 GCC文档reads:
-I dir
...
将目录dir添加到要在预处理期间搜索头文件的目录列表中。
and:
使用预处理指令
#include
包括用户和系统头文件。它有两个变体:
#include <file>
此变体用于系统头文件。它在系统目录的标准列表中搜索名为file的文件。您可以使用
-I
选项在此列表之前添加目录。...
如果您想写
#include <an_awesome_lib/Core/src/header_of_lib.h>
然后您应将/home
指定为包含目录:
-I/home
但是,包括整个home
目录不是一个好主意。我建议您更改很棒的库的目录结构。例如,您可以模仿Boost并在an_awesome_lib
中创建an_awesome_lib
目录:
an_awesome_lib
|--- an_awesome_lib
然后使用-I/home/an_awesome_lib
。
或者您可以在include/an_awesome_lib
中创建an_awesome_lib
:
an_awesome_lib
|--- include
|--- an_awesome_lib
然后使用-I/home/an_awesome_lib/include
。