Home > Software design >  c language including files without directory?
c language including files without directory?

Time:01-12

to include /usr/include/test/head1.h file I should write #include "test/head1.h" but how can I just write #include "head1.h" to include this file.

CodePudding user response:

The compiler looks for the usual places like /usr/include for files.

One way to achieve this if you know your compiler already looks at /usr/include for include files anyway, is the following instead:

#include "test/head1.h"

This would be a good approach if you plan to include no more files from /usr/include/test and you know no other include directory has files with similar name to avoid conflict.

Other method would be to give compiler a hint for the directory while compiling:

gcc -I/usr/include/test <other-options> yourcode.c

Have fun writing C!

CodePudding user response:

The answer above mine is correct, but I just wanted to add some advice pertaining to making the #include that you want work if you're using CMake.

If you're using CMake, you can add the path up till your header in CMakeLists.txt, like so:

target_include_directories(<projectname> PUBLIC path/to/headers), and you can add as many paths as you want, separated by spaces.

  •  Tags:  
  • Related