pthread链接问题导致弱符号例子失败

在《程序员的自我修养》一书中介绍强符号和弱符号:

里面这样写道:

规则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

记录一下这个问题。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值