C:相同签名的函数的链接错误
当我运行我的程序时,我得到了一个链接错误,即使test.h和test.c中的函数签名是相同的: 测试h:C:相同签名的函数的链接错误,c,linker,linker-errors,C,Linker,Linker Errors,当我运行我的程序时,我得到了一个链接错误,即使test.h和test.c中的函数签名是相同的: 测试h: void function(int); 测试c: #include "test.h" #include "stdio.h" static void function(int n) { printf("%d\n", n); } 主要条款c: #include "test.h" int main()
void function(int);
测试c:
#include "test.h"
#include "stdio.h"
static void function(int n) {
printf("%d\n", n);
}
主要条款c:
#include "test.h"
int main() {
function(5);
return 0;
}
这是输出:
Undefined symbols for architecture x86_64:
"_function", referenced from:
_main in ccNaA2H2.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
在课堂上,我了解到函数签名是函数名及其参数。
那么,为什么我的主程序不调用test.h中的函数(5),它将调用test.c中的函数(5)
谢谢
静态
在全局范围内(函数之外)表示它仅在该文件中可见(它具有内部链接)。因此,test.c中的静态无效函数(intn)
在main.c中不可见
用于调用函数(5)
在main中,编译器在test.h中看到函数原型,但是链接器找不到它的实现,因为c文件中的函数是静态的
解决方案:如果要在其他文件中使用函数,请删除单词
static
。在test.c
中,您将函数声明为static
,这意味着它在该模块外不可见。删除静态
关键字