每次编译我都会收到以下错误消息:
Undefined reference to ( function name )
假设我有三个文件:Main.c
、printhello.h
、printhello.c
。 Main.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/