Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid linking to libstdc++

I'm working on an embedded project that currently uses C in Linux and uClibc. We're interested in moving it to C++, but I don't want the overhead associated with linking in libstdc++. My impression is that this is possible provided we don't use anything from STL, such as iostream or vector.

How does one direct g++ to compile without linking to libstdc++?

like image 891
Brian Avatar asked Oct 31 '09 01:10

Brian


3 Answers

You could use

g++ -nodefaultlibs -fno-exceptions a.cc

But you cannot use all c++ features this way...

like image 101
Wienczny Avatar answered Nov 18 '22 20:11

Wienczny


When you compile, use g++ -c to compile only. Then for linking, use ld instead of g++. This invokes the linker directly, which requires you to name all your libraries on the command line (including libc and libcrt), however.

Alternatively, if you're using g++ as a "better c", you may be able to use gcc for your final link step (which will include libc automatically)

like image 32
Kevin Lacquement Avatar answered Nov 18 '22 19:11

Kevin Lacquement


For the sake of completeness and correctness:

g++ -c -fno-exceptions a.cpp
gcc a.o -o a
like image 2
rindeal Avatar answered Nov 18 '22 20:11

rindeal