Setup Cocos2dx environment in Linux for Android (Part 1)

In this post I’m going to explain the steps for installing the Cocos2dx environment in Linux for working with Eclipse and Android. We travel from basics to compile and execute the examples HelloCpp in Linux and TestCpp in Android. I’m assuming you have knowledge of Android, C++, Linux and you have already installed  and configured Eclipse with Android.  I’ve included some paths in my .bashrc file. I’m going to use them during this post, with this I can make some things easyly, for example, I can access to my android adb from any terminal. In order to do this, open a terminal in your home directory, and with your favourite editor modify the .bashrc file, nano .bashrc, and add the following lines:

Now for making the change effective we have to execute the following command: source .bashrc. The first thing that we’re going to do is install the eclipse plugin for C/C++, the Eclipse CDT Plugin, we have two different ways for doing that. The first one, is looking for it in the Eclipse Marketplace via Help > Eclipse Marketplace..”, the second one is using the update site: http://download.eclipse.org/tools/cdt/releases/juno. After this, we’re going to download the last version of Cocos2dx from the official site (http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Download), in our case the version 2.1.4, and the last version of the Android NDK (http://developer.android.com/tools/sdk/ndk/index.html), unzip both files in a directory of our choice, in my case the “Programas” directory.

Now we have to define a few environment variables by editing the .bashrc file again nano .bashrc.

Again, for making the change effective we have to execute the following command: source .bashrc. You must be sure that you have installed the Java JDK because we’re going to need it later. If you don’t have it installed, use the command sudo apt-get install openjdk-6-jdk. If you have already installed a Java JRE, check your version with java -version, in my case I had the 6, so I installed the correspondednt JDK.

Now the fun part begins. We’re going to install the necesary dependences to use Cocos2dx. If you have not installed it yet, you’ll have to install the build essentials and the ia32-libs packages, check my previous post. Now we launch the scrpit “make-all-linux-project.sh”, in the root directory of the Cocos2dx uncompressed files:  sudo bash make-all-linux-project.sh. When the process finished, we’ll check if everything went ok, so we have to go to the  “cocos2d-x-2.1.4/samples/Cpp/HelloCpp/proj.linux/bin/release”  directory and execute the HelloCpp: ./HelloCpp. If you see a window with the Cocos2dx logo and the “Hello World” text, we can proudly follow with the next step.

Now starts the hard part. Let’s make all this work in eclipse in order to develop more comfortably. The first thing that we’re going to do is import all the necessary project for working with Coco2dx in Linux and Android. To do this right-click on the Project Explorer of the C/C++ perspective of Eclipse, then, click “Import…> General > Existing Projects into Workspace”. Right-click on “Browse” and choose the Cocos2dx root directory. Once we get the complete list, right-click on “Deselect All” and from the list we select the following projects only:

  • libBox2D – (proj.linux)
  • libChipmunk – (proj.linux)
  • libcocos2d – (proj.linux)
  • libcocos2dx – (android/java)
  • libCocosDenshion – (proj.linux)
  • libExtension . (proj.linux)
  • HelloCpp – (proj.linux)
  • TestCpp – (proj.android)

It seems a pretty obvious list, but took me a good while to figure out which projects I needed. But what really was a real waste of time was my ignorance about the CDT plugin. I have a 64bits Linux distribution, because of this when I compiled the projects by command line I didn’t get any errors. But when I used eclipse, all projects had problems with the dependencies. After reviewing all the compilation scripts used by the “make-all-linux-project.sh” and putting one by one all the correct dependencies to compile the projects in eclipse, I found out that every C++ project had a 64bits configuration ready to be used in eclipse, it could be selected in the project properties. I show you in these images the steps to follow, if your operating system is 64-bit based.

Main Menu Home Logo

