gcc -lpthread 干什么用

#include <stdio.h>

#include <pthread.h>

void *ThreadFunc(void *pArg)  //参数的值为123

{

int i = 0;

for(; i<10; i++)

{

       printf("Hi,I'm child thread,arg is:%d\n", (int)pArg);

       sleep(1);

}

pthread_exit(NULL);

}

int main()

{

              pthread_t thdId;

              pthread_create(&thdId, NULL, ThreadFunc, (void *)123 );

              int i = 0;

              for(; i<10; i++)

              {

                     printf("Hi,I'm main thread,child thread id is:%x\n", thdId);

                      sleep(1);

              }

              return 0;

}

编译时需要带上线程库选项:

gcc  -o  a  a.c  -lpthread

看到 -lpthread就想到了

Gcc –o ./bin/main.exe  ./src/*.c  -I ./include/

但是似乎是不同的。搜索如下:


gcc编译线程程序,为什么要加-lpthread,头文件已经包含了<pthread.h>了啊

包含头文件了,仅能说明有了线程函数的声明, 但是还没有实现, 加上-lpthread是在链接阶段,链接这个库
http://zhidao.baidu.com/link?url=4jUrmjtqneMui7KJmOpxti_HDsAvYjmGhwuVJFLVgdHOrKfC3DKs68cm7g0y6FGxpc_qExVCwz4PMbeb_JqnV_



-lpthread -lm 这两个参数在多线程中起到什么作用? -pthread和-lpthread有什么区别?

lpthread是表示要连接到pthread的库是这里省略的lib,你应该可以找到共享库libpthread.so的。因为pthread编程用到的函数在pthread库里面,就像你使用pow等数学计算函数,需要用到math.h.需要 -lm。 
http://zhidao.baidu.com/link?url=pIOHTwSTuya_zuzCPEeT-1ibV01ISyWSUSX_Duu4qA2OKbHCbkr-7HKOBzIGtcxQ27hvrlqEfqcrqVwzW4wzua

 
 
 
 -pthread 相比于 -lpthread 链接选项究竟多做了什么工作呢?我们可以在verbose模式下执行一下对应的gcc命令行看出来
http://chaoslawful.iteye.com/blog/568602

 

 

 

转载于:https://www.cnblogs.com/kangqy/p/4399621.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值