Note: This step is needed because the build script not only compiles the source code but it also copies files from cocos2d-x Resource folder into assets folder.

Once you have all files from Resource folder into assets folder you can compile your native code directly from Eclipse by click in Project->Clean. Be sure that Build automatically is selected. This will compile cocos2d library, the project library (libhelloworld.so in my case), a bunch of other files, create some directories and move the created files to them. It will take a while. See picture 7.

Picture 7 – Building the project from the second time on.

Note: if your application crashes immediately after it started check whether there are any file in the android/assets folder. If this folder is empty you probably didn’t build the native code of cocos2d-x project as explained in the section Building the cocos2d’s HelloWorld sample for Android in the post Developing with cocos2d-x for android on Linux.

2.5) Running ndk-gdb

Inside Eclipse / MOTODEV Studio for Android execute you project in debug mode. Click over its name then click in the green bug in the toolbar.

The first time it will open a window asking you how to debug the project. Chose either Android Application (if you’re using Eclipse) or Android Application using MOTODEV Studio (if you’re using MOTODEV Studio). See picture 8.

Picture 8 – First time your application is debugged

Note: I don’t know why but always you perform a Debug As or Run As command for the first time Eclipse / MOTODEV Studio will use the first emulator you’ve added to it. If the IDE complains it cannot run the application / game because API level problem open your debug configuration and chose an emulator that has API greater than or equal to API 9.

Once the application is up and running, open the terminal, go to <PATH TO COCOS2DX_ROOT>/<PROJECT NAME>/android directory and run ndk-gdb script. In my case, it is

1

/home/plicatibu/local/sdk/cocos2d/HelloWorld/android

You must see something like

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

GNU gdb6.6

Copyright(C)2006Free Software Foundation,Inc.

GDB isfree software,covered by the GNU General PublicLicense,andyou are

welcome tochange it and/ordistribute copies of it under certain conditions.

I don’t know why but sometimes with no reason I get some errors when I try to debug (it doesn’t matter whether it’s pure Java application). In this situation I do this: I stop the emulator, start it again, wait until it is up and running then I click in Debug As again.

Notes:

Whenever you stop the native debugger (gdb), application quits.

Bellow are some error messages you can come across and how to solve them.

Error:

1

2

3

ERROR:Could notextract PID of application on device/emulator.

Are you sure the application isalready started?

Consider using--start or--launch=&lt;name&gt;ifnot.

Reason: You project is NOT running in debug mode in the emulator. Run it in debug mode as show above.

It is in the last line. It’s needed because Eclipse will run this command internally and it will conflict with the script. Taking in account that gdb.setup is generated over and over again by ndk scripts we can’t modify it directly but we need to use a copy instead.

2.7) Creating a C/C++ debug configuration

The following steps will guide you in order to create a debug configuration for you native code. Once you got this configuration, you’ll be able to debug native code inside Eclipse / MOTODEV Studio for Android.

Click in the Debug symbol then click in the as show in picture 9.

Picture 9 – 1st step to create native debugging configuration

In Debug Configurations window select C/C++ Application and press New button as advised on the right pane. See picture 10.

Picture 10 – 2nd step to create debugging configuration

The window will show a bunch of options. In its bottom there is a link Select other. Click in this link (see picture 11).

Picture 11 – Link in which you must click

A windows will pop up. Mark the Use configuration specific settings checkbox and then select Standard Create Process Launcher option as shown in picture 12.

Picture 12 – Selecting the correct gdb launcher

In the Name field enter whatever text you wish. I like to name cocos<MY PROJECT NAME> to ease identification. In my case I named it cocosHelloWorld.

Click in the Browse button near Project: field in the Main tab. It will pop up a window with all open projects . Select your native project (in my case, HelloWorld) and click in the OK button.

The field C/C++ Application: in Main tab needs to be filled with full path to app_process (generated in the step 2.5). Use the browse button to navigate to <PATH TO COCOS2DX_ROOT>/<PROJECT NAME>/android directory and then select app_process.
In my case, the full path is /home/plicatibu/local/sdk/cocos2d/HelloWorld/android/obj/local/armeabi/app_process.

