Linux compile command-pthread &-lpthread

Source: Internet
Author: User

Compile makefile time to make compile connection phase always prompt, unable to open such library or the format of such a library is not correct (1 first look at 32 bit, 64 bit whether the corresponding: Bits,cflags lflags....,2 will. Directory import variable libs for OS. A and other dependent libraries)

Then see if there are no corresponding bits of the compiler tool, need to install Gcc-multilib This package, install this package on Ubuntu:
sudo apt-get install Gcc-multilib
The same is true of g++.
sudo apt-get install G++-multilib

See if there is a-l./when connecting to a dynamic library. So

And then look at this command for multithreaded compilation-lpthread

Under normal circumstances, When we link to a library (file name libxxx.so or LIBXXX.A, etc.), we use the-lxxx way, when we use multi-threading in Linux, we need to link pthread Library, according to Convention, we should use-lpthread way to link; but, through daily observation, I find many The open source code uses the-pthread parameter instead of using-lpthread, which is why?

After Google, I finally found some clues:

    1. For portability: in Linux, Pthread is present as a separate library (libpthread.so), but not necessarily in other UNIX variants, such as there is no separate pthread library in FreeBSD, so-lpthread can not be used to link pthread in FreeBSD, and-pth Read does not present this problem because the FreeBSD compiler can correctly expand the-pthread to the dependent parameters under the system. In the same way, other variants will have the same difference, and if you use-lpthread, you may have problems porting to other Unix variants, and in order to maintain high portability, we'd better use-pthread (although this practice is not accepted as a C standard, But it is basically a fact standard).
    2. Add extra Flags: In most systems,-pthread will be expanded to "-d_reentrant-lpthread", that is, in addition to the link Pthread Library, the macro _reentrant is defined first. The purpose of defining this macro is to open various multithreaded support branches in the system header file. For example, we often use the error code flag errno, if not defined _reentrant, is implemented as a global variable, if the _reentrant is defined, it will be implemented for each thread unique, so as to avoid thread contention errors.

Resources:

    1. Https://groups.google.com/forum/#!topic/comp.programming.threads/NCEpG0EOCCY
    2. http://www.lambdacs.com/cpt/FAQ.html#Q300
    3. Ext.: tiger_flying

Linux compile command-pthread &-lpthread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.