How To Include Files From Multiple Directories In

2019-09-06 13:19发布

       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条回答
贪生不怕死
2楼-- · 2019-09-06 13:46

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

查看更多
登录 后发表回答