I like to let build configurations as active and I like to enable auto build in order to let Eclipse build the project before launch whenever it’s needed. You can play with these options as you wish and see what fits better for you. Click in the Apply button.

Note: If you try to fill C/C++ Application: before you’ve set Project: Eclipse will complain with the error message Project must first be entered before broswing for a program.

You can see how Main tab it was set by me in picture 13.

Picture 13 – Main tab is configured

Click in the Debugger tab.

In the Debugger: combobox select gdbserver. See picture 14.

Picture 14 – Selecting gdbserver in Debugger combobox

In the GDB Debugger: you need to insert the full path to gdb binary provided by Android NDK. It’s located at

Set the initial break point. Android projects doesn’t have a traditional main function. As I want start debugging my own code I will chose the first function to be called by my program.
This can vary a bit but usually you will pick a function from a file inside

You can see how Connection configuration will look like in picture 16.

Picture 16 – Connection properly con figured in the Debugger tab.

Finally we finished configuring the application in order to be able to debug our native code using Eclipse.

3) Debugging routine

Steps presented here must be repeated every time you wish to debug your application.

Set a break point int the Java source code. This break point must be put somewhere after System.loadLibrary()has been executed. It’s needed to assure that all native code libraries have been loaded.I recommend you to put a break point in the first line of onCreate() funtion in the main Activity. In my case, as I’m using the HelloWorld sample, the break point was set in the onCreation() function that belongs to ApplicationDemo activity, as showin in picture 17.

Picture 17 – Break point in Java code.

Start the application in debug mode. It will hang in the line set in the step above. Picture 18 shows application stopped at break point.

Picture 18 – Application paused at break point

Open terminal and go to

1

&lt;PATH TOCOCOS2DX_ROOT&gt;/&lt;PROJECT NAME&gt;/android

and type

1

ndk-gdb-eclipse&lt;ENTERN&gt;

In my case, I would type ndk-gdb-eclipse command in the directory

1

/home/plicatibu/local/sdk/cocos2d/HelloWorld/android

Once you hit ENTER key you should immediately come back to command prompt without any kind of message. At this point gdb server is up and running just waiting for connections.

In Eclipse, goto Debug Configurations (see picture 9 above). It will open the Debug configurations window. Select the C/C++ debug configuration we created in 2.7 (I called it cocosHelloWord) and click in the Debug button. See picture 19.

Picture 19 – Debug configurations window

After you’ve clicked in the Debug button, Eclipse will connect into gdb server. If everything went fine you should see something like in picture 20.

Picture 20 – Eclipse successfully connected into gdb server

Resume application execution by clicking in F8 key or by clicking in the resume button. the application will run and stop in the C/C++ breakpoint, as show in picture 21.

Picture 21 – We hit breakpoint in the native code

From now on you can debug your native code using Eclipse as if it were pure C/C++: you can both see and set variable values, set new break points and so on.

In picture 22 you can see that I’m inspecting local variable w with mouse over it.

Picture 22 – Inspecting local variable w

Below are some problems that you may encounter and possible solutions.

Please note that the link for the book Learning Cocos2D: A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk in this post is an affiliate link and I do earn a commission through any purchases that you make.If you do make a purchase, I appreciate it greatly! If you wish, shoot me an email or let me know on the Facebook Page so I can thank you personally.Rest assured that I do not recommend a product / service that I don’t trust and that I don’t use.

Please, try my games, play free on-line games on my site, tweet this post url and share it on facebook, google+ and other social medias.

Hello! Thanks for the great tutorial. I’ve been waiting for some great guy figure this out!
I almost successfully followed your tutorial, however, I ran into this problem on attached image at last stage. It seems that gdb message is streamed to eclipse console and the only problem is gdb cannot find appropriate source file like main.cpp. I am wondering if I have to add all the source files (cocos2d-x and game sources) into eclipse project. These files are usually under Xcode Project template and I have separate android project.
I tried to add those *.cpp files on eclipse project, but all failed. Could you give me more detail on how all the source files are integrated into eclipse project for GUI break point debugging?

Yeah I already set up the makefile for my project, and all is well except debugging. So I couldn’t find the reason why the gdb cannot find starting function in main.cpp. I checked the point you mentioned, and I found no problem on my path setup.
And I am also wondering that how you can set break point under Eclipse GUI if the project doesn’t include any source files. Do you set the break point by using gdb command line or you just set by double click on the line in Eclipse window?Anyway, I really appreciate your help. Thanks:D

