[英]UseJava in CMake to GENERATE_NATIVE_HEADERS
CMake seems to be really cool. CMake 似乎真的很酷。 Just the other day, I found out I can use it to compile Java sources into jars.
就在前几天,我发现我可以用它来将 Java 源代码编译成 jars。 There also is an option to GENERATE_NATIVE_HEADERS .
GENERATE_NATIVE_HEADERS还有一个选项。 However, it just does not generate headers for me.
但是,它只是不会为我生成标题。 The resulting Makefile invokes
javac
without -h someplace
.生成的 Makefile 调用
javac
而没有-h someplace
。 Any hints what I might be missing?任何提示我可能会遗漏什么? This is what I have so far:
这是我到目前为止:
cmake_minimum_required(VERSION 3.10)
find_package(Java 11 REQUIRED)
find_package(Java COMPONENTS Development)
find_package(JNI REQUIRED)
include(UseJava)
add_jar(SomeJava
SomeJava.java
ENTRY_POINT SomeJava
GENERATE_NATIVE_HEADERS SomeJava-native
)
add_library(native SHARED
native.c
)
target_include_directories(native PRIVATE
${JNI_INCLUDE_DIRS}
)
#include "SomeJava.h"
JNIEXPORT void JNICALL Java_SomeJava_printHelloWorld(JNIEnv * env, jclass cls) {
printf("Native Hello World!\n");
}
public class SomeJava {
public static void main(String[] args) {
printHelloWorld();
}
static {
System.loadLibrary("native");
}
public static native void printHelloWorld();
}
If I invoke javac
manually to generate SomeJava.h
, it works just fine.如果我手动调用
javac
来生成SomeJava.h
,它工作得很好。
Per the examples in the documentation , it appears you may want to link to your produced INTERFACE
target named SomeJava-native
:根据文档中的示例,您似乎想要链接到名为
SomeJava-native
生成的INTERFACE
目标:
The produced target for native headers can then be used to compile C/C++ sources with the target_link_libraries() command.
然后,可以使用 target_link_libraries() 命令将生成的本机头文件目标用于编译 C/C++ 源代码。
Currently, your code doesn't appear to use this target, so you can add this to the end of your CMake file:目前,您的代码似乎没有使用此目标,因此您可以将其添加到 CMake 文件的末尾:
target_link_libraries(native PRIVATE SomeJava-native)
Also, be sure your CMake version is 3.11 or greater, as the GENERATE_NATIVE_HEADERS
feature was not available in earlier versions.此外,请确保您的 CMake 版本为 3.11 或更高版本,因为
GENERATE_NATIVE_HEADERS
功能在早期版本中不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.