1. 程式人生 > >執行緒編譯問題(undefined reference to `pthread_create')

執行緒編譯問題(undefined reference to `pthread_create')


#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

void
printids(const char *s)
{

        pid_t pid;
        pthread_t tid;

        pid = getpid();
        tid = pthread_self();
        printf("%s pid = [%u] tid = [%u] [0x%x]\n",
                s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);

}

void *
thr_fn(void *arg)
{

        printids("new thread:");
        return ((void *)0);

}
int
main()
{

        pthread_t ntid;
        int err;

        err = pthread_create(&ntid, NULL, thr_fn, NULL);
        if(err != 0)
                printf("不能建立執行緒[%s]\n",strerror(err));
        printids("main thread:");
        sleep(3);
        exit(0);
}


[[email protected] pthread]# gcc test.c -o test

/tmp/ccq2Mbxs.o: In function `main':
test.c:(.text+0x89): undefined reference to `pthread_create'
collect2: ld 返回 1
[[email protected] pthread]#
問題原因:
   pthread 庫不是 Linux 系統預設的庫,連線時需要使用靜態庫 libpthread.a,所以在使用pthread_create()建立執行緒,以及呼叫 pthread_atfork()函式建立fork處理程式時,需要連結該庫。

問題解決:
    在編譯中要加 -lpthread引數
    gcc test.c -o test -lpthread

相關推薦

執行編譯問題undefined reference to `pthread_create'

#include <stdio.h> #include <pthread.h> #include <stdlib.h> void printids(const char *s) { pid_t pid;

Makefile中要編譯執行是報undefined reference to `pthread_create'類似錯誤

Makefile中要編譯包含執行緒庫的檔案時,把-lpthread放在哪裡呢? 答:把-lpthread放在連結那句編譯命令的最後即可啦。如:  $(CC) -o test_threadpool main.o threadpool.o  -lpthread 編譯階段是不用

Linux Ubuntu執行執行程式出現undefined reference topthread_create’和undefined reference to ‘pthread_join’錯誤。

Linux Ubuntu執行執行緒程式出現undefined reference to ‘pthread_create’和undefined reference to ‘pthread_join’錯誤。 編寫好執行緒程式碼,進行編譯 gcc xiancheng.c -o xiancheng 出

json-c 交叉編譯undefined reference to rpl_malloc

(如果有幫助,請點個贊;如果有錯誤,請指出;如果轉載,請註明出處) 關鍵詞: (1)json-c (2)mips、openwrt (3)Relocations in generic ELF 正文

gcc編譯執行程式需帶-lpthread選項否則出錯:undefined reference to `pthread_create'

程式中兩處使用了pthread_create函式,包含了標頭檔案#include <pthread.h>。 gcc xxx.c -o xxx 編譯時出現以下錯誤: 分析: 用gcc編譯使用了POSIX thread的程式時通常需要加額外的選項,以連結到庫。 此

Linux程式設計時使用gcc編譯.c出現以下問題,pthread.c:(.text+0x29): undefined reference to `pthread_create'collect2: err

Linux程式設計時使用gcc編譯.c出現以下問題 client.c:(.text+0x13e): undefined reference to `pthread_create' collect2: ld 返回 1   解決方法如下: 只需在用gcc編譯時加上-pthread選項即

Caffe 工程的一些編譯錯誤以及解決方案undefined reference to cv::imread

原文連結:http://blog.csdn.net/yhl_leo/article/details/51371936 在編譯caffe時遇到了該博文的第二種情況,改正後可以正確執行: (注意根據電腦情況可使用make all -j16) 整理一下最近遇到caffe工程

編譯caffe時提示未定義的引用undefined reference to

CXX/LD -o .build_release/examples/cifar10/convert_cifar_data.bin .build_release/tools/extract_features.o:在函式‘int feature_extraction_pipeline<float>

Linux 編譯報錯 undefined reference to `pthread_create'

在編譯Linux下多執行緒程式的時候出現錯誤資訊, 寫好程式碼,進行編譯時,出現如下錯誤提示資訊:  thread.c:(.text+0xae): undefined reference to `pthread_create' thread.c:(.

[ c++] cmake 編譯undefined reference to `std::cout' 錯誤的解決方案

bin cut () cmake fin epo linking com urn cmake .. 和 make 之後,出現如下錯誤 Linking CXX executable ../../../bin/ModuleTest CMakeFiles/Modu

C++編譯提示“undefined reference to...”

編寫的一個簡單C++時鐘Clock類,放入了標頭檔案中,主函式在呼叫標頭檔案進行編譯時出現上述錯誤,具體程式碼如下: clock.h: #include <iostream> using namespace std; class Clock { private:

引用qcustomplot編譯錯誤undefined reference to `_imp___ZN8QPrinterC1ENS_11PrinterModeE'

引用qcustomplot,在編譯的時候報了一堆這樣的錯誤:undefined reference to `_imp___ZN8QPrinter C1ENS_11PrinterModeE', undefined reference to `_imp___ZN8QPrinter

codeblocks:編譯問題 undefined reference to vtable for...

一個嚴重的問題,在codeblocks用C++程式設計中出現 undefined reference to vtable for…的問題,昨天就遇到了,今天通過查資料才解決,主要涉及到類中的虛擬函式實現的問題。 相信大家都有習慣,程式設計時,寫幾個函式就編譯一下,否則全寫好再編譯的話會,會

解決undefined reference to `pthread_create'的方法

        工作任務需要,寫了一個小的程式模組來通過uart與下位機進行通訊獲取資料,於是想到線上程中監聽uart字元裝置檔案描述符並處理下位機傳過來的資料。在單獨編譯這個小模組的測試程式時,程式

ubuntu 12.04 codeblocks undefined reference topthread_create'解決方法

settings---》compiler and bug settings --》link settings 在左邊新增libpthread.a  ,右邊新增 -lpthread即可。 ubuntu libpthread.a 的路徑為 /usr/lib/x86_64-lin

ubuntu 下沒有pthread庫以及報undefined reference to 'pthread_create'的解決方法

最近在Ubuntu下編譯一個程式,需要使用多執行緒庫pthread,但是編譯時總是提示“undefined reference to 'pthread_create'”的錯誤,如下圖所示: 要解決這

Makefile編寫執行問題,報錯undefined reference to `main'

Makefile檔案 CC=cc CFLAGS=-g -c OBJDIR=obj OBJS=func.o main_test.o go:$(OBJS:%=$(OBJDIR)/%) ${C

undefined reference to `pthread_create'的錯誤。

晚上編譯一個工程,TMD編譯時總是報thread.o: In function `create_thread': thread.c:(.text+0x1ef): undefined reference to `pthread_create' collect2: ld ret

thread_join.c:(.text+0x6f): undefined reference to `pthread_create' 問題的解決

今天在測試一個執行緒例子時,結果出現如下錯誤 [[email protected] fzf]# gcc thread_join.c -o thread_join /tmp/cc1HozRv.o: In function `main': thread_join.

java自學-執行通訊生產者和消費者問題同步塊實現

注意事項: 1、sleep()和wait()區別:       sleep:進入【普通】阻塞佇列;不釋放鎖;可以在任何地方使用。       wait:進入【該監視器】阻塞佇列,是與sleep不同的阻塞佇列;釋放鎖;wait