Avoid linking to libstdc++
Asked Answered
R

3

19

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++?

Ramiah answered 31/10, 2009 at 1:9 Comment(0)
T
12

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)

Trottier answered 31/10, 2009 at 1:13 Comment(4)
When I try this I get "undefined reference to `__gxx_personality_v0'" when trying to link.Ramiah
I think that means you're using some C++ specific feature. As to which feature it is, I'm not sure.Trottier
Okay, I got it to work by referring to <a href="#329559 question</a>. You have to compile with "g++ -fno-exceptions -c" and then link separately.Ramiah
That link was supposed to be: https://mcmap.net/q/94628/-what-is-__gxx_personality_v0-forRamiah
B
13

You could use

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

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

Baalbeer answered 31/10, 2009 at 1:19 Comment(0)
T
12

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)

Trottier answered 31/10, 2009 at 1:13 Comment(4)
When I try this I get "undefined reference to `__gxx_personality_v0'" when trying to link.Ramiah
I think that means you're using some C++ specific feature. As to which feature it is, I'm not sure.Trottier
Okay, I got it to work by referring to <a href="#329559 question</a>. You have to compile with "g++ -fno-exceptions -c" and then link separately.Ramiah
That link was supposed to be: https://mcmap.net/q/94628/-what-is-__gxx_personality_v0-forRamiah
C
2

For the sake of completeness and correctness:

g++ -c -fno-exceptions a.cpp
gcc a.o -o a
Conveyor answered 16/12, 2015 at 20:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.