c - C:相同签名功能的链接错误
问题描述
运行程序时出现链接错误,即使 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() {
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
在课堂上,我了解到函数签名是函数名及其参数。那么,为什么我在 main 中的程序不调用 test.h 中的 function(5) 来调用 test.c 中的 function(5) 呢?
谢谢
解决方案
static
在全局范围内(函数之外)意味着它仅在此文件中可见(它具有内部链接)。因此,static void function(int n)
从 main.c 中看不到 in test.c。
对于function(5);
main 中的调用,编译器在 test.h 中看到了函数原型,但链接器找不到它的实现,因为 c 文件中的函数是static
.
static
解决方案:如果您想在不同的文件中使用该功能,请删除单词。
推荐阅读
- sql-server - 存储过程错误:消息 512,级别 16,状态 1,过程 sp_ActFTC,第 64 行子查询返回超过 1 个值
- java - 活动类中的getArguments()?
- ubuntu - 无法在 ubuntu 14.04 LTS 上升级乘客
- wcf - WCF -“未能查找通道以接收传入消息。找不到端点或 SOAP 操作。” 但仅来自一位客户
- angular - TypeError:未定义不是对象(评估“parentOutlet.peekState”)
- java - 运行 javafx 应用程序时在后台播放 mp3 文件
- spring-amqp - Spring AMQP 有没有办法设置所有虚拟机的最大消费者数量?
- python - 在 requirements.txt 中安装模块失败,因为找不到在那里定义的另一个模块
- haskell - 为什么某些函数调用在没有类型应用程序的情况下无法工作?
- php - Solr+Php 的查询语法