歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux業界 >> 解決Linux下編譯時出現的錯誤

解決Linux下編譯時出現的錯誤

日期:2017/3/2 13:45:44   编辑:Linux業界

  (1)由於是Linux新手,所以現在才開始接觸線程編程,照著GUN/Linux編程指南中的一個例子輸入編譯,結果出現如下錯誤:

  undefined reference to 'pthread_create'

  undefined reference to 'pthread_join'

  問題原因:

  pthread 庫不是 Linux 系統默認的庫,連接時需要使用靜態庫 libpthread.a,所以在使用pthread_create()創建線程,以及調用 pthread_atfork()函數建立fork處理程序時,需要鏈接該庫。

  問題解決:

  在編譯中要加 -lpthread參數

  以下是代碼片段:

  gcc -o thread thread.c -lpthread

  thread.c為你些的源文件,不要忘了加上頭文件#include

  (2)出現警告:內建函數strlen不兼容的隱式聲明,內建函數exit不兼容的隱式聲明

  問題原因:因為函數strlen在string內,exit在stdlib內。在文件中沒有添加頭文件 string.h和stdlib

  問題解決:在文件中添加頭文件 string.h和stdlib

  (3)出現錯誤:sem_union的存儲大小未知

  問題原因:在新版2.6內核中關於union sem_union 這個聯合體已經被注釋了,需要自己寫這個聯合體

  問題解決:在C文件中先定義

  union semun {int val; struct semid_ds *buf; unsigned short *array;} sem_union;

  隨後編譯時它就能找到預先定義好的sem_union聯合體了。

Copyright © Linux教程網 All Rights Reserved