tags:

views:

381

answers:

5

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?

A: 

Most of it's all in the header files, because it's so heavily templated. Very little requires libstdc++.so (iostream, may be it, I think).

eduffy
STL is mostly header files. The Standard library includes other things.
Martin York
All C++ require libstdc++. It has some essential functions in there required by the standard.
Martin York
+5  A: 

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.

Martin York
The directory is `c:\windows\system32`. At least that's where Visual Studio's runtimes are kept. I don't think MinGW links to them though.
avakar
And to add to that, mingw links your app to its standard C++ library statically.
nos
This answer isn't entirely correct. libstdc++ is not the same as MSVCRT.DLL. The latter is the *c* library, the former is the *c++* library. It's true that every binary produced by MinGW will depend on MSVCRT.dll, but that doesn't really have anything to do with the question that was asked. As I point out below, and as nos points out in the previous comment, the libstdc++ library is by default linked statically into your executable.
Matthew Talbert
Furthermore, MSVCRT.DLL is the Visual Studio 6 (VC98) C Runtime. Newer versions have version numbers in the name, e.g. MSVCRT90.DLL, and they are installed in a "SxS" folder.
MSalters
+2  A: 

The only basic run-time dependancy for MinGW C++ programs is on MSVCRT.DLL. Other dependancies will depend on what your program actually does - for example, if it uses ODBC database connectivity, it will depend on ODBC32.DLL (and probably some other Windows DLLs). However, using classes like std::string or std::vector in a MinGW C++ program will not introduce new dynamic library dependancies.

If you are worried about dynamic library dependancies, check out the tool "Dependency Walker" at http://www.dependencywalker.com/

anon
+2  A: 

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.

Matthew Talbert
+1  A: 

C and C++ runtime libraries are linked in the same way as normal libraries, the main difference is that they are usually automatically compiled against and linked by the compiler and linker with no need to specify them.

It is incorrect however, to generalise that you don't have to ship them with your app. In most cases where you ship dynamically linked binaries you will need to include them, for example if you compile with MSVC++, you will link against whatever is installed on your build machine, if you install the dynamically linked binary on a fresh windows install, you are likely to run into problems unless you ensure the libraries are included as part of the installpack (see documentation about visual studio redistributables). The same is true on Solaris machines (standard libraries are upgraded as part of a patch set). With Linux, it's more complicated, you can't link statically due to the GPL, however the libraries are usually installed via distro packages.

Robert Tuck
Good answer, however I was talking about mingw which happens to link standard libraries statically...
Lawand