我查看了其他问题以确保这不是重复的。
我有 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'
我提出了两个都有效的独立解决方案:
- 这样做:
gcc -pthread -o file2 file2.c file1.c
- 这样做:将
#include "file1.c"
放在代码 file2.c 中,而不是 #include "file1.h"
编辑:如果您有两个主电源,则不能执行方法 #2!
我是一名优秀的程序员,十分优秀!