I made it! The problem was gdb.setup file I copied as you instructed.
Two paths have gdb.setup
1. PROJECT_NAME/android/libs/armeabi
2. PROJECT_NAME/android/obj/local/armeabi

I don’t know why ndk or COCOS2D-x project build script respectively create each gdb.setup file on separated diectory. Anyway the gdb.setup on 1 is updated everytime I modified my Android.mk and build. But the gdb.setup on 2 is not updated since the project is created.

You instructed to copy gdb.setup from 2. That caused problem for me.
I copied and paste updated directory info from 1 into gdb2.setup.

Hi! First of all, thanks for this detailed tutorial. I really appreciate your effort. I have a question though. When I try to convert the project to C/C++ in Eclipse, I don’t see Android GCC as an option. Do you know what I’m possibly missing?

Please, help to solve the problem with
‘Program “ndk-build” is not found in PATH’ error I’ve got on step 2.4 after clean project
My $PATH is[email protected]:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/alexey/app-dev/jdk:/home/alexey/app-dev/jdk/jre/bin:/home/alexey/app-dev/android-sdk-linux/platform-tools/:/home/alexey/app-dev/android-ndk-r7/:/home/alexey/app-dev/android-ndk-r7/ndk-build:ndk-build

Line in terminal ‘ndk-build -h’ works as it should be i.e. gives instructions for using ndk-build.
What have I missed?

I think something is wrong with your PATH variable. You’ve put the following paths in it:

/home/alexey/app-dev/android-ndk-r7/ndk-build
and
ndk-build

but they are not paths to directories.

You should remove them.

The only path you need in your PATH variable should be

/home/alexey/app-dev/android-ndk-r7 (assuming that you android ndk is installed there).

Other point you should pay attention is that in this tutorial I assume your android ndk is installed in android-ndk but you’re using android-ndk-r7.

You should use a soft link named android-ndk that points to android-ndk-r7 and update your PATH to use the soft link (/home/alexey/app-dev/android-ndk) instead of real path /home/alexey/app-dev/android-ndk-r7.

Once you’ve set your PATH environment variable please log out and log in again.

I still have the same problem. Right after Clean operation in Eclipse Console it says
Cannot run program “ndk-build”: Unknown reasonError: Program “ndk-build” is not found in PATHPATH=[/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games]**** Build Finished ****

When I type ‘echo $PATH’ in terminal it says[email protected]:~$ echo $PATH/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/jdk1.6.0_30/bin:/home/alexey/app-dev/android-ndk:/home/alexey/app-dev/android-sdk:/home/alexey/app-dev/android-sdk/platform-tools:/home/alexey/app-dev/cocos2dx:/home/alexey/workspace

When an environment variable (like PATH) is updated, created or destroyed no program sees modifications until user logs off and then logs in again.

Looking to PATH value reported by eclipse and the one you’ve set seems that you’ve updated your PATH and was using eclipse without the log off / log in procedure.

Was that? If that was the case everything should work fine after the first time you’ve logged of and logged in again.

If after the log off / log in procedure you still have problems I don’t know what is happening but I’ll try to discover what is going on.

Regards.

Michael

Hi and sorry for my english.
I had the same problem as Alexey Slepov had:
“Right after Clean operation in Eclipse CDT Build Console it says
**** Build of configuration Default for project C2dxHelloWorld ****

ndk-build V=1

Cannot run program “ndk-build”: Unknown reason
Error: Program “ndk-build” is not found in PATH”

The solution was the same – use the “/home/iz/opt/andrid-ndk-r7/ndk-build” instead of ndk-build. And my PATH variable in MOTODEV Eclipse is NOT THE SAME as PATH in linux Ubuntu. This is strange.My development machine:Linux Ubuntu 32bitandroid-ndk-r7cocos2d-1.0.1-x-0.11.0jdk-6u30Motodev studio for Android 3.1.If someone knows what’s going on, tell me please.

Hi, Michael.
Could you please post your PATH here ? I’d like to see it prior to say something.

