编译时 -pthread 和 -lpthread 之间的区别 [英] Difference between -pthread and -lpthread while compiling

查看:29
本文介绍了编译时 -pthread 和 -lpthread 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译多线程程序时使用的gcc -pthreadgcc -lpthread有什么区别?

What is the difference between gcc -pthread and gcc -lpthread which is used while compiling multithreaded programs?

推荐答案

-pthread 告诉编译器链接 pthread 库并配置线程的编译.

-pthread tells the compiler to link in the pthread library as well as configure the compilation for threads.

例如,以下显示了在我的 Ubuntu 机器上安装的 GCC 包上使用 -pthread 选项时定义的宏:

For example, the following shows the macros that get defined when the -pthread option gets used on the GCC package installed on my Ubuntu machine:

$ gcc -pthread -E -dM test.c > dm.pthread.txt
$ gcc          -E -dM test.c > dm.nopthread.txt
$ diff dm.pthread.txt dm.nopthread.txt 
152d151
< #define _REENTRANT 1
208d206
< #define __USE_REENTRANT 1

使用 -lpthread 选项只会导致链接 pthread 库 - 不会定义预定义的宏.

Using the -lpthread option only causes the pthread library to be linked - the pre-defined macros don't get defined.

底线:您应该使用 -pthread 选项.

Bottom line: you should use the -pthread option.

注意:-pthread 选项在 GCC 文档中被记录为特定于平台的选项,因此它可能并不总是可用.但是,它在 GCC 文档未明确列出的平台上可用(例如 i386 和 x86-64) - 您应该在可用时使用它.

Note: the -pthread option is documented as a platform specific option in the GCC docs, so it might not always be available. However, it is available on platforms that the GCC docs don't explicitly list it for (such as i386 and x86-64) - you should use it when available.

另请注意,GCC 还使用了其他类似的选项,例如 -pthreads(在 Solaris 2 上列为 -pthread 的同义词)和 -mthread(用于 i386 和 x86-64 Windows 上的 MinGW 特定线程支持).我的理解是 GCC 正试图统一使用 -pthread.

Also note that other similar options have been used by GCC, such as -pthreads (listed as a synonym for -pthread on Solaris 2) and -mthread (for MinGW-specific thread support on i386 and x86-64 Windows). My understanding is that GCC is trying to move to using -pthread uniformly going forward.

这篇关于编译时 -pthread 和 -lpthread 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