1

When we create a static library, we have to provide clients with 2 files:

  • .h files
  • .lib files

However, when we create a dynamic library, we must provide clients with 3 files:

  • .h files
  • .lib files (aka import files)
  • .dll files

As far as I know, I cannot build a client app (such as a console app) that

  • statically links against dynamic libraries
  • dynamically links against static libraries

Question

When I build a simple console app as follows, for example:

#include <iostream>

int main()
{
    std::cout << "Hello World!";

    return 0;
}

The output (.exe file in this case) is self-contained. Does it mean that "all c++ standard libraries are static libraries" ?

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • see https://stackoverflow.com/q/26103966/390913 – perreal May 04 '18 at 04:39
  • 1
    Just a note here: DLLs are Microsoft stuff. Other operating systems don't do that two-binary-file-library dance. A shared library lives in a .so file, and you link to that file. (In both cases you need a .h file to tell the compiler what the library provides). – Pete Becker May 04 '18 at 11:35

2 Answers2

3

The output (.exe file in this case) is self-contained. Does it mean that "all c++ standard libraries are static libraries" ?

No. No.

When I execute ldd on a simple C++ program in Linux, I get.

linux-vdso.so.1 =>  (0x00007ffc125f2000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6e371b2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6e3757c000)

That means, the executable will not run unless you have those dynamic libraries.

You will find similar dependencies on Windows.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you. Does it mean that we can always statically link any DLL files? – Second Person Shooter May 04 '18 at 04:50
  • 2
    No. You cannot statically link DLLs. There is a .lib file that corresponds to a DLL. The linker needs that .lib file to create the .exe but the DLL is needed at run time. – R Sahu May 04 '18 at 04:54
  • Thank you. In my understanding, there are only two type of libraries: static libraries and dynamic libraries. So the c++ standard libraries must belong to one of them, but which one? – Second Person Shooter May 04 '18 at 06:04
  • @ArtificialStupidity, the standard does not specify that. It's up to an implementation to make that call. My experience is limited to desktop compilers (g++ and Visual Studio). Both of them happen to use dynamic libraries.I suspect clang uses dynamic libraries too but I don't have any experience with it. – R Sahu May 04 '18 at 06:16
2

On Windows, when building with Visual Studio at least, you can choose whether to link against the static (.lib) or dynamic (.dll) runtime libraries. You select this in the project settings somewhere.

The former makes your .exe more portable as it doesn't rely on the DLLs for the version of the runtime library you linked against being present on the target machine. It is therefore my personal preference. The latter makes your program smaller.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48