简体   繁体   English

如何在 Linux 上的 C 中包含来自多个目录的文件?

[英]How To Include Files From Multiple Directories In C on Linux?

       gcc main.c -o main -I include 

I am creating a small c application with following directory structure:我正在创建一个具有以下目录结构的小型 c 应用程序:

app =>应用=>

  • => src (a directory, with all source files) => src (一个目录,包含所有源文件)
  • => include (a directory, with all header files) =>包含(一个目录,包含所有头文件)
  • => common (a directory, with all common files) => common (一个目录,包含所有公共文件)
  • => main.c => main.c

Now I am trying to run main.c which contains #include directive to include header files from include directory and function calls to .c files in both common and src directories.现在我正在尝试运行包含#include指令的main.c以将头文件从包含目录和函数调用包含commonsrc目录中的.c文件中。 I am using -I option but it is useful only for one directory path indication.我正在使用-I选项,但它仅对一个目录路径指示有用。 How does the compiler will look in all src , common and include directories to resolve the calls.编译器将如何在所有srccommoninclude目录中查找以解析调用。 Kindly suggest me a command or make file to provide path of multiple directories while compiling with gcc .请建议我在使用gcc编译时提供一个命令或制作文件以提供多个目录的路径。

Multiple -I options are permitted.允许多个-I选项。 The description of the -I option from Options for Directory Search states: 目录搜索选项中对-I选项的描述指出:

Add the directory dir to the head of the list of directories to be searched for header files.将目录 dir 添加到要搜索头文件的目录列表的头部。 This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories.这可用于覆盖系统头文件,替换您自己的版本,因为在系统头文件目录之前搜索这些目录。 However, you should not use this option to add directories that contain vendor-supplied system header files (use -isystem for that).但是,您不应使用此选项添加包含供应商提供的系统头文件的目录(为此使用 -isystem)。 If you use more than one -I option , the directories are scanned in left-to-right order;如果您使用多个 -I 选项则按从左到右的顺序扫描目录; the standard system directories come after.标准系统目录紧随其后。

For example:例如:

gcc main.c -o main -Iinclude -Isrc/include -Icommon/include gcc main.c -o main -Iinclude -Isrc/include -Icommon/include

Note that if main.c is using functions implemented in another .c file(s) then the other .c files will also need compiled and linked into the final program binary.请注意,如果main.c使用在另一个.c文件中实现的函数,则其他.c文件也需要编译并链接到最终程序二进制文件中。 For example:例如:

gcc main.c src/another.c -o main -Iinclude -Isrc/include -Icommon/include gcc main.c src/another.c -o main -Iinclude -Isrc/include -Icommon/include

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM