0
votes
       gcc main.c -o main -I include 

I am creating a small c application with following directory structure:

app=>

  • =>src (a directory, with all source files)
  • =>include (a directory, with all header files)
  • =>common (a directory, with all common files)
  • =>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. I am using -I option but it is useful only for one directory path indication. How does the compiler will look in all src, common and include directories to resolve the calls. Kindly suggest me a command or make file to provide path of multiple directories while compiling with gcc.

1
See this might be helpfulSuvarna Pattayil
Are you including .c files?LtWorf
Your grammar is unclear. What is in common/?Beta
Jeez! I wish they wrote manuals for compilers... Don't you?autistic

1 Answers

5
votes

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