Qt5編譯添加gcc參數


ZC: 都是網上搜到的

 

1、

問題:

在linux下編寫多線程,已經添加#include<pthread.h>頭文件,qt編譯器提示: error: undefined reference to `pthread_create'。
經過百度,得知:“pthread 庫不是 Linux 系統默認的庫,連接時需要使用靜態庫 libpthread.a,所以在使用pthread_create()創建線程,以及調用 pthread_atfork()函數建立fork處理程序時,需要鏈接該庫。”
所以,解決方法是:“在編譯中要加 -lpthread參數(gcc thread.c -o thread -lpthread)”。

問題是,我不想在終端單獨打gcc命令,而是希望能在qtcreator中直接編譯。請問該如何設置(添加上這一個-lpthread的后綴)?

 

解決方法是:

  在多線程那個項目的 .pro文件 中添加:LIBS += -lpthread 。

 

2、

問題:

undefined reference to `shm_open'

undefined reference to `shm_unlink'

 

解決:

注意一下man shm_open的幫助文件的最后幾行:

NOTES

       These functions are provided in glibc 2.2 and  later.   
Programs  using

       these  functions  must  specify  the  -lrt  flag to cc in order to link

       against the required ("realtime") library.

 

       POSIX leaves the behavior of the combination of  O_RDONLY  and  O_TRUNC

       unspecified.   On  Linux,  this  will successfully truncate an existing

       shared memory object - this may not be so on other Unices.

 

       The POSIX shared memory object implementation on Linux 2.4 makes use of

a dedicated file system, which is normally mounted under /dev/shm.

 

如果你注意到的話,這樣編譯就能通過了:

gcc -lrt -o test test.c

其實就是要連接庫的原因。

 

3、

4、

5、

 


免責聲明!

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



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