1

I defined namespace "MAPDATA" in a header file(MapData.h) that I have created,and I defined a variable in "MAPDATA".

MapData.h

namespace MAPDATA{

cocos2d::CCSize MapSizeData;
cocos2d::CCSize TileSizeData;
int MaxTileXData;
int MaxTileYData;
}

However, If I write #include "MapData.h" in else class,Linker appear the following error.

duplicate symbol __ZN7MAPDATA11MapSizeDataE in:
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
duplicate symbol __ZN7MAPDATA12TileSizeDataE in:
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
duplicate symbol __ZN7MAPDATA12MaxTileXDataE in:
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
duplicate symbol __ZN7MAPDATA12MaxTileYDataE in:
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/TileMapManage.o
    /Users/nyoronyoro-kun/Desktop/出力先/Build/Intermediates/tilegame.build/Debug-iphonesimulator/tilegame.build/Objects-normal/i386/MapData.o
ld: 4 duplicate symbols for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

But, If I remove name(MAPDATA) of namespace and write #include "MapData.h" in else class, the error disappeared.

After remove name of namespace

namespace {

cocos2d::CCSize MapSizeData;
cocos2d::CCSize TileSizeData;
int MaxTileXData;
int MaxTileYData;
}

If someone could explain clearly why the error disappeared,I would be forever grateful.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
user3321541
  • 221
  • 1
  • 16

1 Answers1

1

This is a fairly common error due to the fact that any implementation file that includes that header will contain a definition of those variables. This will lead to duplicate symbols during linking.

It is resolved by defining a single instance of each of those objects in a new, separate, .cpp file. Also it's not clear to me why you are using a namespace with no name.

So first declare those objects with extern:

MapData.h:

extern cocos2d::CCSize MapSizeData;
extern cocos2d::CCSize TileSizeData;
extern int MaxTileXData;
extern int MaxTileYData;

And then add a source file containing the definition of those object:

MapData.cpp:

#include "MapData.h"
#include ....   // other files

cocos2d::CCSize MapSizeData;
cocos2d::CCSize TileSizeData;
int MaxTileXData;
int MaxTileYData;

Note also, that you might want to put those variables into a class, which can be initialised properly and can maintain all the positive aspects of object orientation.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242