[Guide] Compiling Cocos2d-x with CLion

I’m trying to make a little guide, something needs to be retweaked but I think that this will be important for some windows users like me.
Any help would be appreciated for the TODO section, some little imperfection like copying tinyxml and glfw to classes folder for example.

Cocos 2D-x compiling with CLion/CMake under Windows

Installation & Setup

Download Cocos 2d-x latest build [github]

cd cocos2d-x
python download-deps.py
setup.py
cocos new MyGame -p com.your_company.mygame -l cpp -d ProjDir
cd ProjDir/MyGame

Install MSYS2 [download]

start msys2-x86_64-20150512 *(or latest version)*
run msys2

Setup MSYS2 [wiki]

pacman -Sy
pacman --needed -S bash pacman pacman-mirrors msys2-runtime
close MSYS2 and restart with MINGW64 shell
pacman -Su

Install MinGW Packages

  • git
  • mingw-w64-x86_64-cocos2d-x-git
  • mingw-w64-x86_64-cmake
  • mingw-w64-x86_64-toolchain
  • mingw-w64-x86_64-gcc
  • mingw-w64-x86_64-gdb
  • mingw-w64-x86_64-qt-creator (optional)

pacman -S git mingw-w64-x86_64-cocos2d-x-git mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain mingw-w64-x86_64-gcc mingw-w64-x86_64-gdb --force

CLion Setup [download]

Download from JetBrains site, install, then setup.

Settings > Build, Execution, Deployment

> Toolchains

(Environment) > Use MinGW Home: C:\MSYS2\mingw64
(CMake executable) > Use specified: C:\MSYS2\mingw64\bin\cmake.exe
(GDB executable) > MinGW-w64 GDB GNU

> CMake

CMake options: -G"MSYS Makefiles"

Import Project and select ProjDir
Then start to load CMakeLists.txt
If you have errors with Openal not found, install these: (also vorbis, glfw and pkg-config)

pacman -S mingw-w64-x86_64-openal mingw-w64-x86_64-libvorbis mingw-w64-x86_64-glfw mingw-w64-x86_64-pkg-config

Downgrade Chipmunk Lib:
mingw-w64-x86_64-chipmunk-6.2.1-1-any.pkg.tar.xz download

Put package in MSYS2\var\cache\pacman\pkg and then install with:

pacman -U /var/cache/pacman/pkg/mingw-w64-x86_64-chipmunk-6.2.1-1-any.pkg.tar.xz

Change CMakeLists

set(GAME_SRC
  ${PLATFORM_SPECIFIC_SRC}
)

set(GAME_HEADERS
  ${PLATFORM_SPECIFIC_HEADERS}
)

file(GLOB ALL_FILES
    "Classes/*.h"
    "Classes/*.cpp"
)

if(GAME_HEADERS)
add_executable(${APP_NAME} ${GAME_SRC} ${GAME_HEADERS} ${ALL_FILES})
else()
add_executable(${APP_NAME} ${GAME_SRC})
endif()

Could some expert CMake developers say if the “file GLOB” section is correct? This is the only solution I have found for not adding manually all .cpp and .h files

Now reload CMakeLists then Build and cross the fingers!

Useful Links

TODO

(aka not working yet)

  • Resources bugged, especially if subfolders are present
  • Tinyxml2 fixing, avoid copying it in classes folder
  • glfw fixing, same as above

7 Likes

thanks for a very clear tutorial :smile:

after following your guide, I still got error multiple definition of tinyxml2. Do I need to use cocos2d-x prebuilt tinyxml2 and disable calling tinyxml2 from mingw?
Because cocos2d-x already included tinyxml2.cpp in external folder. I guess the source code just got compiled twice if I included another tinyxml2 library
EDIT : I just read your todo sections (oops)… So yeah, it’s weird, why cocos2d-x included tinyxml2 source code while it’s still looking for another tinyxml2 lib in CMakeLists.txt

I’ve temporary solved copying tinyxml2 in Classes folder, but obviously we have to find a more elegant way :stuck_out_tongue:

next error is
1: error: cannot find -lcurl_imp

is there some modification in CMakeLists.txt according this?

Hmmmm, this error is new to me, let me find something…

