118

New to C++; Basic understanding of includes, libraries and the compile process. Did a few simple makefiles yet.

My current project involves using an informix DB api and i need to include header files in more than one nonstandard dirs. How to write that ? Havent found anything on the net, probably because i did not use good search terms

This is one way what i tried (not working). Just to show the makefile

LIB=-L/usr/informix/lib/c++
INC=-I/usr/informix/incl/c++ /opt/informix/incl/public

default:    main

main:   test.cpp
        gcc -Wall $(LIB) $(INC) -c test.cpp
        #gcc -Wall $(LIB) $(INC) -I/opt/informix/incl/public -c test.cpp

clean:
        rm -r test.o make.out
MrSmith42
  • 9,961
  • 6
  • 38
  • 49
groovehunter
  • 3,050
  • 7
  • 26
  • 38

3 Answers3

132

You have to prepend every directory with -I:

INC=-I/usr/informix/incl/c++ -I/opt/informix/incl/public
Antoine Pelisse
  • 12,871
  • 4
  • 34
  • 34
105

You need to use -I with each directory. But you can still delimit the directories with whitespace if you use (GNU) make's foreach:

INC=$(DIR1) $(DIR2) ...
INC_PARAMS=$(foreach d, $(INC), -I$d)
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • 25
    Or shorter: `INC_PARAMS=$(INC:%=-I%)` with [substitutions](https://www.gnu.org/software/make/manual/html_node/Substitution-Refs.html). `$(foreach,,)` is more readable, but substitutions are so commonly used that it's good to familiarize how they work anyway. – skalee May 08 '14 at 09:19
  • 5
    or `INC_PARAMS = $(addprefix -I,$(INC))`. – Ma Ming Jun 05 '18 at 03:06
2

Make's substitutions feature is nice and helped me to write

%.i: src/%.c $(INCLUDE)
        gcc -E $(CPPFLAGS) $(INCLUDE:%=-I %) $< > $@

You might find this useful, because it asks make to check for changes in include folders too

Vishwajith.K
  • 85
  • 1
  • 10