C语言 链接期间对全局变量的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28090281/
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:42:32  来源:igfitidea点击:

Undefined reference to global variable during linking

cgcccompilationlinker

提问by freieschaf

I am trying to compile a program which is divided into 3 modules, corresponding to 3 source files: a.c, b.c, and z.c. z.ccontains the main()function, which calls functions in a.cand b.c. Furthermore, a function in a.ccalls a function in b.c, and viceversa. Finally, there is a global variable countwhich is used by the three modules and is defined in a separate header file, global.h.

我试图编译被划分成3个模块,对应于3个源文件的程序:a.cb.c,和z.cz.c包含main()调用a.c和中的函数的函数b.c。此外,函数 ina.c调用函数 in b.c,反之亦然。最后,有一个count由三个模块使用的全局变量,定义在单独的头文件global.h.

The code of the source files is the following:

源文件的代码如下:

a.c

a.c

#include "global.h"
#include "b.h"
#include "a.h"

int functAb() {
    functB();
    functA();
    return 0;
}

int functA() {
    count++;
    printf("A:%d\n", count);
    return 0;
}

b.c

b.c

#include "global.h"
#include "a.h"
#include "b.h"

int functBa() {
    functA();
    functB();
    return 0;
}

int functB() {
    count++;
    printf("B:%d\n", count);
    return 0;
}

z.c

z.c

#include "a.h"
#include "b.h"
#include "global.h"

int main() {
    count = 0;
    functAb();
    functBa();
    return 0;
}

The header files:

头文件:

a.h

a.h

#ifndef A_H
#define A_H

#include <stdio.h>

int functA();
int functAb();

#endif

b.h

b.h

#ifndef B_H
#define B_H

#include <stdio.h>

int functB();
int functBa();

#endif

global.h

global.h

#ifndef GLOBAL_H
#define GLOBAL_H

extern int count;

#endif

And, finally, the makefilethat reproduces my error:

最后,makefile重现我的错误:

CC = gcc
CFLAGS = -O3 -march=native -Wall -Wno-unused-result

z:  a.o b.o z.o global.h
    $(CC) -o z a.o b.o z.o $(CFLAGS)
a.o:    a.c b.h global.h
    $(CC) -c a.c $(CFLAGS)
b.o:    b.c a.h global.h
    $(CC) -c b.c $(CFLAGS)
z.o:    z.c a.h global.h
    $(CC) -c z.c $(CFLAGS)

With this, I can compile the objects a.o, b.o, and z.ofine, but, when linking with make z, I get undefined reference to 'count'in all of them:

有了这个,我可以编译对象a.ob.oz.o很好,但是,当与 链接时make z,我得到undefined reference to 'count'了所有对象:

z.o: In function `main':
z.c:(.text.startup+0x8): undefined reference to `count'
a.o: In function `functAb':
a.c:(.text+0xd): undefined reference to `count'
a.c:(.text+0x22): undefined reference to `count'
a.o: In function `functA':
a.c:(.text+0x46): undefined reference to `count'
a.c:(.text+0x5b): undefined reference to `count'
b.o:b.c:(.text+0xd): more undefined references to `count' follow
collect2: ld returned 1 exit status

I managed to reproduce the error in my actual code in this minimal example, so I guess there is a problem in the dependencies between modules, but I can't spot it. Can anyone point me in the right direction?

在这个最小的例子中,我设法在我的实际代码中重现了错误,所以我猜模块之间的依赖关系存在问题,但我无法发现它。任何人都可以指出我正确的方向吗?

回答by Mohit Jain

Change your z.cto

改变你z.c

#include "a.h"
#include "b.h"
#include "global.h"

int count; /* Definition here */
int main() {
    count = 0;
    functAb();
    functBa();
    return 0;
}

From global.h, all your files inherit the declarationof variable countbut the definitionis missing from all files.

global.h,您的所有文件都继承了变量的声明count但所有文件中都缺少该定义

You must add the definition to one of the file as int count = some_value;

您必须将定义添加到文件之一 int count = some_value;

回答by Sourav Ghosh

You have declaredcount, not definedit.

您已声明计数,而不是定义它。

externis a part of declaration, not a definition.

extern是声明的一部分,而不是定义。

To be explicit, externis a storage-class specifier and used at declaration.

明确地说,extern是一个存储类说明符,用于声明。

You need to defineint countsomewhere in your source files.

您需要在源文件中的某处定义int count

回答by LPs

You have to add int count;to your z.c file. This because of declaring a variable in header file as externtells to the compiler that the variable will be declared in another file, but the variable is not declared yet and will be resolved b linker.

您必须添加int count;到您的 zc 文件中。这是因为在头文件中声明了一个变量,因为它extern告诉编译器该变量将在另一个文件中声明,但该变量尚未声明并将在链接器中解析。

Then you need to declare the variable somewhere.

然后你需要在某处声明变量。