Home > Mobile >  g cannot change include path with -I
g cannot change include path with -I

Time:05-19

I'm on kubuntu using g 7.5.0 / GNU make for C . My file structure:

bin
| .o files

header
|archiver.h

source
|main.cpp
|archiver.cpp

makefile

I want my source files to be able to detect header files without having to do #include "../header/archiver.h". I've tried using:

g   -I/header

but this does not work. I get the error:

g  : fatal error: no input files. 

makefile that was requested

CC = g  
CFLAGS = -c -Wall

objects = bin/main.o bin/archiver.o

all : $(objects)
    $(CC) -o build $(objects)

bin/%.o : source/%.cpp
    $(CC) $(CFLAGS) $?
    mv *.o bin

.PHONY : clean
clean : 
    rm -rf all $(objects)

CodePudding user response:

The command

g   -I<header-dir>

doesn't change any default settings for the g include search paths with subsequent calls, as you seem to assume.

You'll need to pass that compiler flag for each individual c call, which are issued by make according the rules defined in your makefile.
The latter is what you need to adapt, best using a pre-defined makefile variable like CXXFLAGS or CXXINCLUDES (check the GNU-make documentation for details).


For your specific case

CFLAGS = -c -Wall -I./header

should work.

  • Related