Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the C++ standard library linked to my application?

Tags:

c++

mingw

When I usually use code (include headers) from 3rd party (non-standard) C++ libraries, a pre-built binary file is linked to (or included in) the target executable that represents my application, but what happens with C++ standard library?, as far as I have seen I don't have to ship a library with an application that uses code only from the C++ standard library, thus is the code statically linked and included in the executable?

like image 948
Lawand Avatar asked Aug 22 '09 19:08

Lawand


2 Answers

No, the standard libraries by default are dynamically linked at runtime.

When running the dynamic loader will look in a couple of standard places for dynamic libraries if it finds it loads and runs otherwise the application quits.

On Unix Systems:
/usr/lib: look for: libstdc++*

On Windows:
c:\windows\system32 look for: MSVCRT.DLL

There are also a couple of Environment variables that can affect the search path. Look at your platforms man page for dlopen to see what they are. Everything you need should be in the man pages for dlopen on your platform.

Most systems have these libs in the appropriate places and will automatically be found.
The rest of the STL will not introduces extra shared lib dependencies.

like image 53
Martin York Avatar answered Oct 01 '22 06:10

Martin York


In recent MinGW gcc/g++ versions (4.40) you can link against a shared dll rather than the default static library by using the flag -shared-libstdc++.

The static versions of the library are located in /mingw/lib/gcc/mingw32/[gcc version]. The file name is libstdc++.a. This will be linked in by default when compiling a c++ app with MinGW.

like image 40
Matthew Talbert Avatar answered Oct 01 '22 04:10

Matthew Talbert