Home > 书山有路 > posix thread相关函数的编译(undefined reference to `pthread_create’)

posix thread相关函数的编译(undefined reference to `pthread_create’)

在解APUE2e Exercise 16.3的问题时,尝试着用thread来应答client请求(代码见exercise16-3b.c)。当发现server的某个socket有请求到达时(select函数),创建一个thread,让这个thread accept客户端的请求,然后发送相应的信息。在eclipse上写完程序后,准备编译,却得到undefined reference to `pthread_create’的错误。本以为是参数写错了,查了多遍,都没有问题。然后尝试着用gcc编译,也得到一样的错误信息。Google一下才知道,pthread所在的library并不在编译路径里,需要先链接对应的library才行。

解决办法:
eclipse:
Project Properties -> C/C++ Build -> Settings -> Tool Setting -> GCC C Linker -> Libraries -> Add ‘pthread’,如下图所示。

add build library for pthread

gcc:
加入编译选项lpthread,比如: gcc -o ruptimed exercise16-3b.c -lpthread。

  1. January 16th, 2012 at 23:38 | #1

    和编译有关的疑问,都可以在gcc manual中找到。
    http://gcc.gnu.org/onlinedocs/

  2. litianxiaozi
    January 17th, 2012 at 16:44 | #2

    @Atom 嗯,知道。有时候自己找太麻烦,懒得,就直接google了。

  1. No trackbacks yet.