简体   繁体   中英

Are c++ standard libraries static libraries?

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 dynamic libraries
  • dynamically links 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" ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM