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

梦想与她 提交于 2019-12-12 05:19:49

问题


       gcc main.c -o main -I include 

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.


回答1:


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

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.

For example:

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. For example:

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



来源:https://stackoverflow.com/questions/15570010/how-to-include-files-from-multiple-directories-in-c-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!