用GCC编译时出错

发布于 2024-11-29 05:44:08 字数 469 浏览 0 评论 0原文

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

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

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

好倦 2024-12-06 05:44:09

看来错误来自链接器而不是编译器。您需要编译并链接这两个源文件。我认为您所做的只是将头文件包含在 Main.c 中,并且您没有编译 printhello.c

您需要:

gcc Main.c printhello.c -o myprog

先构建目标文件

gcc -c printhello.c
gcc -c Main.c

然后链接它们

gcc Main.o printhello.o

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 the printhello.c

You need to :

gcc Main.c printhello.c -o myprog

or

construct the object files first

gcc -c printhello.c
gcc -c Main.c

then link them

gcc Main.o printhello.o
原谅过去的我 2024-12-06 05:44:08

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

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

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.

gcc Main.c printhello.c -o a.out
抽个烟儿 2024-12-06 05:44:08

我认为该错误是链接器错误而不是编译器错误;它试图告诉您,您尚未提供制作完整程序所需的所有功能。

您需要像这样编译程序:

gcc -o printhello Main.c printhello.c

这假设您的文件 Main.c 类似于:

#include "printhello.h"

int main(void)
{
    print_hello();
    return 0;
}

并且您的文件 printhello.c 类似于:

#include "printhello.h"
#include <stdio.h>

void print_hello(void)
{
    puts("Hello World");
}

您在 < code>printhello.h 应该是:

void print_hello(void);

这明确表示该函数不带参数。带有空括号的声明意味着“有一个函数 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:

gcc -o printhello Main.c printhello.c

This assumes that your file Main.c is something like:

#include "printhello.h"

int main(void)
{
    print_hello();
    return 0;
}

and that your file printhello.c is something like:

#include "printhello.h"
#include <stdio.h>

void print_hello(void)
{
    puts("Hello World");
}

Your declaration in printhello.h should be:

void print_hello(void);

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 call print_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 to print_hello() include no arguments), but C++ is not the same as C.

椵侞 2024-12-06 05:44:08

另一种方法是显式地为 printhello 构建对象文件:

gcc -c printhello.c -o printhello.o
gcc -o Main main.c printhello.o

这具有允许其他程序使用 print_hello 方法的额外好处

Another way to do it is to explicitly build object files for the printhello:

gcc -c printhello.c -o printhello.o
gcc -o Main main.c printhello.o

This has the added benefit of allowing other programs to use the print_hello method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文