将 C++ 代码与 'gcc' 链接(不带 g++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1001535/
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
Linking C++ code with 'gcc' (without g++)
提问by Rafael Almeida
Hi all: quick question: I'm in a situation where it would be useful to generate my C++ executable using only 'gcc' (without g++). Reason for this is that I have to submit the code to an automatic submission server which doesn't recognize the 'g++' (or 'c++', for that matter) command.
大家好:简短的问题:我现在只使用“gcc”(没有 g++)生成我的 C++ 可执行文件会很有用。这样做的原因是我必须将代码提交到自动提交服务器,该服务器无法识别“g++”(或“c++”,就此而言)命令。
In my experiments, while I'm compilinggcc works well. Problem is, when I try to link the generated object files it gets messed up. Now, based on what I understood from the gcc man page (I may be way off, so tell me if I am), g++ is basically gcc, but it links the C++ library.
在我的实验中,当我编译gcc 时效果很好。问题是,当我尝试链接生成的目标文件时,它会变得一团糟。现在,根据我从 gcc 手册页中的理解(我可能离题了,所以告诉我是不是),g++ 基本上是 gcc,但它链接了 C++ 库。
If this is true, how can I (if possible) explicitly link the C++ library withoutusing the g++ (or c++) command?
如果这是真的,我如何(如果可能)在不使用 g++(或 c++)命令的情况下显式链接 C++ 库?
EDIT:I'm adding the makefile to better illustrate the problem:
编辑:我正在添加 makefile 以更好地说明问题:
COMPILER = gcc
CFLAGS = -Wall -g -x c++
# MODULE COMPILATION
model: modules/model.h modules/sources/model.cpp
$(COMPILER) $(CFLAGS) -c modules/sources/model.cpp -o obj/model.o
algorithms: modules/algorithms.h modules/sources/algorithms.cpp
$(COMPILER) $(CFLAGS) -c modules/sources/algorithms.cpp -o obj/algorithms.o
io: modules/io.h modules/sources/io.cpp
$(COMPILER) $(CFLAGS) -c modules/sources/io.cpp -o obj/io.o
stopwatch: modules/stopwatch.h modules/sources/stopwatch.cpp
$(COMPILER) $(CFLAGS) -c modules/sources/stopwatch.cpp -o obj/stopwatch.o
# EXECUTABLE GENERATION
exe: model algorithms io stopwatch
$(COMPILER) $(CFLAGS) main.cpp obj/model.o obj/algorithms.o obj/io.o obj/stopwatch.o -o bin/process
# DEFAULT TEST CASE
run: exe
./bin/process -i data/nasa_small.log -a data/nasa_small.access -s data/nasa_small.stack
# CLEANING ROUTINE
clean:
rm -f obj/*
回答by CB Bailey
You can link the standard c++ library with the -l
flag to gcc:
您可以将带有-l
标志的标准 c++ 库链接到 gcc:
gcc cplusplus.o -lstdc++ -o myexe
回答by JesperE
If you run g++ with the "-v" option, it will show what command and options it uses. You should be able to deduce the correct gcc command line from there.
如果您使用“-v”选项运行 g++,它将显示它使用的命令和选项。您应该能够从那里推断出正确的 gcc 命令行。