c - 使用 GCC 编译时出错

标签 c function gcc reference undefined

每次编译我都会收到以下错误消息:

Undefined reference to ( function name )

假设我有三个文件:Main.cprinthello.hprinthello.cMain.c 调用函数 print_hello(),它返回“Hello World”。该函数在 printhello.c 中定义。

现在,这是 printhello.h 的以下代码:

#ifndef PRINTHELLO_H 
#define PRINTHELLO_H

void print_hello();

#endif

我确信这段代码没问题。不过,我仍然不知道为什么它会给我错误。你能帮帮我吗?

最佳答案

undefined reference 是链接器错误。您正在编译和链接所有源文件吗?由于 main.c 调用 print_hello(),链接器应该看到它的定义。

gcc Main.c printhello.c -o a.out

关于c - 使用 GCC 编译时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7054246/

相关文章:

c - 控制线程挂起和恢复一组工作线程

python - 函数内的函数涉及 Python 中 DataFrame 的每一列

linux - linux下的16位链接

PHP使用字符串指向一个函数

c - 如何在预处理器宏中使用 "sizeof"?

c - 由于 gcc 特定语法,如何阻止 Eclipse CDT 发出错误?

c - 褪色功能错误

c - strcpy 将随机数添加到空字符串

c - C中表达式的解析规则是什么?

javascript - 为什么 Function 属性定义在对象本身上(例如 Function.name),而方法定义在 Function.prototype 上?