makefile 链接线程 pthread

本文介绍了一个使用Makefile编译并链接pthread库的示例。通过简单的C程序创建多个线程并打印信息,演示了如何在Makefile中指定链接选项来连接pthread库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在makefile 链接线程 pthread
源文件:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
  pthread_t threads[NUM_THREADS];
  int rc;
  long t;
  for(t=0;t<NUM_THREADS;t++){
    printf("In main: creating thread %ld\n", t);
    rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
    if (rc){
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }
  pthread_exit(NULL);
}
 
makefile文件:
sortieren : sortieren.o
    gcc -lpthread sortieren.o

sortieren.o : sortieren.c 
    gcc -c sortieren.c

参考自:http://stackoverflow.com/questions/6332410/compile-link-error-using-pthread
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值