Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unresolved external symbol for structures in cpp?

I know most of time you get UNRESOLVED EXTERNAL SYMBOL for not including library or for not correctly defining function in class, but i get same error for structure. I have few files .cpp and .h in one of the .h i have defined

extern struct MyDataStruct StructData;

I include this .h in my .cpp file but i get

'struct MyDataStruct StructData' unresolved external symbol

I define this structure out of any class and i access it without any class prefix.

like image 705
Cooker Avatar asked Jul 12 '26 03:07

Cooker


1 Answers

You need to actually define it in one and one only .cpp file ie allocate some space in the object file for it e.g.

struct MyDataStruct StructData;

Note in this case there is no extern.

Whilst all other code accesses it through the declaration in the header which retains the extern - which tells the compiler and linker that somewhere else in the executable there is something to resolve the reference.

like image 97
mmmmmm Avatar answered Jul 14 '26 18:07

mmmmmm