Linking C++ code with #39;gcc#39; (without g++)(将 C++ 代码与“gcc链接(没有 g++))
问题描述
大家好:快速提问:我的情况是,只使用gcc"(不使用 g++)生成我的 C++ 可执行文件会很有用.原因是我必须将代码提交到无法识别g++"(或c++")命令的自动提交服务器.
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 运行良好.问题是,当我尝试链接生成的目标文件时,它会搞砸.现在,根据我从 gcc 手册页中的理解(我可能离题了,如果我是的话,请告诉我),g++ 基本上是 gcc,但它链接了 C++ 库.
In my experiments, while I'm compiling gcc 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.
如果这是真的,我如何(如果可能)使用 g++(或 c++)命令不显式链接 C++ 库?
If this is true, how can I (if possible) explicitly link the C++ library without using the g++ (or c++) command?
我正在添加生成文件以更好地说明问题:
I'm adding the makefile to better illustrate the problem:
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/*
推荐答案
您可以将带有 -l
标志的标准 c++ 库链接到 gcc:
You can link the standard c++ library with the -l
flag to gcc:
gcc cplusplus.o -lstdc++ -o myexe
这篇关于将 C++ 代码与“gcc"链接(没有 g++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 C++ 代码与“gcc"链接(没有 g++)


- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07