在github上看到一个项目,其中lib
目录下的实现文件包含有另一个head
文件夹下的头文件。lib
文件夹和head
文件夹不是同级的文件夹,head
文件夹是另一个文件夹的子文件夹。但是lib
文件夹里面的实现文件直接以#include "head/xx.hpp"
的方式包含着头文件的。想知道为什么可以这样包含,lib
下的实现文件如何能够访问到那个头文件呢?
先谢谢大家了
怪我咯2017-04-17 14:46:52
At this time, you should use the -I
parameter in gcc. For example, your head/xxx.h
position is in /path/to/libs/head/xxx.h
, and when drinking, use #include "head/xxx.h"
. Just compile with the following parameters:
gcc lib/xxx.c -I/path/to/libs
man gcc
is as follows:
-I dir
Add the directory dir to the list of directories to be searched for
header files. Directories named by -I are searched before the
standard system include directories. If the directory dir is a
standard system include directory, the option is ignored to ensure
that the default search order for system directories and the
special treatment of system headers are not defeated . If dir
begins with "=", then the "=" will be replaced by the sysroot
prefix; see --sysroot and -isysroot.
ringa_lee2017-04-17 14:46:52
There should be a configuration option that sets the previously omitted path prefix. Clone the project, use sublime or something to globally search for the missing path characters, and you will know where it is configured.
Normally when you write a program, the previous #include<stdio.h>
is not placed in the currently visible directory. The Linux system has some default environment variables like C_INCLUDE_PATH
, which store some system paths. These paths are the header file paths that will be searched by default when you include. You can continue to add your own path later.
I don’t know what platform your project is on. Like Android, when compiling jni, just use LOCAL_C_INCLUDES to specify the header file path, and the long header file path can be omitted from the code. .
You can also search for keywords linux system header file search path and so on to learn relevant knowledge.