【问题标题】:C++ compilation error for JNI: unknown type name JNIEnv jint JavaVMJNI 的 C++ 编译错误:未知类型名称 JNIEnv jint JavaVM
【发布时间】:2017-04-04 00:48:20
【问题描述】:

我收到以下头文件的编译错误:

#include <jni.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    jint x1;
    jint y1;
    jint x2;
    jint y2;
} Bounds;

...

#ifdef __cplusplus
};
#endif

还有其他的JNI引用,比如jobjectJNIEnvJavaVM等。 它并没有抱怨缺少 标头(确实如此,但通过添加包含路径很容易解决)。我已经检查了头文件,并且在该头文件中定义了类型(以及 也是)。

这对我来说没有任何意义。有什么想法吗?

编辑:我忘记包含以下错误文本。

g++ -O2 -fPIC -fpermissive -I. -I.. -I/usr/include -I/usr/local/include/libavcodec -I/usr/local/include/libavdevice -I/usr/local/include/libavformat -I/usr/local/include/libswscale -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers -DUNIX -shared -c -o Plugin.o Plugin.cpp
clang: warning: argument unused during compilation: '-shared'
In file included from Plugin.cpp:19:
In file included from Plugin.h:16:
Data.h:24:5: error: unknown type name 'jint'
    jint x1;
    ^
Data.h:25:5: error: unknown type name 'jint'
    jint y1;
    ^
Data.h:26:5: error: unknown type name 'jint'
    jint x2;
    ^
Data.h:27:5: error: unknown type name 'jint'
    jint y2;
    ^

【问题讨论】:

  • 能否附上完整的错误文本?
  • @ZabojCampula 就像我说我在查找标题时没有问题,它们被包含在内。
  • @GuyKogus:好的,所以我重复 Leon 的问题:编译器错误到底是什么?
  • 尝试使用选项-E进行编译以查看预处理器结果。也许你在那里找到了提示。可能是标题位于与您预期不同的位置或其他位置。顺便说一句,您使用clang 还是gcc?命令行包含命令g++,但警告看起来像来自clang。

标签: c++ java-native-interface g++


【解决方案1】:

C代码没有明显的错误,如果开发环境设置正确,是可以编译的。因此,可疑区域是开发环境,可能缺少或损坏的 JNI 头文件。

C 编译器提供选项-E,编译器仅在应用该选项时运行预处理器。分析的输出可能包含找到扩展 ifdefs 等头文件的位置。

预处理器输出显示包含错误的jni.h 文件。解决方案是正确设置项目包含路径,包括正确的jni.h

【讨论】:

    【解决方案2】:

    这确实很奇怪。

    你可以看看非常相似的代码,在这里编译得很好:

    https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
    

    在你的情况下只有一句话。编译代码时,不要使用

    -shared
    

    您应该在构建共享库时使用它:

    # compile the code
    g++ -O2 -fPIC -fpermissive -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers -DUNIX -c -o c/recipeNo025_HelloWorld.o c/recipeNo025_HelloWorld.cpp
    
    # make shared lib out of it
    g++ -g -shared c/recipeNo025_HelloWorld.o -o lib/libHelloWorld.$(EXT)
    

    【讨论】:

      猜你喜欢
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2015-10-20
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      相关资源
      最近更新 更多