0

I have a small programm that use one installed lib, in tree view it looks like:

`-main.c`
`-include/include.h`
`-makefile`

I compile it like:

gcc $(CFLAGS) -I/usr/include/xlsxwriter -L/usr/lib64/ -lxlsxwriter -o $@ $(OBJS)

But i have some trouble with adding include.h from include dir. I tried this, but it doesn't work:

gcc $(CFLAGS) -I/usr/include/xlsxwriter include/ -L/usr/lib64/ -lxlsxwriter -o $@ $(OBJS)

main.c contains:

#include <xlswriter.h>
#include "include.h"

How could i include my own include.h file to compiler options?

  • 1
    You need to add it like `-Iinclude`. See ["How to add multiple header include and library directories to the search path in a single gcc command?"](https://stackoverflow.com/questions/5846804/how-to-add-multiple-header-include-and-library-directories-to-the-search-path-in) – tlongeri Jan 12 '21 at 00:57
  • And... a distinct `-I` option for *each* added include path. – WhozCraig Jan 12 '21 at 00:58

1 Answers1

0

As was pointed out, you do need an -Ipath for each include path you want to add to the environment, but in your case, you really don't need to use include search path. The include search path should only really be used for directories that are shared between multiple projects.

Use explicit "#include "include/include.h" in the source file. Project local include paths, aren't usually added via the build environment. It's a bad practice, because there might be an include.h file in an earlier include path entry and you clearly want the one that is in your local include directory. It also slows down your build, as the compiler may search multiple paths before finding your target header.

Use the build environment to point to system/global shared include directories only.

See also: Why create an include/ directory in C and C++ projects?

jwdonahue
  • 6,199
  • 2
  • 21
  • 43
  • Omitting varieties of headers’ names, let’s assume that they never match other varieties of names, is it still a bad idea and practice? The question appeared to the author because he concerned the opposite belief that include/include.h is not an example of common practice – chicago_industry Jan 12 '21 at 01:27
  • @chicago_industry, thanks for the feedback. I updated the answer. – jwdonahue Jan 12 '21 at 01:43