Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple definition of ... linker error

I defined a special file: config.h

My project also has files:

t.c, t.h pp.c, pp.h b.c b.h l.cpp 

and #includes:

in t.c:

    #include "t.h"     #include "b.h"     #include "pp.h"     #include "config.h" 

in b.c:

    #include "b.h"     #include "pp.h" 

in pp.c:

    #include "pp.h"     #include "config.h" 

in l.cpp:

    #include "pp.h"     #include "t.h"     #include "config.h" 

there are no include directives in my *.h files, only in *.c files. I defined this in config.h:

const char *names[i] =         {             "brian", "stefan", "steve"         }; 

and need that array in l.cpp, t.c, pp.c but Im getting this error:

pp.o:(.data+0x0): multiple definition of `names' l.o:(.data+0x0): first defined here t.o:(.data+0x0): multiple definition of `names' l.o:(.data+0x0): first defined here collect2: ld returned 1 exit status make: *** [link] Error 1 

I have include guards in every *.h file I use in my project. Any help solving this?

like image 296
mazix Avatar asked Jul 20 '13 17:07

mazix


People also ask

What is multiple definition error in C?

If you put a definition of a global variable in a header file, then this definition will go to every . c file that includes this header, and you will get multiple definition error because a varible may be declared multiple times but can be defined only once.

What does error Multiple definition of main?

Multiple definition of main means you have written main function more than once in your program which is not correct according to C language specifications.

What causes a linker error?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker.

How do you fix a linker error?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.


1 Answers

Don't define variables in headers. Put declarations in header and definitions in one of the .c files.

In config.h

extern const char *names[]; 

In some .c file:

const char *names[] = {    "brian", "stefan", "steve" }; 

If you put a definition of a global variable in a header file, then this definition will go to every .c file that includes this header, and you will get multiple definition error because a varible may be declared multiple times but can be defined only once.

Also, one more thing you can do if you have to define your variables inside of a header file you can use the static keyword.

static const char *names[] = {   "brian", "stefan", "steve" }; 

This way variable names will be defined only once in your entire program and can be accessed multiple number of times.

like image 168
Yu Hao Avatar answered Oct 04 '22 05:10

Yu Hao