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?

2

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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, thanks for the feedback. I updated the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.