zoukankan      html  css  js  c++  java
  • 如何解决用CMake未定义引用`JNI_CreateJavaVM'?

    我需要从C ++运行Java,一般来说问题已经解决,但我的make系统或脚本出了问题,有一个创建JVM的C ++文件:

    #include <jni.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        JavaVMOption jvmopt[1];
        jvmopt[0].optionString = const_cast<char *>("-Djava.class.path=.");
    
        JavaVMInitArgs vmArgs;
        vmArgs.version = JNI_VERSION_1_2;
        vmArgs.nOptions = 1;
        vmArgs.options = jvmopt;
        vmArgs.ignoreUnrecognized = JNI_TRUE;
    
        // Create the JVM
        JavaVM *javaVM;
        JNIEnv *jniEnv;
        long flag = JNI_CreateJavaVM(&javaVM, (void **)
                &jniEnv, &vmArgs);
    
        return 0;
    }

    有一个CMakeLists.txt文件:

    cmake_minimum_required(VERSION 3.12)
    project(games_test_system)
    
    include_directories(/usr/java/jdk-10.0.2/include /usr/java/jdk-10.0.2/include/linux)
    link_directories(/usr/java/jdk-10.0.2/lib /usr/java/jdk-10.0.2/lib/server)
    
    set(CMAKE_CXX_STANDARD 14)
    
    add_executable(games_test_system main.cpp)

    运行它:

    /opt/clion-2018.2.2/bin/cmake/linux/bin/cmake --build /home/obabichev/CLionProjects/games-test-system/cmake-build-debug --target games_test_system -- -j 4

    得到这个错误:

    [ 50%] Linking CXX executable games_test_system
    CMakeFiles/games_test_system.dir/main.cpp.o: In function `main':
    /home/obabichev/CLionProjects/games-test-system/main.cpp:21: undefined reference to `JNI_CreateJavaVM'
    collect2: error: ld returned 1 exit status
    CMakeFiles/games_test_system.dir/build.make:83: recipe for target 'games_test_system' failed
    make[3]: *** [games_test_system] Error 1
    CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/games_test_system.dir/all' failed
    make[2]: *** [CMakeFiles/games_test_system.dir/all] Error 2
    CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/games_test_system.dir/rule' failed
    make[1]: *** [CMakeFiles/games_test_system.dir/rule] Error 2
    Makefile:118: recipe for target 'games_test_system' failed
    make: *** [games_test_system] Error 2

    回答:

    CMakeLists.txt文件中,添加

    find_package(JNI REQUIRED)
    target_link_libraries(games_test_system ${JNI_LIBRARIES})

    而不是手动选择目录也可以依赖CMake

    include_directories(${JNI_INCLUDE_DIRS}
  • 相关阅读:
    奥展项目笔记12-批量下载文件
    深度学习笔记03-梯度下降和方向传播
    深度学习笔记02-高效计算基础(python)
    深度学习笔记01-数学基础
    解决Android Studio卡在Gradle:Resolve dependecies 'app:_debugCompile'问题
    Oracle DB , 计算各个用户/schema 的磁盘占用空间
    转载:删除github上文件夹的两种方式
    Win7无法保存共享帐户密码
    怎么做网线,网线水晶头接法和线序
    QQ Protect 的删除
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/10605407.html
Copyright © 2011-2022 走看看