Regards.

Michael

It seems, that Eclipse uses it’s own PATH variable instead of system in MOTODEV for Android 3.1. Here is what I found:
If you go to Project->Properties->C/C++ Build->Environment you can see, that PATH already defined there and it is not the same as PATH in Linux (Ubuntu). I pressed the Edit button (to the right) and replaced the contents of this Eclipse’s PATH with the needed one. Then saved changes. Pressed Project->Build all and saw, that error is gone. ndk-build is now in PATH and it works!
I added screenshot, how this thing looked before I made changes.

Could you set your PATH on Linux and remove PATH from MOTODEV’s Project->Properties->C/C++ Build->Environment ?

I’d like to confirm whether Eclipse will find ndk-build out this way.

Thanks.

Michael Turner

I tried to remove. When I press “delete” (on the right side) it does not delete the variable PATH in MOTODEV. It sets it to default “/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games”. “undefine” button does not make sense too. Of course, you can use screenshot. (I’ve attached better one, I think)

Barney

Hi,

I have managed to get as far as step 2.5 following your tutorial. But when I run ndk-gdb I get the following error:

Using my tutorial I was able to debug on simulator but I didn’t tried to debug on real device.

I will use all information you kindly provided to make some tests and them I will either update this tutorial or create a new one explaining how to debug on real devices (of course, I will give you all credits).

Thanks a lot!

Barney

Hi Marcio,

Unofortunatley I could not solve the “Remote communication error: Bad file descriptor.” error.

I think my problem may be because I am using Samsung Galaxy S2 – if I make it work I will tell you how!

I had “No line 33 in file {any project C++ source file, except main.cpp}” error when tried to add breakpoint. After adding specified string I can add breakpoint and app stops on breakpont well. Thank you!

Regis

Thanks for this tutorial! You write “Otherwise you either don’t have Android NDK installed or it’s not in the PATH. Please, correct this before to proceed.” What does “it’s not in the PATH” mean ? What are the steps to ‘put the Android NDK in the path’? Thanks!

shall I download gawk from:
http://gnuwin32.sourceforge.net/packages/gawk.htm ??
if yes, than which download ? ("Complete package, except sources" OR "Sources")
where do I need to setup the gawk's path ?

thanks in advance :)

EDIT:

on rerunning the same code i got a different error

[email protected]_14-PC /cygdrive/c/Development/cocos2d-x12/ecli/android
$ /cygdrive/c/Development/android-ndk-r7b/ndk-gdb-eclipse
awk: fatal: can't open source file /cygdrive/c/Development/cocos2d-x12/ecli/android/C:Developmentandroid-ndk-r7b/build/awk/check-awk.awk’ for reading (No such file or directory)
ERROR: Could not run ‘/usr/bin/awk’ command. Do you have it installed properly?

I visited my C:Developmentandroid-ndk-r7b/build/awk/check-awk.awk the file exists there. which path do i need to edit ?

means gmake is not installed or path is not set (g++ and make are installed). do i need it installed ? please try these commands on your system if all of them works.

Chris

Hi, great tutorial, very clear thank you.

I have a problem, when I run ndk-gdb-eclipse (or just ndk-gdb from cygwin command line) the debugger is unable to find any debugging symbols. I have done everything to make sure it is in debug mode, the –verbose output tells me it found debuggable flag, but it will not find any symbols. Only thing I can think of is the debug symbols are being stripped before being installed, but readelf shows the full set of debug symbols present. Any idea why this might happen?

Thanks for your great work.
I tried the tutorial and everything went fine, but when I open the file “main.cpp” many errors appear in problems window, and then eclipse refuses to build or debug until i fix them, if i deleted them from problems window it builds fine, but they appear again when i start native debugging, it’s not very cool to delete them every time i want to start the debugger, debugging routine is already long…
any help appreciated.
my setup:
Mac OS X 10.7.2
cocos2d-1.0.1-x-0.12.0
MOTODEV Studio for Android
android-ndk-r7b
android-sdk-macosx: android-10

