Mixed C/C++ in NDK?

1,103 views
Skip to first unread message

Mark Winchester

unread,
Dec 16, 2011, 10:43:59 AM12/16/11
to android-ndk
Does the NDK support mixed C/C++ projects? I've got a project that
utilizes static libraries written in both C and C++. I'm having a
hell of a time trying to get them to compile with the ndk-build
command. These are existing Unix projects, so I'm scratching my head
about why they aren't working. Any advice?

-Mark

Nalin Savara

unread,
Dec 16, 2011, 12:09:46 PM12/16/11
to andro...@googlegroups.com
Hi Mark,
1. are you sure you are doing a clean build of all your statically
linked libraries and compiling them into arm code before trying to
link them ?
2. Earlier versions of ndk used gcc compiler that supported a slightly
ancient dialect of c++ lacked stl support etc, but afaik you can avoid
problems by using latest ndk or (atleast till some time back) using
crystax's ndk build.

Regards,

Nalin

> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to
> android-ndk...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/android-ndk?hl=en.
>
>

Seabass

unread,
Dec 16, 2011, 11:05:34 AM12/16/11
to android-ndk
Yes the NDK supports C and C++, so long as you link to the proper
library. However, you can't just expect your Unix projects to compile.
The NDK is limited in many respects, for example, the STL has no
exceptions last I checked. Refer to the Documentation in the NDK
directory. It explains the limitations of the NDK.

-Seb

David Turner

unread,
Dec 19, 2011, 7:43:14 AM12/19/11
to andro...@googlegroups.com
On Fri, Dec 16, 2011 at 5:05 PM, Seabass <sebastian....@gmail.com> wrote:
Yes the NDK supports C and C++, so long as you link to the proper
library. However, you can't just expect your Unix projects to compile.
The NDK is limited in many respects, for example, the STL has no
exceptions last I checked.

Note: we provide several STLs with the NDK, and one of them fully supports exceptions and rtti.
 
Refer to the Documentation in the NDK
directory. It explains the limitations of the NDK.

-Seb

On Dec 16, 10:43 am, Mark Winchester <mwinches...@gmail.com> wrote:
> Does the NDK support mixed C/C++ projects?  I've got a project that
> utilizes static libraries written in both C and C++.  I'm having a
> hell of a time trying to get them to compile with the ndk-build
> command.  These are existing Unix projects, so I'm scratching my head
> about why they aren't working.  Any advice?
>
> -Mark

chihwah

unread,
Dec 19, 2011, 7:53:12 AM12/19/11
to andro...@googlegroups.com

Hello sir / madam,

Sharing data between 2 apps is possible, as far as I read about Android
UID and content provider.
The content provider is a light SQL database, but for sharing just a
variable or array it's kinda overkill not?

Is there not another way to share data between apps with use of UID and
a few java / jni function calls?

Greetings,

Cwli

Mark Winchester

unread,
Dec 19, 2011, 10:09:08 AM12/19/11
to android-ndk
I threw together a simple example that is having the same trouble as
the larger project. Please let me know if anything jumps out at you.

Android.mk:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cppLib
LOCAL_SRC_FILES := ./CppCode/cppFunction.cpp

include $(BUILD_STATIC_LIBRARY)

#-------------------------------------------------

include $(CLEAR_VARS)

LOCAL_MODULE := cLib
LOCAL_SRC_FILES := ./CCode/cfunction.c

include $(BUILD_STATIC_LIBRARY)

#-------------------------------------------------

include $(CLEAR_VARS)

LOCAL_MODULE := sharedLib
LOCAL_SRC_FILES := \
./SharedCode/SharedSource.cpp
LOCAL_STATIC_LIBRARIES := \
cppLib \
cLib

include $(BUILD_SHARED_LIBRARY)

./CCode/cfunction.c
#include "cfunction.h"

int cFunction(int parama, int paramb)
{
int retVal;

retVal = parama + paramb;

return(retVal);
}

./CppCode/cppFunction.cpp:
#include "cppFunction.h"

int cppFunction(int parama, int paramb)
{
int retVal;

retVal = parama - paramb;

return(retVal);
}

./SharedCode/SharedSource.cpp
#include "../CCode/cfunction.h"
#include "../CppCode/cppFunction.h"

int SharedFunction (void)
{
int cppResult, cResult;

cppResult = cppFunction(54, 38);
cResult = cFunction(3, 9);

return(cppResult - cResult);
}

When I try to build this project, I get an error that the reference to
cFunction(int, int) is undefined. What's odd, though, is that if I
rename cfunction.c to cfunction.cpp, it all compiles fine. This is
the output from the ndk-build:
Compile++ thumb : sharedLib <= SharedSource.cpp
Compile++ thumb : cppLib <= cppFunction.cpp
StaticLibrary : libcppLib.a
Compile thumb : cLib <= cfunction.c
StaticLibrary : libcLib.a
SharedLibrary : libsharedLib.so
./obj/local/armeabi/objs/sharedLib/./SharedCode/SharedSource.o: In
function `SharedFunction()':
/Android/Workspace/CExperiment/jni/./SharedCode/SharedSource.cpp:9:
undefined reference to `cFunction(int, int)'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libsharedLib.so] Error 1


Any help would be appreciated.
-Mark

David Turner

unread,
Dec 19, 2011, 10:20:33 AM12/19/11
to andro...@googlegroups.com
The problem is in no way related to the NDK, it's a basic C++ name mangling issue.

If you want to call a C function from C++, you need to ensure that its declaration in C++ uses C-linkage (usually with 'extern "C" ...")


-Mark

Mark Winchester

unread,
Dec 19, 2011, 10:57:59 AM12/19/11
to android-ndk
That got my trivial example working. I appreciate the help. Now to
see if I can apply lessons learned...

-Mark

On Dec 19, 9:20 am, David Turner <di...@android.com> wrote:
> The problem is in no way related to the NDK, it's a basic C++ name mangling
> issue.
>
> If you want to call a C function from C++, you need to ensure that its
> declaration in C++ uses C-linkage (usually with 'extern "C" ...")
>

> Seehttp://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

Reply all
Reply to author
Forward
0 new messages