用GCC编译时出错
每次编译时,我都会收到以下错误消息:
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
我确信这段代码没问题。但我仍然不知道为什么它会给我这个错误。你能帮助我吗?
Every time I compile I get the following error message:
Undefined reference to ( function name )
Let's say I have three files: Main.c
, printhello.h
, printhello.c
. Main.c
calls function print_hello()
, which returns "Hello World". The function is defined in printhello.c
.
Now, here's the following code of printhello.h:
#ifndef PRINTHELLO_H
#define PRINTHELLO_H
void print_hello();
#endif
I am sure this code is fine. I still don't know why is it giving me the error, though. Can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来错误来自链接器而不是编译器。您需要编译并链接这两个源文件。我认为您所做的只是将头文件包含在
Main.c
中,并且您没有编译printhello.c
您需要:
gcc Main.c printhello.c -o myprog
或
先构建目标文件
然后链接它们
It seems that the error is from the linker and not the compiler. You need to compile and link both the source files. I think what you are doing is simply including the header file in
Main.c
and you are not compiling theprinthello.c
You need to :
gcc Main.c printhello.c -o myprog
or
construct the object files first
then link them
未定义的引用是链接器错误。您正在编译并链接所有源文件吗?由于 main.c 调用
print_hello()
,链接器应该看到它的定义。Undefined references are the linker errors. Are you compiling and linking all the source files ? Since the main.c calls
print_hello()
, linker should see the definition of it.我认为该错误是链接器错误而不是编译器错误;它试图告诉您,您尚未提供制作完整程序所需的所有功能。
您需要像这样编译程序:
这假设您的文件
Main.c
类似于:并且您的文件
printhello.c
类似于:您在 < code>printhello.h 应该是:
这明确表示该函数不带参数。带有空括号的声明意味着“有一个函数
print_hello()
,它不返回任何值并采用不确定(但不是可变参数)的参数列表”,这是完全不同的。特别是,您可以使用任意数量的参数调用print_hello()
,并且编译器无法拒绝该程序。请注意,C++ 将空参数列表视为与
void print_hello(void);
相同(因此它将确保对print_hello()
的调用不包含任何参数),但 C++ 是和C不一样。The error is, I think, a linker error rather than a compiler error; it is trying to tell you that you've not provided all the functions that are needed to make a complete program.
You need to compile the program like this:
This assumes that your file
Main.c
is something like:and that your file
printhello.c
is something like:Your declaration in
printhello.h
should be:This explicitly says that the function takes no parameters. The declaration with the empty brackets means "there is a function
print_hello()
which returns no value and takes an indeterminate (but not variadic) list of arguments", which is quite different. In particular, you could callprint_hello()
with any number of arguments and the compiler could not reject the program.Note that C++ treats the empty argument list the same as
void print_hello(void);
(so it would ensure that calls toprint_hello()
include no arguments), but C++ is not the same as C.另一种方法是显式地为 printhello 构建对象文件:
这具有允许其他程序使用 print_hello 方法的额外好处
Another way to do it is to explicitly build object files for the printhello:
This has the added benefit of allowing other programs to use the print_hello method