this is listing o the errors:
Description Resource Path Location Type
Symbol ‘cocos2d’ could not be resolved main.cpp /MyGameName/jni/helloworld line 10 Semantic Error
Type ‘JavaVM’ could not be resolved main.cpp /MyGameName/jni/helloworld line 15 Semantic Error
Type ‘jint’ could not be resolved main.cpp /MyGameName/jni/helloworld line 15 Semantic Error
Function ‘setJavaVM’ could not be resolved main.cpp /MyGameName/jni/helloworld line 17 Semantic Error
Symbol ‘JNI_VERSION_1_4’ could not be resolved main.cpp /MyGameName/jni/helloworld line 19 Semantic Error
Type ‘jint’ could not be resolved main.cpp /MyGameName/jni/helloworld line 22 Semantic Error
Type ‘jint’ could not be resolved main.cpp /MyGameName/jni/helloworld line 22 Semantic Error
Type ‘JNIEnv’ could not be resolved main.cpp /MyGameName/jni/helloworld line 22 Semantic Error
Type ‘jobject’ could not be resolved main.cpp /MyGameName/jni/helloworld line 22 Semantic Error
Function ‘sharedDirector’ could not be resolved main.cpp /MyGameName/jni/helloworld line 24 Semantic Error
Method ‘getOpenGLView’ could not be resolved main.cpp /MyGameName/jni/helloworld line 24 Semantic Error
Function ‘sharedOpenGLView’ could not be resolved main.cpp /MyGameName/jni/helloworld line 26 Semantic Error
Type ‘cocos2d::CCEGLView’ could not be resolved main.cpp /MyGameName/jni/helloworld line 26 Semantic Error
Method ‘setFrameWidthAndHeight’ could not be resolved main.cpp /MyGameName/jni/helloworld line 27 Semantic Error
Function ‘sharedDirector’ could not be resolved main.cpp /MyGameName/jni/helloworld line 30 Semantic Error
Method ‘setOpenGLView’ could not be resolved main.cpp /MyGameName/jni/helloworld line 30 Semantic Error
Type ‘AppDelegate’ could not be resolved main.cpp /MyGameName/jni/helloworld line 32 Semantic Error
Type ‘AppDelegate’ could not be resolved main.cpp /MyGameName/jni/helloworld line 32 Semantic Error
Function ‘sharedApplication’ could not be resolved main.cpp /MyGameName/jni/helloworld line 33 Semantic Error
Method ‘run’ could not be resolved main.cpp /MyGameName/jni/helloworld line 33 Semantic Error
Function ‘reloadAllTextures’ could not be resolved main.cpp /MyGameName/jni/helloworld line 37 Semantic Error
Function ‘sharedDirector’ could not be resolved main.cpp /MyGameName/jni/helloworld line 38 Semantic Error
Method ‘setGLDefaultValues’ could not be resolved main.cpp /MyGameName/jni/helloworld line 38 Semantic Error

It seems that either you didn’t installed native support or you haven’t converted your project to C/C++ project.

E Bustaty

Thanks for the reply.

I did both, now eclipse cleans and builds just fine, I am able to debug native code now, my problem is that these errors appears after the debugging session so I have to delete them when I want to debug again, the don’t appear after build!

Yesterday I installed cocos2d-x 1.0.1-0.12.0 , android-ndk r7c and MOTODEV 3.0 in my Ubuntu 10.10 32 bits and I had no problem.

E Bustaty

I,ve solved it…
all I needed to do is to open Preferences -> C/C++ -> Code Analysis, and uncheck everything.
I don’t know why no one suffered this, I used MotoDev Studio with default preferences. anyway, this is the solution just in case…
thank you Marcio for your effort.

for one use the eclipse 4 juno. the environment variable JAVA_HOME must be set in for example /etc/profile. other wise eclipse will not get the variable, then the auto detect path will not work, I use many time to find that, because I set environment variable in .zshrc

TechyNUT

Everything is setup properly with the help of this tutorial but now when I import my project as per your first tutorial and convert the project in to C/C++ suggested as above . I get error in my android.ml file at this line $(call import-module,tests) is not imported and something like that which does not allow me to run my application in debug mode ?

/Users/niteshpurohit/Desktop/NDK/ndk-gdb-eclipse: line 104: make: command not foundERROR: The device does not support the application’s targetted CPU ABIs! Device supports: armeabi Package supports:
any help,please