[轉]c++使用thread類時編譯出錯,對‘pthread_create’未定義的引用


轉載地址:https://blog.csdn.net/wuhui20091515/article/details/52531202

例子1

#include <iostream>

#include <thread>

using namespce std;

void hello()

{

      cout << "hello concurent world!";

}

int main (int argc, char * argv[])

{

     thread t(hello);

     t.join();

    return 0;

}

這個小例子直接用make編譯是無法通過的。會報錯

/tmp/ccYB66pt.o:在函數‘std::thread::thread<void (&)()>(void (&)())’中:
1-1.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21):對‘pthread_create’未定義的引用
collect2: 錯誤:ld 返回 1
<builtin>: recipe for target '1-1' failed
make: *** [1-1] Error 1

解決方法是在編譯的時候加上-lpthread參數。這個類用到posix實現的線程了。

g++ -o test test.cpp -lpthread

./test

結果輸出 hello concurent world!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM