链接期间未定义对全局变量的引用 [英] Undefined reference to global variable during linking
问题描述
我正在尝试编译一个程序,该程序分为3个模块,对应于3个源文件: ac
, bc
和 zc
。 zc
包含 main()
函数,该函数调用 ac $ c $中的函数c>和
bc
。此外, a.c
中的函数调用 b.c
中的函数,反之亦然。最后,有一个全局变量 count
,该变量由三个模块使用,并在单独的头文件 global.h $ c中定义$ c>。
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.c
contains the main()
function, which calls functions in a.c
and b.c
. Furthermore, a function in a.c
calls a function in b.c
, and viceversa. Finally, there is a global variable count
which is used by the three modules and is defined in a separate header file, global.h
.
源文件的代码如下:
ac
#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;
}
bc
#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;
}
zc
#include "a.h"
#include "b.h"
#include "global.h"
int main() {
count = 0;
functAb();
functBa();
return 0;
}
头文件:
ah
#ifndef A_H
#define A_H
#include <stdio.h>
int functA();
int functAb();
#endif
bh
#ifndef B_H
#define B_H
#include <stdio.h>
int functB();
int functBa();
#endif
global.h
#ifndef GLOBAL_H
#define GLOBAL_H
extern int count;
#endif
最后,生成我的错误的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)
这样,我可以编译对象 ao
, bo
和 zo
很好,但是当与 make z
链接时,我得到对'count'
的未定义引用:
With this, I can compile the objects a.o
, b.o
, and z.o
fine, but, when linking with make z
, I get undefined reference to 'count'
in all of them:
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?
推荐答案
更改您的 zc
#include "a.h"
#include "b.h"
#include "global.h"
int count; /* Definition here */
int main() {
count = 0;
functAb();
functBa();
return 0;
}
来自 global.h
,您的所有文件都继承了变量 count
的声明,但是所有文件中都缺少定义。
From global.h
, all your files inherit the declaration of variable count
but the definition is missing from all files.
必须将定义添加到文件之一,因为 int count = some_value;
You must add the definition to one of the file as int count = some_value;
这篇关于链接期间未定义对全局变量的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!