Undefined reference to global variable during linking
Solution 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;
Solution 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.
Solution 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.
Related videos on Youtube
freieschaf
Updated on November 18, 2020Comments
-
freieschaf over 3 years
I am trying to compile a program which is divided into 3 modules, corresponding to 3 source files:
a.c
,b.c
, andz.c
.z.c
contains themain()
function, which calls functions ina.c
andb.c
. Furthermore, a function ina.c
calls a function inb.c
, and viceversa. Finally, there is a global variablecount
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
, andz.o
fine, but, when linking withmake z
, I getundefined 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?
-
Taozi almost 8 yearsCan "int count" be inside the main function?
-
Mohit Jain almost 8 years@Taozi No, it will make the
count
a local variable. We need a global definition. -
Matt Montag over 2 yearsyep, it's diabolical. Sorry to all the beginners :)
-
Daniel N. about 2 yearsIn my case, when I compile inside a Linux distro I have no issues at all, but when I try to compile the same program with cygwin, I get this error. I found that indeed my variables are declared inside a function and that maybe is the root cause of my headaches, but why does it Linux compile without complain?