G++无法更改包括-i的路径

发布于 2025-01-30 13:24:55 字数 607 浏览 2 评论 0原文

我使用g ++ 7.5.0 / gnu在kubuntu上制作C ++。我的文件结构:

bin
| .o files

header
|archiver.h

source
|main.cpp
|archiver.cpp

makefile

我希望我的源文件能够检测标头文件而无需执行#include“ ../header/archiver.h”。我尝试使用:

g++ -I/header

但这不起作用。我得到错误:

g++: fatal error: no input files. 

请求的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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2025-02-06 13:24:55

该命令

g++ -I<header-dir>

不会更改g ++的任何默认设置,包括您似乎假设的带有后续调用的搜索路径。

您需要为每个C ++调用传递该编译器标志,该标志由Make根据您的makefile
发出的规则。
后者是您需要适应的方法,最好使用预定义的makefile变量,例如cxxflagscxxCincludes(查看GNU-MAKE文档以获取详细信息)。


对于您的具体情况

CFLAGS = -c -Wall -I./header

应该有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文