I looked into FindCURL.cmake, there is only implementation for MSVC. So I guess, it must be the source of problem.

The strange thing is that I don’t have this problem, so maybe I have something installed that unintentionally fix this problem.

still wondering why there is these lines when creating cmakelists
– CURL add to include_dirs: C:/TDM-GCC-64/include
– CURL libs added to ‘cocos2d’: C:/TDM-GCC-64/lib/libcurl_imp.lib

although I have deleted it in FindCURL.cmake and the real file (libcurl_imp.lib)

@Levis
Can you post your last CMakeLists.txt? Because my CMakeLists.txt is still looking for .lib library.

Sorry in this moment I’m in another office, as soon as I return to my workstation I’ll find it and post it.
In some way we have to fix this problem :stuck_out_tongue:

It’s okay, I got past curl problem, I just need to recompile the new CMakesLists.txt and error disappear.

Right now I’m facing this error
D:\mydir\cocos2d\cocos\platform\win32\CCFileUtils-win32.cpp:64: error: undefined reference to `_get_wpgmptr’

I don’t know how to fix it yet

Perfect!

Ok don’t worry for the getwpgmptr error, the fix is described here (thanks to @romsvm) : Trying to make CLion working with CMake and Cocos2DX

It’s awful I know but maybe some guru could help us with the Resources problem :stuck_out_tongue:

Even if this problem is from mingw that doesn’t have _get_wpgmptr.

wow it’s really awful… is there no other way to fix this? So I have to type in my cocos2d-x project Resource’s folder in CCFileUtils-win32.cpp?
EDIT : I found this solution


I’m trying to modify the code now

Helped me a lot. But I also needed to install the following.

pacman -S mingw-w64-x86_64-openal mingw-w64-x86_64-libvorbis mingw-w64-x86_64-glfw mingw-w64-x86_64-pkg-config mingw-w64-x86_64-freetype mingw-w64-x86_64-mpg123 mingw-w64-x86_64-glew mingw-w64-x86_64-boost mingw-w64-x86_64-chipmunk mingw-w64-x86_64-libwebsockets mingw-w64-x86_64-libwebp

Also _pgmptr can be replaced with GetModuleFileNameW:

<<<
WCHAR *pUtf16ExePath = nullptr;
_get_wpgmptr(&pUtf16ExePath);
===
WCHAR pUtf16ExePath[MAX_PATH];
GetModuleFileNameW(NULL, pUtf16ExePath, MAX_PATH);
>>>

And to fix resources loading, you need to change:

<<<
s_resourcePath = convertPathFormatToUnixStyle(utf8ExeDir);
===
s_resourcePath = convertPathFormatToUnixStyle(utf8ExeDir) + "Resources/";
>>>

in cocos2d/cocos/platform/win32/CCFileUtils-win32.cpp.

1 Like

Thank you for these other useful tips!

I almost done ! but i encount problem :smile:
[100%] Linking CXX executable bin\MyGame.exe
…/msys64/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/5.2.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: cannot find -lbullet
…/msys64/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/5.2.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: cannot find -lrecast

and I run
pacman -S mingw-w64-x86_64-cocos2d-x-git
it told me --> target not found

what can I do ?
thanks.

I’ve just updated Cocos v3.9 and I have tried a new compile, without success.
The same error like you.

Furthermore it seems that: mingw-w64-x86_64-cocos2d-x-git no longer exists

So I have to install more things:

pacman -S mingw-w64-x86_64-openal mingw-w64-x86_64-libvorbis mingw-w64-x86_64-glfw mingw-w64-x86_64-pkg-config mingw-w64-x86_64-glew mingw-w64-x86_64-mpg123 mingw-w64-x86_64-freetype mingw-w64-x86_64-libwebp mingw-w64-x86_64-tinyxml2 mingw-w64-x86_64-libwebsockets mingw-w64-x86_64-xxhash mingw-w64-x86_64-flatbuffers mingw-w64-x86_64-bullet
I have found only bullet library in mingw, but not recast.
Anyway the errors after linking are both for Bullet and Recast

Somebody have fixed this?

Hello, this is my solution, works with cocos2d-x v3.9.

1. Install and setup cocos2d-x as descibed in guide

2. Install msys2 as described in http://msys2.github.io/

3. Install additional “MSYS” build packages (copy-paste in msys shell, and press enter):

pacman -S base-devel \
msys2-devel \
mingw-w64-i686-toolchain \
mingw-w64-x86_64-toolchain \
git \
mingw-w64-x86_64-gcc \
mingw-w64-x86_64-gdb \

4. Clone mingw-packages scripts repository:

git clone https://github.com/Alexpux/MINGW-packages.git

I create pull request to official repository, so i hope “cocos2dx-git” package will be avaliable from “pacman” soon

5. Go to cocos2dx-git package build script folder:

cd MINGW-packages/mingw-w64-cocos2dx-git/

6 Build package:

makepkg-mingw -sLf

7. Install package, in my case it named “mingw-w64-x86_64-cocos2d-x-git-r33416.d371ec2-1-any.pkg.tar.xz” (depends on version in package file, mingw-w64-x86_64-cocos2d-x-git-*.pkg.tar.xz will be placed in current folder) so need run command:

pacman -U mingw-w64-x86_64-cocos2d-x-git-r33416.d371ec2-1-any.pkg.tar.xz

8. You can close MSYS shell, no additional packages requered

9. Now you may open CLion and setup toolchain path (Settings > Build, Execution, Deployment) as describe in guide

10. Then you need edit CMakeLists.txt (only CMakeLists in root of project, don’t need edit cocos2d CMakeLists.txt or other). First add recast and bullet library dependences in CMakeLists.txt:

After string:

add_subdirectory(${COCOS2D_ROOT})

Add:

if(WIN32)
 add_subdirectory(${COCOS2D_ROOT}/external/bullet)
 add_subdirectory(${COCOS2D_ROOT}/external/recast)
endif()

11. Then you need add tinyxml source path :

Replace

elseif ( WIN32 )
set(PLATFORM_SPECIFIC_SRC
 proj.win32/main.cpp
 )
set(PLATFORM_SPECIFIC_HEADERS
 proj.win32/main.h
 proj.win32/resource.h
 )
endif()

By:

elseif ( WIN32 )
set(PLATFORM_SPECIFIC_SRC
 proj.win32/main.cpp
 ${COCOS2D_ROOT}/external/tinyxml2/tinyxml2.h
 )
set(PLATFORM_SPECIFIC_HEADERS
 proj.win32/main.h
 proj.win32/resource.h
 ${COCOS2D_ROOT}/external/tinyxml2/tinyxml2.cpp
 )
endif()

12. If you try to build, you see some compilation errors. First error in cocos2d\cocos\ui\UIEditBox\UIEditBoxImpl-win32.cpp in function CWin32InputBox::DlgProc:

Need replace:

TIMERPROC lpTiTorFunc = [](HWND, UINT, UINT, DWORD) {

By:

TIMERPROC lpTimerFunc = [](HWND, UINT, UINT_PTR, DWORD) {

13. If you get error message while compiling “bullet” library:

Replace all error lines with:

reinterpret_cast<uint64_t>

By:

reinterpret_cast<uintptr_t>

And:

btCollisionObject** eaPtr = (btCollisionObject**)(taskDesc.m_mainMemoryPtr);

By:

btCollisionObject** eaPtr = reinterpret_cast<btCollisionObject**>(taskDesc.m_mainMemoryPtr);

14. Then in file cocos2d\cocos\platform\win32\CCFileUtils-win32.cpp in function _checkPath()

Replace:

WCHAR *pUtf16ExePath = nullptr;
_get_wpgmptr(&pUtf16ExePath);

By:

WCHAR pUtf16ExePath[CC_MAX_PATH];
HMODULE hModule = GetModuleHandle(NULL);
GetModuleFileName(hModule, pUtf16ExePath, (sizeof(pUtf16ExePath)));

15. To access to resource folder from app, you need edit CMakeLists.txt OR add line in AppDelegate.cpp in function AppDelegate::applicationDidFinishLaunching():

FileUtils::getInstance()->addSearchPath("Resources\\");

(VS copy resource in build folder, but in CMakeLists resource will be copied in Resources folder)

17. Build and run

2 Likes

Thank you for your contrib!!! Much appreciated!