问题
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
.
The code of the source files is the following:
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
#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
#include "a.h"
#include "b.h"
#include "global.h"
int main() {
count = 0;
functAb();
functBa();
return 0;
}
The header files:
a.h
#ifndef A_H
#define A_H
#include <stdio.h>
int functA();
int functAb();
#endif
b.h
#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
And, finally, the makefile
that reproduces my error:
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.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?
回答1:
Change your z.c
to
#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 declaration of variable count
but the definition is missing from all files.
You must add the definition to one of the file as int count = some_value;
回答2:
You have declared count, not defined it.
extern
is a part of declaration, not a definition.
To be explicit, extern
is a storage-class specifier and used at declaration.
You need to define int count
somewhere in your source files.
回答3:
You have to add int count;
to your z.c file.
This because of declaring a variable in header file as extern
tells 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.
Then you need to declare the variable somewhere.
来源:https://stackoverflow.com/questions/28090281/undefined-reference-to-global-variable-during-linking