简体   繁体   中英

CMake Link Executable to shared library (Android Studio 2.2)

I created a small Test Project in the new Version of Android Studio to test the new c++ support.

I try to call a Function from a shared library. The Function is in an other .cpp File

Here is a Part of my CmakeLists.txt File:

add_library(JNI SHARED src/main/cpp/native-lib.cpp)
add_executable(testex src/main/cpp/test2.cpp)
INCLUDE_DIRECTORIES (src/main/cpp)
target_link_libraries(testex JNI)

The test2.h File:

class Test{
public:
      int side;
      intgetArea();
};

The test2.cpp File:

#include "test2.h"
int Test::getArea(){
          return side*side;
}

And a Part of my native-lib.cpp File:

JNIEXPORT jstring JNICALL
.....(JNIEnv *env,jobject instance){
Test *test = new Test();
test->getArea();
 .
 .
 .

I get these Error: error: undefined reference to 'Test::getArea()

clang++: error: linker command failed with exit code 1 (use -v to see invocation)

ninja: build stopped: subcommand failed.

I hope someone can help me :)

Android CMake support is to generate shared lib for Java code to load during the run-time; the shared lib could call other functions in other native libs ( shared or static ). Your usage model is not supported: android does not support native executable in jni framework.

It looks you are trying to test your native code. One feasible approach is to use googletest suite ( https://cmake.org/cmake/help/v3.10/module/GoogleTest.html ) for your JNI testing.

Here is the github reference about google test: https://github.com/google/googletest/blob/master/googletest/README.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM