Cocos2d-X Coding Style - cannot use another cocos2d based library.

Hello,

I am writing a game based on cocos2d-x.
I am struggling to add other libraries to my game, because I dont think Cocos2d-X can be used properly as a library, referenced by other libraries.
Let me explain :

My project is setup like this :
AwesomeGame depends on

  • cocos2d (lib)
  • cocosDenshion (lib)
  • extensions (lib)
  • App42SDK (lib) who depends on:
    • cocos2d (lib)
  • StoreLib (lib) that depends on:
    • cocos2d (lib)
      etc.

When I build and link AwesomeGame project, I have lots of :

2>libApp42SDK.lib(UserService.obj) : error LNK2005: "public: __thiscall cocos2d::CCAccelerometerDelegate::CCAccelerometerDelegate(void)" (??0CCAccelerometerDelegate@cocos2d@@QAE@XZ) already defined in libcocos2d.lib(libcocos2d.dll)
2>libApp42SDK.lib(UserService.obj) : error LNK2005: "public: __thiscall cocos2d::CCActionEase::CCActionEase(void)" (??0CCActionEase@cocos2d@@QAE@XZ) already defined in libcocos2d.lib(libcocos2d.dll)
2>libApp42SDK.lib(UserService.obj) : error LNK2005: "public: __thiscall cocos2d::CCBlendProtocol::CCBlendProtocol(void)" (??0CCBlendProtocol@cocos2d@@QAE@XZ) already defined in libcocos2d.lib(libcocos2d.dll)
2>libApp42SDK.lib(UserService.obj) : error LNK2005: "public: __thiscall cocos2d::CCKeypadDelegate::CCKeypadDelegate(void)" (??0CCKeypadDelegate@cocos2d@@QAE@XZ) already defined in libcocos2d.lib(libcocos2d.dll)

The reason is that cocos2d implements some functions in its headers.
Then the headers are included by app42SDK, and potentially many other libraries, which means the symbols are duplicated and the final executable cannot link properly.

So I am left wondering :

  • Is there any specific reason why it has been made that way ?
  • Am I missing something ( an option somewhere ? ), because I can see that tinyxml2 has this problem as well…
  • If needed, can we change that to provide functions implementation that are either inlined, or in the source (.cpp) file ?

Last thing : I know cocos2d-x has an “extension” folder where some providers of SDK based on cocos are telling us to put them. However, C++ already has had a system for a very long time to include external code ( via libraries ) and it would make sense for me to get that right, and keep git submodules for each dependencies I have, without mixing sources of all projects in one place just for the sake of doing things “the cocos2d-x way”…

Thanks for any insight.

I think your problems are the duplication of multiple cocos2d-x runtime. You shouldn’t let you App42SDK and StoreLib rely on a separate new cocos2d-x lib. I think both your Awesome games and external lib should rely on the same cocos2d-x lib.

You could achieve this by adding a submodule under your App42SDK , you StoreLib and you Awesome game.

regards,
guanghui