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

标签 linux multithreading posix undefined-reference

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

我有 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/

相关文章:

shell - SUS/POSIX shell 兼容的 '-nt' 测试替换

c++ - 为什么 pread 接受偏移值时需要 lseek?

Linux命令或脚本计算文本文件中的重复行?

c - 意外标记附近的语法错误

iphone - Objective-C : Start an object on a background thread -- interact as usual?

java - 与 Thread.sleep() 等效的 Java 接口(interface)是什么?

c# - Async/Await 如何处理 "old"结果?

c++ - C++ 库名称上的 'f' 后缀是什么意思,如何加载它?

mysql - 挂载 LVM2 并从故障 HDD 中恢复丢失的数据?

linux - 如何在 Git 中设置权限?