C++ 如何使用 g++ 创建具有特定名称的目标文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13337869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:12:15  来源:igfitidea点击:

How do I use g++ to create object file with a specific name

c++unixubuntumakefile

提问by Amre

I am making a makefile and one of the targets is exptrtest.o how do I use g++ to create an objectfile with that name, the name of my cpp file is exprtest.cpp not exptrtest.cpp?

我正在制作一个 makefile,其中一个目标是 exptrtest.o 我如何使用 g++ 创建一个具有该名称的目标文件,我的 cpp 文件的名称是 exprtest.cpp 而不是 exptrtest.cpp?

exptrtest.o: exprtest.cpp
    g++ -Wall -g -c exprtest.cpp

to make it more clear, this is my makefile:

为了更清楚,这是我的makefile:

all: exprtest
exprtest: exptrtest.o driver.o parser.tab.o scanner.o
    g++ -Wall -g -o exprtest exptrtest.o driver.o parser.tab.o scanner.o
exptrtest.o: exprtest.cpp
    g++ -Wall -g -c exptrtest.o exprtest.cpp
driver.o: driver.cpp scanner.hpp driver.hpp
    g++ -Wall -g -c driver.cpp
parser.tab.o: parser.tab.hpp parser.tab.cpp
    bison parser.ypp
    g++ -Wall -g -c parser.tab.cpp
scanner.o: scanner.cpp scanner.hpp
    flex -t scanner.ll > scanner.cpp
    g++ -Wall -g -c scanner.cpp
clean:
    rm parser.tab.hpp parser.tab.cpp scanner.cpp

I'm getting the error: "g++: error: exptrtest.o: No such file or directory make: *[exprtest] Error 1"

我收到错误:“g++: error: exptrtest.o: No such file or directory make: *[exprtest] Error 1”

回答by vladr

Use the -ooption in conjunction with -c.

将该-o选项与 结合使用-c

exptrtest.o: exprtest.cpp
    g++ -Wall -g -c exprtest.cpp -o exptrtest.o