在《程序员的自我修养》一书中介绍强符号和弱符号:
里面这样写道:
规则1:不允许强符号被多次定义(即不同的目标文件中不能有同名的 强符号);如果有多个强符号定义,则链接器报符号重复定义错误。
规则2:如果一个符号在某个目标文件中是强符号,在其他文件中都是 弱符号,那么选择强符号。
规则3:如果一个符号在所有目标文件中都是弱符号,那么选择其中占 用空间最大的一个。比如目标文件A定义全局变量global为int型,占4个 字节;目标文件B定义global为double型,占8个字节,那么目标文件A和 B链接后,符号global占8个字节(尽量不要使用多个不同类型的弱符 号,否则容易导致很难发现的程序错误)。
书中写道弱符号很有用,
这种弱符号和弱引用对于库来说十分有用,比如库中定义的弱符号可以 被用户定义的强符号所覆盖,从而使得程序可以使用自定义版本的库函数。
给出了一个一个弱符号的例子:
-
-
#include <stdio.h>
-
#include <pthread.h>
-
int pthread_create( pthread_t*,
-
const
pthread_attr_t*,
-
void* (*)(
void*),
-
void*) __
attribute__
((weak));
-
-
int main()
-
{
-
if(pthread_create) {
-
printf(
"This is multi-thread version!\n");
-
// run the multi-thread version
-
// main_multi_thread()
-
}
else {
-
printf(
"This is single-thread version!\n");
-
// run the single-thread version
-
// main_single_thread()
-
}
-
}
预期的运行结果如下:
-
$ gcc pthread.c -o pt
-
$ ./pt
-
This is single-thread version!
-
-
$ gcc pthread.c -lpthread -o pt
-
$ ./pt
-
This is multi-thread version!
但是在我的linux和osx上,无论是否带编译选项 -lpthread 结果都为
This is single-thread version!
后来猜测 -lpthread 没有找到pthread_create符号导致的,并在其他的blog中找到了相同的问题
In GCC, the -pthread (aka -pthreads) option manages both the compiler preprocessor /and/ linker to enable compilation with Posix threads. The preprocessor will define/enable/use Posix threads versions of some macros (or perform conditional compilation to enable Posix threads logic), and the linker will specifically link the resultant object against libpthread
However, -lpthread simply tells the linker to attempt to resolve any external references against the libpthread library, in the same way that -lm tells the linker to attempt to resolve any external references against the libm library. For -lpthread, your code might not contain external references to libpthread, even if you wrote Posix thread code, because the critical macros haven't been switched on.
简单来说就是,-pthread 会去多个库中链接符号,同时会在编译和链接的时候都打开多线程的宏,但是-lpthread只从libpthread中链接,详情见:https://stackoverflow.com/questions/15115260/pthread-library-undefined-reference
记录一下这个问题。
转载:https://blog.csdn.net/u014789533/article/details/113871677