简体   繁体   English

g++ 无法使用 -I 更改包含路径

[英]g++ cannot change include path with -I

I'm on kubuntu using g++ 7.5.0 / GNU make for C++. My file structure:我在 kubuntu 上使用 g++ 7.5.0 / GNU make for C++。我的文件结构:

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".我希望我的源文件能够检测到 header 个文件,而无需执行 #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请求的makefile

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.不会更改g++的任何默认设置,包括后续调用的搜索路径,正如您所假设的那样。

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 .您需要为每个单独的 c++ 调用传递该编译器标志,这些调用由make根据您的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).后者是您需要调整的,最好使用预定义的 makefile 变量,如CXXFLAGSCXXINCLUDES (查看 GNU-make 文档了解详细信息)。


For your specific case针对您的具体情况

CFLAGS = -c -Wall -I./header

should work.应该管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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