bob.sacamento bob.sacamento - 20 days ago 10
C Question

need a workaround for a "multiple definition" error

A toy code illustrating my problem is as follows:

stuff.h:

#ifndef STUFF
#define STUFF

int a;
int testarr[]={1,2,3};

#endif


fcn.h:

#include "stuff.h"
int b[]={5,6,7};
void fcn();


main.h:

#include "stuff.h"
#include <stdio.h>


fcn.c:

#include "main.h"

void fcn() {
printf("Hello\n");
}


main.c:

#include "main.h"
#include "fcn.h"

int main() {
fcn();
printf("HI\n");
}


An attempt to compile fails with:

/g/pe_19976/fcn_2.o:(.data+0x40): multiple definition of `testarr'
/g/pe_19976/main_1.o:(.data+0x40): first defined here


After doing some reading, I realize that defining the array
testarr
in the header file is a problem. But the thing is, in my real code, several files need access to
testarr
and it needs to have the same assigned values everywhere. I guess I could put it in
main.h
(?) but even if that would work, in my real code it logically belongs in
stuff.h
. How do I solve this conundrum?

BTW, based on something else I found, I tried defining
testarr
as
extern
but got the same problem.

Answer

When you put a variable definition into a header file, any .c file that includes it will have a copy of that variable. When you then attempt to link them, you get a multiple definition error.

Your header files should contain only a declaration of the variable. This is done using the extern keyword, and with no initializer.

Then in exactly one .c file, you put the definition along with an optional initializer.

For example:

main.c:

#include "main.h"
#include "fcn.h"

int a;
int testarr[]={1,2,3};

int main() {
   fcn();
   printf("HI\n");
}

stuff.h:

#ifndef STUFF
#define STUFF

extern int a;
extern int testarr[];

#endif

fcn.h:

#include "stuff.h"
extern int b[];
void fcn();

fcn.c:

#include "main.h"

int b[]={5,6,7};

void fcn() {
   printf("Hello\n");
}
Comments