gpt4 book ai didi

linux - 在 Linux 中线程化时对函数的 undefined reference

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:06 24 4
gpt4 key购买 nike

我查看了其他问题以确保这不是重复的。

我有 3 个文件:

file1.h
file1.c
file2.c

file1.h 中有这一行:

void *myFunction(void *vargp);

file1.c 有:

#include <pthread.h>
#include "file1.h"
void *myFunction(void *vargp)
{
//do stuff
return 0;
}

file2.c 有:

#include <pthread.h>
#include "file1.h"

int main()
{
pthread_t thread_id;
int t1;
t1 = pthread_create(&thread_id, NULL, myFunction, NULL);
pthread_join(thread_id, NULL);
return 0;
}

我使用下面的命令编译链接:

gcc -pthread -o file2 file2.c

我得到了这些错误:

file2.c:(.text+0xa64): undefined reference to `myFunction'
file2.c:(.text+0xa68): undefined reference to `myFunction'

最佳答案

我提出了两个都有效的独立解决方案:

  1. 这样做:gcc -pthread -o file2 file2.c file1.c
  2. 这样做:将 #include "file1.c" 放在代码 file2.c 中,而不是 #include "file1.h"

编辑:如果您有两个主电源,则不能执行方法 #2!

关于linux - 在 Linux 中线程化时对函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50996688/

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