Take the 2-minute tour ×

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:

CFLAGS = -Wall -g -x c++

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

exe: model algorithms io stopwatch
    $(COMPILER) $(CFLAGS) main.cpp obj/model.o obj/algorithms.o obj/io.o obj/stopwatch.o -o bin/process

run: exe
    ./bin/process -i data/nasa_small.log -a data/nasa_small.access -s data/nasa_small.stack

    rm -f obj/*
share|improve this question
gcc and g++ literally point at the same executable. The only difference are the flags passed to the back end. –  Loki Astari Jun 16 '09 at 17:23
Fix the automatic submission server! –  Keith Thompson Sep 15 '12 at 23:51
Wasn't an option =D –  Rafael Almeida Sep 16 '12 at 15:26
add comment

2 Answers

up vote 27 down vote accepted

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

gcc -lstdc++ cplusplus.o -o myexe
share|improve this answer
This works great, I didn't know the name of the library to link it. –  Rafael Almeida Jun 16 '09 at 13:49
I've been using this technique successfully for years. But I've just upgraded to Ubuntu 11.10 (gcc 4.6.1) and it's stopped working. I'm now getting "undefined reference to `operator new(unsigned long)'" error on several machines. Has this problem come up for anyone else? –  mgiuca Dec 27 '11 at 1:46
Oh, never mind. It's because I've been putting -lstdc++ before the o file. Newer versions of GCC seem to require that you put libraries after the o files. –  mgiuca Dec 27 '11 at 1:54
Does this link to a dynamic library or a static library? I noticed that there are both .so and .a files on my system for stdc++. –  Raffi Khatchadourian Feb 26 '13 at 3:01
add comment

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.

share|improve this answer
Also a great solution! –  Rafael Almeida Jun 16 '09 at 13:50
add comment

Your Answer


