将C ++代码与'gcc'链接(不含g ++) [英] Linking C++ code with 'gcc' (without g++)

查看:75
本文介绍了将C ++代码与'gcc'链接(不含g ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:快速的问题:我处于一种只使用'gcc'(不含g ++)来生成C ++可执行文件的情况。原因是我必须将代码提交给自动提交服务器,该服务器无法识别'g ++'(或'c ++',就此而言)命令。



在我的实验中,当我编译时,gcc运行良好。问题是,当我尝试链接生成的对象文件时,它会变得混乱。现在,基于我从gcc手册页了解的内容(我可能会离开,所以告诉我,如果我是),g ++基本上是gcc,但是它链接了C ++库。


$ b $如果这是真的,我如何(如果可能的话)使用g ++(或c ++)命令显式地链接C ++库而不使用



编辑:我添加了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 / *


解决方案

标准c ++库,其中 -l <​​/ code>标志为gcc:

  gcc cplusplus .o -lstdc ++ -o myexe 


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.

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.

If this is true, how can I (if possible) explicitly link the C++ library without using the g++ (or c++) command?

EDIT: 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/*

解决方案

You can link the standard c++ library with the -l flag to gcc:

gcc cplusplus.o -lstdc++ -o myexe

这篇关于将C ++代码与'gcc'链接(不含g ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