Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symbol is multiply defined [duplicate]

Tags:

c++

I have a .h file

test.h

std::list<std::string> RegisterList;

I want to access this list in 2 .c files

A.c

#include "test.h"

RegisterList.push_back(myString);

B.c

#include "test.h"

RegisterList.push_back(myString2);

When i compile this code, i get an error ld: fatal: symbol `RegisteredList' is multiply-defined:

What can be the problem ? Is RegsiterList is initialized by default in test.h which is leading to this problem ?

like image 935
Sharath Bhat Avatar asked Feb 14 '13 08:02

Sharath Bhat


1 Answers

Because you defined the object in the header file and violated one definition rule. Never define objects in header file.

If you want to use a global object, You need to declare it as extern and define it in one and only one source file.

like image 104
billz Avatar answered Sep 23 '22 21:09

billz