CMake与JNI在VisualStudio和Linux之间表现不同。

huangapple go评论52阅读模式
英文:

CMake vs JNI have different behaviors between VisualStudio and Linux

问题

我正在尝试使用CMake创建一个多平台项目(Windows+VisualStudio和Linux+gcc),并使用jni。问题是,在Linux中可以找到jni_md.h,但在MSVC中找不到。
jni_md.h位于包含jni.h的特定于平台的子目录中。

以下是我的项目的一个非常简化的提取部分:
CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
find_package(JNI 11 REQUIRED)
project(spam)
include_directories(${JNI_INCLUDE_DIRS})
add_executable(spam main.cpp)

main.cpp:

#include <jni.h>
int main() {}

在两个世界中,cmake .都可以正常工作。
在Linux世界中:
命令cmake . --build可以正常工作,在gcc的详细跟踪中,我可以看到这些包含文件:
-I /usr/lib/jvm/java-11-openjdk-amd64/include -I /usr/lib/jvm/java-11-openjdk-amd64/include/linux。(我想知道CMake如何找出它必须添加第二个路径)

但在Windows世界中情况糟糕:
在Visual Studio解决方案中,我可以看到C:/Programs/jdk-17.0.4.101-hotspot/include属性/包含目录中,但没有C:/Programs/jdk-17.0.4.101-hotspot/include/win32(其中包含jni_md.h)。
而且,毫不奇怪,命令cmake --build .失败:

  正在生成自定义规则 CMakeLists.txt
  包括
c1xx: 严重错误 C1083: 'C:/Programs/jdk-17.0.4.101-hotspot/include':没有那个文件或目录 [spam.vcxproj]
  main.cpp
C:\Programs\jdk-17.0.4.101-hotspot\include\jni.h(45): 严重错误 C1083: 'jni_md.h':没有那个文件或目录

顺便说一下,C:/Programs/jdk-17.0.4.101-hotspot/includeC:/Programs/jdk-17.0.4.101-hotspot/include/win32实际上是存在的。

如何使它在两个世界中正常工作?
提前感谢!

英文:

I'm trying to use CMake to create a multi-platform project (Windows+VisualStudio AND Linux+gcc) using jni.
The issue is that jni_md.h is well found with Linux but not in MSVC.
jni_md.h is in a platform-dependent subdirectory of the one that contains jni.h.

Here is a very minimal extract of my project :
CMakeLists.txt :

cmake_minimum_required( VERSION 3.12 )
find_package( JNI 11 REQUIRED )
project( spam )
include_directories( ${JNI_INCLUDE_DIRS} )
add_executable( spam main.cpp )

main.cpp :

#include &lt;jni.h&gt;
int main () {}

In both worlds, cmake . works well.
In Linux world :
The command cmake . --build works well, and in the verbose traces of gcc I can see these includes :
-I /usr/lib/jvm/java-11-openjdk-amd64/include -I /usr/lib/jvm/java-11-openjdk-amd64/include/linux. (I wonder how CMake find out that it has to add this second path)

But It's getting bad in windows world:
In the Visual Studio solution, I can see that C:/Programs/jdk-17.0.4.101-hotspot/include is well in the properties/include directories, but not C:/Programs/jdk-17.0.4.101-hotspot/include/win32 (which contains jni_md.h).
And, no surprise, the command cmake --build . fails:

  Building Custom Rule CMakeLists.txt
  include
c1xx : fatal error C1083: &#39;C:/Programs/jdk-17.0.4.101-hotspot/include&#39;&#160;: No such file or directory [spam.vcxproj]
  main.cpp
C:\Programs\jdk-17.0.4.101-hotspot\include\jni.h(45): fatal error C1083: &#39;jni_md.h&#39;&#160;: No such file or directory

By the way, C:/Programs/jdk-17.0.4.101-hotspot/include and C:/Programs/jdk-17.0.4.101-hotspot/include/win32 actually exists.

How to make it properly work in both worlds ?
Thanks in advance !

答案1

得分: 0

FindJNI 显然区分需要 jni_md.h 的平台和不需要的平台。在您的情况下,它得出了错误的结论。
不过,它应该已经计算出路径并将其放入 JAVA_INCLUDE_PATH2 变量中。在其他平台上,这已经合并到 JNI_INCLUDE_DIR 变量中,因此再次将其添加为包含目录是无害的。

英文:

FindJNI apparently makes a distinction between platforms that need jni_md.h and those that doesn't. In your case it arrived at the wrong conclusion.
Still, it should have calculated the path and put it in the JAVA_INCLUDE_PATH2 variable. On other platforms that is folded into the JNI_INCLUDE_DIR variable so adding it again as an include directory is harmless.

答案2

得分: 0

感谢Botje,正确的解决方案是使用变量JAVA_INCLUDE_PATH2(似乎在设置JNI_INCLUDE_DIRS时同时设置)。只需像这样编辑CMakeLists.txt

include_directories( ${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH2} )

这在两个世界中都运行良好!

英文:

Thanx to Botje, the proper solution is to use the variable JAVA_INCLUDE_PATH2 (which seems to be set at the same time that JNI_INCLUDE_DIRS).
Just edit the CMakeLists.txt like this :

include_directories( ${JNI_INCLUDE_DIRS} ${JAVA_INCLUDE_PATH2} )

It works fine in the both worlds !

huangapple
  • 本文由 发表于 2023年3月31日 17:32:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定