Sunday, July 28, 2013

Compiling Cocos2d-x using static glfw 2.7.9 libs

    Over the weekend I wanted to explore the different cross platform frameworks; namely for Android, Linux and iOS operating system platforms. Another caveat for my choice of framework was a C++ API language interface to the framework, this led my to download Cocos2d-x. So, I did what almost every programmer does with archive software packages. Namely download, extract and look at the file directly for signs on how to build, then try to compile it. Readme files and library dependency listings be damned!

So, here are the steps used successfully to compile Cocos2d-x on my Linux box, with Arch Linux as the distro installed. The only problem that I encountered was that Arch Linux current version of needed library glfw, is version 3.0.1-2 and Cocos2d-x was written use glfw version 2.x. To answer your anticipated question. No, glfw-3.x is not backwards compatible with glfw-2.x. Different include file structure, function calls and compatibility issues of the like of such, the glfw organization post on June 12, 2013 http://www.glfw.org/ gives more insight into the matter of transitioning.

Cocos2d-x comes with pre-configured Makefiles, ergo there are no automake, qmake, cmake or configure files to run. If you simply type make and your dependencies are installed and your system has libglfw-2.x then ideally you should have no problems compiling the static libs and the samples. However if your system has libglfw-3.x installed then you get the following error


./platform/linux/CCEGLView.cpp:10:21: fatal error: GL/glfw.h: No such file or directory #include "GL/glfw.h"

So, not wanting to down grade my glfw library, the game plan to fix this error is to download glfx-2.7.9 source code, build the static glfw library, remove reference to the dynamic link library and set the Cocos2d-x Makefile to reference the the proper include GL/glfw.h and then link with our built static library.

Steps:
1) Download and extract Cocos2d, cd to cocos2d-x-2.1.4/cocos2dx/platform/third_party/linux
 
2) Download and extract glfw-2.7.9 your will find it here http://sourceforge.net/projects/glfw/files/glfw/2.7.9/ 

3) Now your ls . should show curl include64 libjpeg  libraries  libwebp glfw-2.7.9  libfreetype2  libpng libtiff

4) cd glfw-2.7.9, then type  make x11-clean && make x11
5)Returning to the root directory were you installed cocos2d-x
 <using your favorite editor, emacs, nano, vi, etc: edit>   cocos2d-x-2.1.4/cocos2dx/proj.linux/cocos2dx.mk
ADD: -I$(COCOS_SRC)/platform/third_party/linux/glfw-2.7.9/include \
FROM:
  -I$(COCOS_SRC)/platform/third_party/linux/libtiff/include \
  -I$(COCOS_SRC)/platform/third_party/linux/libwebp
.
.
.
TO:
  -I$(COCOS_SRC)/platform/third_party/linux/libtiff/include \
  -I$(COCOS_SRC)/platform/third_party/linux/glfw-2.7.9/include \
  -I$(COCOS_SRC)/platform/third_party/linux/libwebp


6)
ADD: $(COCOS_SRC)/platform/third_party/linux/glfw-2.7.9/lib/x11/libglfw.a \
FROM:
    $(STATICLIBS_DIR)/libtiff.a \
    $(STATICLIBS_DIR)/libwebp.a
.
.
.
TO:
    $(STATICLIBS_DIR)/libtiff.a \
    $(COCOS_SRC)/platform/third_party/linux/glfw-2.7.9/lib/x11/libglfw.a \
    $(STATICLIBS_DIR)/libwebp.a

7)
REMOVE:  -lglfw
FROM:
SHAREDLIBS += -lglfw -lGLEW -lfontconfig -lpthread -lGL #DLE
.
.
.
TO:
SHAREDLIBS +=  -lGLEW -lfontconfig -lpthread -lGL

8)
ADD:  -lXrandr
FROM:
LIBS = -lrt -lz -lX11
.
.
.
TO:
LIBS = -lrt -lz -lX11 -lXrandr

9) Now save the cocos2dx.mk file and recompile. Hopefully all should go well and compile, run the linux
   Sample programs to confirm everything is correct. Run the Hello program is in cocos2d-x-2.1.4/samples/Cpp/HelloCpp/proj.linux/bin/release

Good Luck, hope this helps.

1 comment:

  1. That really helped me a lot. I had to modify the glfw-2.7.9 makefile to make it work though. I simply added -fPIC to the linker's flags before calling "make x11-clean && make x11".
    Thank you !

    ReplyDelete