Search code examples
javac++eclipseandroid-ndkcocos2d-x

how to import and edit C++ code in eclipse using ndk?


Scenario: I have run and compiled an existing c++ code of cocos2d-x on android emulator using eclipse and NDK (CDT and Sequoyah are installed)(c++ code edited outside the eclipse). but, not able to edit c++ code or debug during runtime.

If I open a c++ (main.cpp) file, the project gets contaminated with errors. main.cpp is a file having java as well as c++ code in it. and already included in my project. (closing the project and then reopening it solves the errors for the time being)

Problems are:

1) how to open existing c++ files in a working android project?

2) how to tell eclipse that it's a mixed code/project of c++ as well as java. and not an error

thanks.


Solution

  • In order to tell eclipse that it's a mixed project, while in C++ perspective, go to new->Convert to C/C++ project. It's a misnomer, it makes it a mixed project.

    In order to let it find the included files, go to project preferences->C++ Generel->Paths and Symbols. There you can add the include folders for C/C++ (separately). The folders you need are

    {Android NDK install dir}/sources/cxx-stl/{the stl that you're using if you're using it}/include

    and

    {Android NDK install dir}/platforms/Android[yourAPIlevel]/arch-arm/usr/include/android.

    BTW. I was following some tutorial for this process, and they've also recommended going to C/C++ build, and doing the following:

    • uncheck "Use default build command"
    • change build command to ndk-build (we'll come back to that)
    • remove "all" from the text field next to "Build(Incremental Build)"
    • uncheck clean

    This way, you can also build from eclipse. And it's really nice because that way you can also simply press "Run" and it will compile the C++ code, put the .dll (or .so) in the right place, install, and run. One little detail though. I think this is with newer versions of android-ndk that you have to say make APP=yourappname at the android-ndk installation root. So, that ndk-build in eclipse doesn't work. In order to trigger make in the right place, you can simply use make -C PATHTOYOURNDKINSTALLATION APP=yourappname as the build command in eclipse.