Integrate C library with Cocos2d-x

Hi guys.

I want to integrate a speech recognition library into my project. And I found Pocketsphinx and Julius, both are speech recognition librarys written in C. So I want to know how to intergrate these librarys (or any C library) into Cocos2d-x?

Thank you in advance.

Do you have source code?

Pawel Lopusinski wrote:

Do you have source code?

You mean the source code of the library? Yes, I have. I’ve downloaded Julius from their webpage: http://julius.sourceforge.jp/en_index.php?q=index-en.html#get_current_ver (the pre-compile library). After unzip, you can found folder “include” and “lib” in there.

If you are doing this under iOS, it’s quite easy that you just have to add these source codes into your XCode project.
But if you are under Android, things are getting tricky since you have to set up some android.mk files.

Actually if he were to treat the library source as “his own” he would only need to add it to the “usual” Android.mk.

@Dat Nguyen : if you downloaded the pre-compiled version it means that you don’t have source code - you only have the headers (“include” folder) which are required for you to know what is accesible to you, they expose the API. You also have already compiled libraries which get linked to your project during linking when buildiung your project.
With that version your compiler doesn’t compile this library at all.
Fortunatelly there is also a source package avaiable - with it, you should get all library files (so headers and source files) which will be compiled with your project when you build it.

Well, It seems that I can build and run fine on iOS, but as Shuaiying Hou said, Android made me confused. Do I have to add every source file from library into the Android.mk? And how to do that, do I have to treat it (C files) as usual C++ files?

Dat Nguyen wrote:

Well, It seems that I can build and run fine on iOS, but as Shuaiying Hou said, Android made me confused. Do I have to add every source file from library into the Android.mk? And how to do that, do I have to treat it (C files) as usual C++ files?

There is a Android.mk file in your proj.android/jni folder. In the most simple situation you have to modify this file, add .h into LOCAL_C_INCLUDES, .cpp/.c into LOCAL_SRC_FILES if they exists, and your library name into LOCAL_LDLIBS like -l. But if this is a static library(.a) you have to add its name into LOCAL_WHOLE_STATIC_LIBRARIES. You may also add it in LOCAL_SHARED_LIBRARIES instead if it’s a shared library. I think you have to add .h and .c files only in your case since you already have source code instead of .a or .so files.

C files might meet some issue when compiling due to NDK version. Just have a try first.

Is there another way? Because the library contain many of .c file and It will comsume much time to add all of it.

Dat Nguyen wrote:

Is there another way? Because the library contain many of .c file and It will comsume much time to add all of it.

Try this:

SRC_FILES := $(wildcard /*.c)

LOCAL_SRC_FILES := $(SRC_FILES)

Also there was a thread some time ago about Android.mk automatic generation on this forum : http://cocos2d-x.org/boards/6/topics/28444?r=28576#message-28576
To make it also aware of .c files you have to change the command to be inserted into build_native.sh to :
<pre>find L …/Classesname .c
-print | ./mkHelper.sh > jni/Android.mk

Shuaiying Hou wrote:

Dat Nguyen wrote:
> Is there another way? Because the library contain many of .c file and It will comsume much time to add all of it.
>
Try this:
>
SRC_FILES := $(wildcard /*.c)
>
LOCAL_SRC_FILES := $(SRC_FILES)

Alright, It can build with no error now :slight_smile: So many thanks.