Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to global variable during linking

Tags:

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?

like image 891
freieschaf Avatar asked Jan 22 '15 13:01

freieschaf


2 Answers

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;

like image 147
Mohit Jain Avatar answered Sep 24 '22 07:09

Mohit Jain


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.

like image 32
Sourav Ghosh Avatar answered Sep 21 '22 07:09

Sourav Ghosh