简体   繁体   中英

g++ cannot change include path with -I

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM