g++ why don't you have to link iostream binaries but for pthread you do?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 01:24:11

问题


If you have a very basic C++ program that only uses the 'cout' object, you can include iostream in the source file and then when you compile it you don't have to link any external libraries. In other words, you can simply run

g++ main.cpp -c

g++ main.o -o program

./program

When you want to use more complicated objects such as threads, not only do you include pthread, but when you link the program you have to link to a library.

g++ main.cpp -c

g++ main.o -lpthread -o program

./program

So my question is, why didn't I have to link any libraries to use all the iostream objects?


回答1:


You are linking to libraries when you link with g++ main.o -o program. A few libraries are auto-linked by default, and the only way to not link to them is to pass -nodefaultlibs (or equivalent). In particular, you'll find cout in libstdc++, which in turn uses libc. Both of those are linked by default.

If you have ldd installed, you can verify this by running ldd ./program; it will give you a list of all libraries your program has linked with, directly or indirectly.




回答2:


std::cout is defined in GCC's own C++ standard library, which g++ links to by default, and it only depends on standard C I/O facilities such as FILE* and basic file I/O, which are provided in libc, which gcc and g++ also link to by default. So everything you need to use std::cout is linked to by default.

Functions such as pthread_create are not part of the C++ or C standard libraries, they are defined in the separate library libpthread. That library is not linked to by default by GCC, because Pthreads is not part of the language (it's defined by a different standard, POSIX, but not the language standards) and also because linking to libpthread unconditionally would make many C++ programs run slower, for reasons explained below.

GCC's C++ standard library uses reference counting in a number of places (e.g. in the Copy-On-Write implementation of std::string and in std::shared_ptr) and in multithreaded applications the reference count updates need to use atomic instructions. In non-multithreaded applications the atomic operations are not needed, so libstdc++ uses normal, non-atomic updates if the program is single-threaded. The way it detects whether the program multithreaded or not is by checking whether the program is linked to libpthread or not. So linking to libpthread for all programs by default would cause the library to think all programs are multithreaded, and to always use the slower atomic operations, even when the program doesn't use any threads.

So to avoid making some programs slower it is the user's responsibility to link to libpthread explicitly when required.

On POSIX platforms std::thread is a thin wrapper around Pthreads, so the same rule applies for that type as for the functions like pthread_create: the user must link to libpthread manually. That's true even though std::thread is part of the language standard, but because it's implemented on top of Pthreads and because of the performance implications described above, the user must link to it manually.




回答3:


when building gcc and g++ you may specify "default libraries" to link with.

In some pre-built distribution they are kind enough to do -lstdc++ for you, some distribution don't.



来源:https://stackoverflow.com/questions/25036467/g-why-dont-you-have-to-link-iostream-binaries-but-for-pthread-you-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!