Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create native C++ library on Android?

I need to write a dynamic link library in C++ that is used by Java on Android. As I get, it should be .so library, but I don't know how to do that. I tried Cygwin, but it crashes:

$ gcc 1.cpp

/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find -luser32 collect2: ld returned 1 exit status

1.cpp:
int main(int, char**)
{
   return 0;
}

Can anyone help me with that?

P.S. I'm not good at *nix, so it should be better done under Windows

UPD: I've installed both Android NDK and Cygwin and added them to PATH environment variable

UPD2: Thanks for helping. The problem was with Cygwin itself. Reinstalling it and NDK solved the problem.

like image 791
UnknownGosu Avatar asked Jun 30 '10 07:06

UnknownGosu


People also ask

Can I program with C in Android?

You can add C and C++ code to your Android project by placing the code into a cpp directory in your project module. When you build your project, this code is compiled into a native library that Gradle can package with your app.

What C library does Android use?

libc++ LLVM's libc++ is the C++ standard library that has been used by the Android OS since Lollipop, and as of NDK r18 is the only STL available in the NDK. Note: For full details of the expected level of C++ library support for any given version, see the C++14 Status, C++17 Status, and C++20 Status pages.

What are native C C++ libraries Android?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

What is native library in Android?

On Android, it is possible to use the Mobile SDK native API from an NDK library instead of the Java API. To use the native API, you must call TasInitialize with two extra parameters: the JNI environment and the application context. Both are passed as parameters to each JNI native method.


1 Answers

For your example, use g++ instead of gcc, its a C++ program.

Look at the hello-jni sample in the NDK. You need to build your shared library using the NDK and import it into a Java Android project. In the Java code, you need to declare the native functions with 'native' and add the library using static; see snippet below.

public native String  stringFromJNI();

static {
    System.loadLibrary("hello-jni");
}
like image 56
Max Avatar answered Oct 04 '22 00:10

Max