C语言 在 C 编程中,编译时什么是“未定义引用”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29998485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:54:24  来源:igfitidea点击:

In C programming, what is `undefined reference`error, when compiling?

cundefined-reference

提问by dimitris93

I have this following simple program I am trying to compile in linux ubuntu.

我有以下简单程序,我试图在 linux ubuntu 中编译。

Main.c:

Main.c

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

int main()
{   
    struct Person1 p1 = Person1_Constructor(10, 1000);
}

Person.c:

Person.c

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

struct Person1 Person1_Constructor(const int age, const int salary)
{
    struct Person1 p;
    p.age = age;
    p.salary = salary;
    return p;
};

Person.h:

Person.h

struct Person1
{
    int age, salary;
};
struct Person1 Person1_Constructor(const int age, const int salary);

Why do I get the following error?

为什么会出现以下错误

/tmp/ccCGDJ1k.o: In function `main':
Main.c:(.text+0x2a): undefined reference to `Person1_Constructor'
collect2: error: ld returned 1 exit status

I am using gcc Main.c -o Mainto compile.

我是gcc Main.c -o Main用来编译的。

采纳答案by Diego

When you link the program you need to give both Main.o and Person.o as inputs.

当您链接程序时,您需要同时提供 Main.o 和 Person.o 作为输入。

Build usually is done in two steps (1) compilation and (2) linking. To compile your sources do:

构建通常分两步完成(1)编译和(2)链接。要编译您的源代码,请执行以下操作:

$ gcc -c -o Main.o Main.c
$ gcc -c -o Person.o Person.c

Or in one line:

或者在一行中:

$ gcc -c Main.c Person.c

Then the resulting object files must be linked into a single executable:

然后必须将生成的目标文件链接到单个可执行文件中:

$ gcc -o Main Main.o Person.o

For small projects, of a few compilation units like yours, both step can be done in one gccinvocation:

对于像您这样的几个编译单元的小型项目,这两个步骤都可以在一次gcc调用中完成:

$ gcc -o Main Main.c Person.c

both files must be given because some symbols in Person.care used by Main.c.

两者都必须给予文件,因为某些符号在Person.c被使用Main.c

For bigger projects, the two step process allows to compile only what changed during the generation of the executable. Usually this is done through a Makefile.

对于较大的项目,两步过程允许仅编译在生成可执行文件期间更改的内容。通常这是通过 Makefile 完成的。

回答by Mlagma

The problem is probably occurring because you're compiling only Main.cwhen you need to be compiling Main.cand Person.cat the same time. Try compiling it as gcc Main.c Person.c -o MyProgram.

该问题可能发生,因为你只有在编译Main.c时,你需要进行编译Main.c,并Person.c在同一时间。尝试将其编译为gcc Main.c Person.c -o MyProgram.

Whenever you have multiple files, they all need to be specified when compiling in order to resolve external references, as is the case here. The compiler will spit out object files that will later be linked into an executable. Check out this tutorial on makefiles: Make File TutorialIt helps to automate this process.

每当您有多个文件时,都需要在编译时指定它们以解析外部引用,就像这里的情况。编译器将输出稍后将链接到可执行文件的目标文件。查看有关 makefile 的本教程:Make File Tutorial它有助于自动化此过程。