-4

I have experience in PHP, JS and C#.

I use tutorials from http://learncpp.com to learn C++. But now I have some questions about linking process of C++ Standard Library:

  1. I have heared that C++ Standard Library is automatically linked to C++ programs. Is it possible to prevent this? (I'm using Visual C++ 2010 Express)

  2. Is C++ Standard Library present on major OS(s) such as Windows, Mac OS X, Linux and Unix?

  3. If the answer of Question No2 is true, so why some programs redistribute the Microsoft C++ 2010 Redistributables?

  4. Where C++ Standard Library is actually located on various Operating Systems?

  5. How it's possible to embed Required C++ Standard Library files into the application?

Thanks for your time.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
Amir
  • 1
  • 4
  • 1. Maybe. 2 answered by 3. 4. It isn't, it's part of the compiler platform. 5. is unclear. – Kerrek SB Sep 20 '14 at 12:53
  • @KerrekSB: 4. depends on the implementation and how it's configured. – Deduplicator Sep 20 '14 at 12:55
  • Would you please explain it a little simpler for me,i'm a beginner c++ programmer – Amir Sep 20 '14 at 12:56
  • BTW: You should really learn modern C++. This tutorial is from 2007, and only has a small appendix starting you on C++11 (C++0x). (C++14 is current). – Deduplicator Sep 20 '14 at 12:57
  • 1
    https://stackoverflow.com/questions/1993390/static-linking-vs-dynamic-linking Also see LTO, and others. Anyway, you want a tutorial, which is *too broad* by far. – Deduplicator Sep 20 '14 at 13:01
  • Could you explain why you want to avoid linking the C++ standard library (most compilers have their own standard C++ library)? Also, read [linker](http://en.wikipedia.org/wiki/Linker) wikipage, and [Levine's book: *Linkers and Loaders*](http://www.iecc.com/linker/) – Basile Starynkevitch Sep 20 '14 at 13:16
  • @Deduplicator what resource do your recommend to learn C++? – Amir Sep 20 '14 at 18:34

1 Answers1

1

I have heared that C++ Standard Library is automatically linked to C++ programs. Is it possible to prevent this? (I'm using Visual C++ 2010 Express)

Not sure about MSVC++2010, but in GCC you add the -nostdlib flag to the linker flags. This isn't really recommended unless you know what you're doing, since among those libraries are the runtime that makes sure things like static constructors and destructors get called, variables are initialized, and free space is cleared, in addition to calling main. (One case where it's useful is in embedded systems with no OS.)

Is C++ Standard Library present on major OS(s) such as Windows, Mac OS X, Linux and Unix?

Yes. It's part of the toolchain.

why some programs redistribute the Microsoft C++ 2010 Redistributables?

Because Microsoft decided to implement part of the runtime as a shared library (a.k.a. DLL) instead of just statically linking all of it, or making the shared library a standard part of Windows. Also, it may include Windows-specific APIs that Microsoft added which aren't technically part of the C++ standard libraries.

Where C++ Standard Library is actually located on various Operating Systems?

Buried somewhere in your toolchain. In the case of GCC, you use the gcc-config -L command:

$ gcc-config -L
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/32
$ ls /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3
32             hardenednopie.specs     libgomp.la        libstdc++.a
crtbegin.o     hardenednopiessp.specs  libgomp.so        libstdc++.so
crtbeginP.o    hardenednossp.specs     libgomp.so.1      libstdc++.so.6
crtbeginS.o    include                 libgomp.so.1.0.0  libstdc++.so.6.0.17
crtbeginT.o    include-fixed           libgomp.spec      libsupc++.a
crtend.o       libgcc.a                libitm.a          libsupc++.la
crtendS.o      libgcc_eh.a             libitm.la         plugin
crtfastmath.o  libgcc_s.so             libitm.so         vanilla.specs
crtprec32.o    libgcc_s.so.1           libitm.so.1
crtprec64.o    libgcov.a               libitm.so.1.0.0
crtprec80.o    libgomp.a               libitm.spec

How it's possible to embed Required C++ Standard Library files into the application?

They're linked like any other library, so if you want to hard-wire them in, you use the static versions of these libraries. In Unix-type OSes, you just link to the .a versions of the libraries instead of the .so versions. I'm not sure DLLs have static versions, though...

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • 3
    The default setting is Visual Studio is to link to the DLL version of the standard library, but you can change it to link to the statically linked version. Look under Configuration Properties | C/C++ | Code Generation | Runtime Library and select **Multi-threaded** instead of **Multi-threaded DLL**. – Ferruccio Sep 20 '14 at 13:37
  • Microsoft hasn't decided to link the run-time as a DLL. They have provide the run-time as static library as well. They only have decided to set the default to linking against the DLL. It's up to you to designed if your executable depends on the redistributables or not. – harper Sep 20 '14 at 14:12