簡體   English   中英

在Ubuntu上編譯C時沒有輸入文件或未定義的引用

[英]No input files or undefined reference when compiling C on Ubuntu

我正在嘗試在C中編譯此代碼。

 gcc -o -pthread test1.c

我正在使用Linux。 不幸的是我得到:

undefined reference to `pthread_create'
 undefined reference to `thread_join'
  collect2: error: ld returned 1 exit status

當我嘗試編譯時:

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


    void* thread_func(void* parameter){

        int i =0;
        for(i=0; i<5;i++)
        {
            printf("%d\n", i);
        }

      return 0;


    }


 int main (int argc, char *argv)
 {
     pthread_t thread_handle;
     int ret = pthread_create(&thread_handle,0,thread_func,0);
     if(ret != 0) {
        printf("Create thread failed! error: %d",ret);
        return 1;
      }

      thread_join(thread_handle,0);

      return 0;

}

當我從此處使用第二種解決方案時: 在Linux中對pthread_create的未定義引用

我收到另一個錯誤。 我在我的test1.c所在的目錄中。

no input files





ADDIN ERRORS:
1)using gcc -o someFile -pthread test1.c
-undefined reference to `thread_join'
2)gcc -o -pthread test1.c
-undefined reference to `thread_join'
-undefined reference to `pthread_create'

3)gcc test1.c -o test1.o -I. -lpthread 
- undefined reference to `thread_join'
4)gcc -pthread -o test.c
- no input files

我將不勝感激,因為我無法學習線程,因為我無法編譯它。

選項-o之后的單詞是輸出文件,即,您指定-pthread作為輸出文件。 更改為gcc -o someFile -pthread test1.c應該可以解決問題。 您的可執行輸出文件將是someFile

嘗試使用-lpthread進行編譯

gcc -lpthread test1.c -o test1

鏈接器按照命令行上寫的順序處理內容。

因此,在您的示例中,當鏈接器到達您的.o文件,並且需要查找pthread調用的外部引用時,它沒有任何內容可查找。

您可以嘗試:

gcc test1.c -o test1.o -I. -lpthread 

-I. 表示要在當前目錄中查找#include "header.h"文件, -lpthread表示要使用libpthread.a庫文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM