gpt4 book ai didi

c - 在 Ubuntu 上编译 C 时没有输入文件或 undefined reference

转载 作者:行者123 更新时间:2023-11-30 15:27:11 25 4
gpt4 key购买 nike

我正在尝试用 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;

}

当我使用这里的第二个解决方案时:Undefined reference to pthread_create in Linux

我收到另一个错误。我在我的 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

关于c - 在 Ubuntu 上编译 C 时没有输入文件或 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27134781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com