Let’s build the projects in the  previous list order, and I’ll explain the details that we found along the process.

  • We build libBox2D without problems.
  • We build libChipmunk without problems.
  • In the libcocos2d library we found the first little issue, there is a dependency error with an static library, libxml2.a. This problem has a quick solution. In the project properties C/C++ Build > Settings. In GCC C++ Compiler > Includes and GCC C Compiler > Includes remove the following library dependence: ../../../cocos2dx/platform/third_party/linux/libxml2. And in GCC C++ Linker > Miscellaneous remove ../../../cocos2dx/platform/third_party/linux/libraries/lib64/libxml2.a. I’ve already make a pull request fixing that https://github.com/cocos2d/cocos2d-x/pull/3019.
  • In the libCocosDenshion library something strange happend, after fixing al the dependencies, when you try to build the eclipse project you get a nevereding error list like “…multiple definition of…” related with the classes SimpleAudioEngineFMOD.cpp and SimpleAudioEngineOpenAL.cpp. However, by command line it works perfectly, that’s because in the Makefile choose one file or another for build process according of the dependecies that you have already installed . I guess there’s a more elegant way to do this, but I don’t know how to make condicional linked files with the CDT plugin so I simply remove one of the files. I decided to keep SimpleAudioEngineFMOD.cpp, but if you decide to keep the SimpleAudioEngineOpenAL.cpp and eclipse fails building for some dependencies issues (that was my case in Debian) you must install:
  • In the libextension project we found a few issues too :-). At this point, I recognize that these problems are new from the last version 2.1.4. There is a new WebSocket entity that needs a non existing library “libwebsockets.h”.

    Errata (06/07/2013)
    <ERROR>And again… Why is it working perfectly building it by command line?, you know that, don’t you?, it’s because this entity is not included as source in the Makefile :-). So we’re going to remove both files WebSocket.h and WebSocket.cpp from the project explorer in eclipse.</ERROR>
    There is a way to install the “libwebsockets” library :-), and the reason why it’s working perfectly by command line is because this entity is not included as source in the Makefile. For installing this library follow the steps in my new post. I don’t explain that in this post because a think is long enough 🙂

  • But it’s not good enough!! :-), eclipse not found SimpleAudioEngine.h, that’s easy, I know the answer!, just access to the project properties, C/C++ Build > Settings and in the section GCC C++ Compiler > Includes add ../../../CocosDenshion/include. I’ve done the corresponding pull request https://github.com/cocos2d/cocos2d-x/pull/3034
  • And.. finally THE ROCK… I mean, we get to the HelloCpp project, and after build it we get the following errors:

    In the previous Cocos2dx versions I didn’t get these errors, because of this I did some research for a while. After more than an hour I realize a few things. The first one, this two elements are defined in ccFPSImages.h and ccFPSImages.c. The second one, building the project by command line it worked perfectly, of course, bacause are included in the Makefile. The last one, building the project by eclipse it didn’t work bacause the generated libcocos2d.so library didn’t cotains this two elements.
    This error was fixed adding this two files to the “Linked Resources” list in the project properties inside the “Resources” option. I didn’t found the way to do this using the eclipse interface so I edited the .project file directly adding the following lines into the <linkedResources> section. Here is the corresponding pull request: https://github.com/cocos2d/cocos2d-x/pull/3035

    Now the last attempt. Refresh, clean and build the libcocos2d project, do the same thing to the HelloCpp project, and if everything is ok, and you’re not about to surrender, you can execute the project a see a window like this:

Well, I think that’s enough for the first part of the tutorial :-). In the second one, we are going to  run the TestCpp Android project in a real device. Thanks for reading this long post.

8 thoughts on “Setup Cocos2dx environment in Linux for Android (Part 1)

  1. Hi !
    Your tutorial is really incredible. I’m newbie with android and cocos2dx as well. Today I wasted a day for fixing some easy issue (coz I’m not familiar with linux) and I really want to share here for anyone meet the same case:
    I’m try to build the project cocos2dx ver 2.2 using terminal on Ubuntu 13.10 machine. I got the error likes this:
    —————————————————————————-
    /usr/bin/ld: /home/hungton/AndroidSDk/cocos2d-x-2.2/lib/linux/debug/libextension.a(HttpClient.o): undefined reference to symbol ‘pthread_create@@GLIBC_2.2.5’
    /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
    collect2: error: ld returned 1 exit status
    make[1]: *** [bin/debug/TestCpp] Error 1
    —————————————————————————-
    I tried building the older version (2.1.4) in terminal and it’s ok. After a day of googling, I found a solution is changing the file (cocos2d-x-2.2/cocos2dx/proj.linux/cocos2dx.mk)
    LIBS = -lrt -lz -lX11 -> LIBS = -lrt -lz -lX11 -lpthread
    and it worked !

    1. Thanks for your apportation, if you found this bug in the current development branch of the project you should make a pull request with the solution 🙂

  2. Do I need to add all the framework projects as you did into Eclipse in order to create a new project and create a small project as practicing & learning. I read the article like this:
    (http://www.raywenderlich.com/33750/)
    They don’t need to add the whole project, just create the project by using create_android_project.sh script. Could you please give me an advice. Thank you in advance !

    1. Hi Hung Ton,
      You can use the create_android_project.sh. I always import all the framework project because till now I’ve always found issues with those projects in my Debian 🙂
      Thanks a lot for your comments!!

    1. Thanks! I’m about to publish more post about Cocos2dx, Continuous Integration in Android and my researches about cool web frameworks 🙂

  3. I am at a complete loss as to how to thank you sufficiently for this post. Wow. The number of hours I wasted trying to get this to work… Everything you wrote was spot-on, described what happened exactly as it happened, and described solutions that worked perfectly without a hitch–quite an achievement.

    Something I find a little alarming, though: I’m on Ubuntu 3.10 and using Cocos2d-x version 2.2.1. How have so many updates gone by with these errors still present and behaving to the letter as they did in 2.1.4?!

Leave a Reply

Your email address will not be published. Required fields are marked *