Build static libraries out of Cocos2Dx (HUGE lib size)

I have come to grips with how to pre-build and use the prebuilt libraries of Cocos2Dx with win32.

Now my question is, is there an easy way to create STATIC prebuilt libraries?

My project (following this “hacky” way) is working great, but it is asking for the DLL files. I would like to have the code embedded inside my .exe. This can only be if the Cocos2D libs are created as STATIC libraries, not DYNAMIC ones.

Many thanks in advance :slight_smile:

So, I opened
\cocos2d-x-3.14.1\build\cocos2d-win32.sln
and created 2 additional Configurations, Debug_Static and Release_Static, and marked in each one
libcocos2d->General->Configuration Type->Static Library

All good and fine except, the Debug libcocos2d_static.lib is 3.77GB in size! That’s right, 3.77GB!

The Release version is “only” 400MB in comparison.

Any idea how to “fix” this? :slight_smile:

Another update on my findings:

The Visual Studio linker cannot link the Debug 3.77GB file (due to lack of memory? 8GB 64bit machine here).

While the Release 400MB file does get loaded, my project just won’t compile. I get a bunch of ~250 link errors that look like this:

Error	LNK2001	unresolved external symbol "__declspec(dllimport) public: static void __cdecl cocos2d::GLView::setGLContextAttrs(struct GLContextAttrs &)" (__imp_?setGLContextAttrs@GLView@cocos2d@@SAXAAUGLContextAttrs@@@Z)	PsiEngine	C:\Users\tapanas\Documents\Visual Studio 2015\Projects\PsiEngine\proj.win32\AppDelegate.obj	1	
Error	LNK2001	unresolved external symbol "__declspec(dllimport) public: __thiscall cocos2d::Rect::Rect(float,float,float,float)" (__imp_??0Rect@cocos2d@@QAE@MMMM@Z)	PsiEngine	C:\Users\tapanas\Documents\Visual Studio 2015\Projects\PsiEngine\proj.win32\AppDelegate.obj	1	
Error	LNK2001	unresolved external symbol "__declspec(dllimport) public: void __thiscall cocos2d::Director::setContentScaleFactor(float)" (__imp_?setContentScaleFactor@Director@cocos2d@@QAEXM@Z)	PsiEngine	C:\Users\tapanas\Documents\Visual Studio 2015\Projects\PsiEngine\proj.win32\AppDelegate.obj	1	

If anyone has an idea, please help!

just use cocos gen-libs to do the work for you. No need to hack anything together.

Erm, I did use it. It produces DLLs (dynamic libraries, “externally” used) not STATIC ones (embedded in exe)

Really? When I compile using the pre-builts. I come out with one executable only in my bin/

How did you get Cocos2d-x? From our website or from our GitHub repo?

Edit: there is >General->Configuration Type->Static Library which you already mentioned.

Let me get this straight. If I use the “prebuilt” folder (populated by gen-libs), I also get a single .exe in my bin/ which works just fine from within Visual Studio debugger. But if I double-click the .exe, I get the missing DLLs errors.

That’s cool, but I was wondering if I could just produce a self-sufficient .exe which will contain all code with the help of STATIC cocos2d libs.

To sum this up:

  1. cocos gen-libs is configured to produce DLLs, that’s for sure! You can see the DLLs inside the prebuilt folder.
  2. I opened the win32 .sln (solution) and changed Configuration Type to Static Library, with the errors mentioned in my previous message.

So, some more configuration is required to turn cocos2D into a STATIC lib.

EDIT : I got it from the website, IIRC. It’s 3.14.1

oh, I apologize, I see. I think that others have had to bundle the DLL’s as part of an installer.

@dogwalker what did you do?

@stevetranby IIRC you deploy on Windows?

1 Like

We use DLLs and just include all release version dlls (both x86 and x64) alongside our executable and assets, and in the past we’ve also given links to the VS2015 redistributable (or whichever toolset you’ve built with). I’m not sure which is better (including static builds) as everyone has larger hard drives, but then some may play on laptop with less storage than in recent history … so YMMV??

Since this is orthogonal to Cocos2d your better bet would be search or a visual studio specific forum.
https://msdn.microsoft.com/en-us/library/ms235627.aspx

Maybe others have better advice.

Edit: note that we use Macs for development so we only streamline/optimize with prebuilt and static libraries on that platform. Also, DLLs can have advantages (reloading, run-time plugins, etc), granted with a game and specifically one using cocos2d-x any advantages are likely moot (with exception of final exe/package size).

@bkotsias, the specific issue your seeing there is that the linker is looking for the dll flagged versions of those calls and not the static lib versions. In Cocos2dx the link call type is specified in the macro CC_DLL e.g.

typedef struct CC_DLL PhysicsMaterial

In order to force your target to link the static lib versions you need to define CC_STATIC so that the below fragment in CCPlatformDefine-win32.h sets CC_DLL to nothing

#if defined(CC_STATIC)
    #define CC_DLL
#else

I know this is probably irrelevant for you now but thought I’d put this here for anybody else.

I’ve also noted that .lib’s exist for all the external libraries as well as cocos2d.lib and cocos2dInternal.lib so static linking is entirely possible. In fact I’ve just finished getting this to work on Windows myself, previously I did everything on Linux.

slackmoehrle, I’m sorry, I’ve been out of pocket for several months, with family and work, and I’m just now trying to get back to working on my project for 2017. Honestly, I didn’t build the static library (but I think I’d like to, to reduce build times). I did have to include some visual studio 2012 redistributables. Man, it’s going to take me a while to come back up to speed!

Meanwhile, I think I’d like to upgrade from cocos2d-x 3.13 to 3.15, and maybe upgrade Visual Studio. And I’ll bet stuff has changed on Steam. Whew!