如何在Linux上的C中包含来自多个目录的文件? [英] How To Include Files From Multiple Directories In C on Linux?

查看:254
本文介绍了如何在Linux上的C中包含来自多个目录的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

       gcc main.c -o main -I include 

我正在创建一个小型c应用程序,其中我的所有源文件都在src目录中,而所有头文件都在include目录中,所有公共文件也都在公共目录中.所有这三个目录与main.c一起位于一个名为"app"目录的目录下.现在,我正在尝试运行main.c,其中包含#include指令,包括来自include和函数调用的头文件,这些头文件包括对common和src目录中的.c文件的调用.我正在使用-I,但仅对一个目录路径指示有用. 我如何告诉编译器查看src common和include目录来解析调用. 请给我建议一个命令或制作文件,以在使用gcc进行编译时包含多个目录的路径.

I am creating a small c application where my all source files are in src directory while all header files in include directory, also all common files are in common directory. All these three directories are under one directory named as "app" directory along with main.c. Now i am trying to run main.c which contains #include directive include header files from include and function calls to .c files in both common and src directories. I am using -I but it is useful only for one directory path indication. How do I tell compiler to look in both src common and include directories to resolve the calls. Kindly suggest me a command or make file to include path of multiple directories while compiling with gcc.

推荐答案

允许使用多个-I选项. 目录搜索选项中-I选项的描述 状态:

Multiple -I options are permitted. The description of the -I option from Options for Directory Search states:

将目录dir添加到要搜索头文件的目录列表的开头.因为这些目录是在系统头文件目录之前搜索的,所以可以用来替代您自己的版本来覆盖系统头文件.但是,您不应使用此选项来添加包含供应商提供的系统头文件的目录(为此使用-isystem). 如果您使用多个-I选项,则会以从左到右的顺序扫描目录;之后是标准系统目录.

Add the directory dir to the head of the list of directories to be searched for header files. 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). If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

例如:

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

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

请注意,如果main.c使用的是在另一个.c文件中实现的功能,则其他.c文件也将需要编译并链接到最终程序二进制文件中.例如:

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. 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

这篇关于如何在Linux上的C中包含来自多个目录的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