簡體   English   中英

JNI 的 C++ 編譯錯誤:未知類型名稱 JNIEnv jint JavaVM

[英]C++ compilation error for JNI: unknown type name JNIEnv jint JavaVM

我收到以下頭文件的編譯錯誤:

#include <jni.h>

#ifdef __cplusplus
extern "C" {
#endif

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

...

#ifdef __cplusplus
};
#endif

還有其他 JNI 引用,例如jobjectJNIEnvJavaVM等。它並沒有抱怨 <jni.h> 標頭丟失(它是,但通過添加包含路徑很容易修復)。 我檢查了頭文件,並且在該頭文件中定義了類型(還有 <jni_md.h> )。

這對我來說沒有任何意義。 有什么想法嗎?

編輯:我忘記包含以下錯誤文本。

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;
    ^

C代碼沒有任何明顯的錯誤,如果開發環境設置正確,則可以編譯。 所以可疑的區域是開發環境,它可能丟失或損壞了 JNI 頭文件。

C 編譯器提供選項-E ,只有在應用該選項時,編譯器才會運行預處理器。 分析的輸出可能包含發現頭文件的位置,包括擴展 ifdefs 等。

預處理器輸出顯示包含錯誤的jni.h文件。 解決方案是正確設置項目包含路徑,包含正確的jni.h

這確實很奇怪。

你可以看一下非常相似的代碼,在這里編譯得很好:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM