简体   繁体   中英

Cmake, Android: call static function defined inside a pre-built library

How do I call a static function which is defined/implemented inside a pre-built library (library.so).

It is defined as following:

namespace MyNS {

        class Database {

        public:
            static void logDB();
        };
}

There are no headers provided with library.so, but the signature of function is known.

I created my header and added the above definition in it, then I included it in my source file and tried to call that function:

#include "Database.h"

JNIEXPORT jstring JNICALL Java_com_my_package_name_MyClass_stringFromJNI(JNIEnv *env, jobject instance) {

    MyNS::Database::logDB();//compilation error

    return env->NewStringUTF("test");
}

I get undefined reference to MyNS::Database::logDB()' error, probably because of missing function implementation.

I guess there should be a way to link that header to the library which implements the function ?

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)


add_library( native-lib
             SHARED
             src/cpp/my_src.cpp )

# Include libraries needed for hello-jni lib
target_link_libraries(native-lib
                      android
                      log)

 # Specifies a path to native header files.
 include_directories(src/cpp/)

#add pre-built library
add_library( library
             SHARED
             IMPORTED)


set_target_properties( # Specifies the target library.
                       library

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       /path/to/lib/library.so )

include_directories(/path/to/lib/)

You need to link the library that you imported with the one that you are building. Just put this at the end of your CMakeLists.txt:

target_link_libraries(native-lib library)

It sounds like you already have the header set up correctly.

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