【问题标题】:CMake Link Executable to shared library (Android Studio 2.2)CMake链接可执行文件到共享库(Android Studio 2.2)
【发布时间】:2016-09-26 11:52:37
【问题描述】:

我在新版本的 Android Studio 中创建了一个小测试项目来测试新的 c++ 支持。

我尝试从共享库中调用函数。该函数位于另一个 .cpp 文件中

这是我的 CmakeLists.txt 文件的一部分:

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)

test2.h 文件:

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

test2.cpp 文件:

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

还有我的 native-lib.cpp 文件的一部分:

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

我得到这些错误:错误: 未定义对 'Test::getArea()

的引用

clang++:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

ninja:构建停止:子命令失败。

我希望有人可以帮助我:)

【问题讨论】:

  • 您应该考虑考虑库等的工作方式。您的 test2.cpp 在您的可执行文件中编译。那你就不能在图书馆里使用它了。

标签: android c++ android-ndk cmake


【解决方案1】:

Android CMake 支持是为 Java 代码生成共享库以在运行时加载;共享库可以调用其他本机库(共享或静态)中的其他函数。不支持您的使用模式:android 不支持 jni 框架中的原生可执行文件。

【讨论】:

    【解决方案2】:

    您似乎正在尝试测试您的本机代码。一种可行的方法是使用 googletest 套件 (https://cmake.org/cmake/help/v3.10/module/GoogleTest.html) 进行 JNI 测试。

    这里是关于google test的github参考:https://github.com/google/googletest/blob/master/googletest/README.md

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2010-12-07
      相关资源
      最近更新 更多