我早已忘記了C++以及如何正確使用它,但現在我必須處理一個類,並且我有點卡住了。 所以我使用cocos2dx爲ios編寫遊戲,但是我使用的是Lua綁定而不是C++。 但是...即使如此,我需要寫一些使用JNI的東西,所以...我們來了。包括來自不同目錄的頭文件的問題[不是路徑問題]
我有頭文件和CPP文件是這樣的:
DeviceInfoAndroid.h:
#include <jni.h>
#include "../libs/cocos2dx/platform/android/jni/JniHelper.h"
extern "C" {
const char* getAndroidDeviceId();
}
DeviceInfoAndroid.cpp:
#ifndef _DEVICE_INFO_ANDROID_
#define _DEVICE_INFO_ANDROID_
#include "DeviceInfoAndroid.h"
#include <string>
extern "C" {
const char *getAndroidDeviceId() {
JniMethodInfo t;
const char* retVal;
if (JniHelper::getStaticMethodInfo(t
, "org/cocos2dx/lib/Cocos2dxActivity" //org/cocos2dx/lib/Cocos2dxActivity
, "getIMEI"
, "()Ljava/lang/String;")) {
jstring jstr;
jstr = (jstring);
t.env->CallStaticObjectMethod(t.classID, t.methodID);
char *cstr;
cstr = (char *) t.env->GetStringUTFChars(jstr, 0);
std::string ret(cstr);
t.env->ReleaseStringUTFChars(jstr, cstr);
t.env->DeleteLocalRef(jstr);
retVal = ret.c_str();
}
return retVal;
}
}
#endif
我知道,這一切的#ifndef垃圾是不需要的,因爲我只在一個地方和一個地方使用它,所以不會發生循環依賴,但是我已經添加了它[它在刪除它時不會改變]。
所以..JniHelper.h文件的路徑是正確的。它看起來像這樣BTW。:
JniHelper.h
#ifndef __ANDROID_JNI_HELPER_H__
#define __ANDROID_JNI_HELPER_H__
#include <jni.h>
#include <string>
#include "CCPlatformMacros.h"
namespace cocos2d {
typedef struct JniMethodInfo_
{
JNIEnv * env;
jclass classID;
jmethodID methodID;
} JniMethodInfo;
class CC_DLL JniHelper
{
public:
static JavaVM* getJavaVM();
static void setJavaVM(JavaVM *javaVM);
static jclass getClassID(const char *className, JNIEnv *env=0);
static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode);
static bool getMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode);
static std::string jstring2string(jstring str);
private:
static JavaVM *m_psJavaVM;
};
}
#endif // __ANDROID_JNI_HELPER_H__
好了..現在..我用一個makefile來構建它[它是一個更大的構建過程的一部分,但對於這個特定的文件時,它看起來是這樣的(它位於同一目錄中的.h和.cpp文件我粘貼在這裏。
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic
LOCAL_SRC_FILES := \
AppDelegate.cpp \
DeviceInfoAndroid.cpp \
DeviceInfo.cpp \
../libs/lua/cocos2dx_support/LuaEngineImpl.cpp \
../libs/lua/cocos2dx_support/Cocos2dxLuaLoader.cpp \
../libs/lua/cocos2dx_support/LuaCocos2d.cpp \
../libs/lua/cocos2dx_support/LuaEngine.cpp \
../libs/lua/CocosDenshion_support/LuaSimpleAudioEngine.cpp \
../libs/lua/exts/luallthread/llthreads.c \
../libs/lua/exts/luasocket/auxiliar.c \
../libs/lua/exts/luasocket/buffer.c \
../libs/lua/exts/luasocket/except.c \
../libs/lua/exts/luasocket/inet.c \
../libs/lua/exts/luasocket/io.c \
../libs/lua/exts/luasocket/luasocket.c \
../libs/lua/exts/luasocket/mime.c \
../libs/lua/exts/luasocket/options.c \
../libs/lua/exts/luasocket/select.c \
../libs/lua/exts/luasocket/tcp.c \
../libs/lua/exts/luasocket/timeout.c \
../libs/lua/exts/luasocket/udp.c \
../libs/lua/exts/luasocket/unix.c \
../libs/lua/exts/luasocket/usocket.c \
../libs/lua/exts/lualoadexts.c \
../libs/lua/exts/luasocketscripts.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../libs/cocos2dx \
$(LOCAL_PATH)/../libs/cocos2dx/platform \
$(LOCAL_PATH)/../libs/cocos2dx/include \
$(LOCAL_PATH)/../libs/cocos2dx/lua_support \
$(LOCAL_PATH)/../libs/CocosDenshion/include \
$(LOCAL_PATH)/../libs/lua/lua \
$(LOCAL_PATH)/../libs/lua/tolua \
$(LOCAL_PATH)/../libs/lua/cocos2dx_support \
$(LOCAL_PATH)/../libs/lua/CocosDenshion_support \
$(LOCAL_PATH)/../libs/lua/exts \
$(LOCAL_PATH)/../libs/lua/exts/luallthread \
$(LOCAL_PATH)/../libs/lua/exts/luasocket \
$(LOCAL_PATH)/../libs/cocos2x/platform/android/jni \
LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../Android/libs/$(TARGET_ARCH_ABI)) \
-L$(call host-path, $(LOCAL_PATH)/../libs/cocos2dx/platform/third_party/android/libraries/$(TARGET_ARCH_ABI)) -lcurl \
-lcocos2d \
-lcocosdenshion \
-llua
include $(BUILD_SHARED_LIBRARY)
正如你可以看到我已經添加了路徑JniHelper.h文件到LO CAL_C_INCLUDES。 我嘗試刪除路徑,只使用「JniHelper.h」,但它會引發錯誤,指出該文件不存在。
構建過程中我得到的錯誤是:
Compile++ thumb : game_logic <= DeviceInfoAndroid.cpp
jni/../../Classes/DeviceInfoAndroid.cpp: In function 'const char* getAndroidDeviceId()':
jni/../../Classes/DeviceInfoAndroid.cpp:10: error: 'JniMethodInfo' was not declared in this scope
jni/../../Classes/DeviceInfoAndroid.cpp:10: error: expected ';' before 't'
jni/../../Classes/DeviceInfoAndroid.cpp:12: error: 'JniHelper' has not been declared
jni/../../Classes/DeviceInfoAndroid.cpp:12: error: 't' was not declared in this scope
jni/../../Classes/DeviceInfoAndroid.cpp:17: error: expected primary-expression before ')' token
make: *** [obj/local/armeabi/objs-debug/game_logic/DeviceInfoAndroid.o] Error 1
任何提示嗎? 我做錯了什麼? 我知道,爲了用C++編寫我有很多東西需要學習,但正如我所提到的...我不打算用C++編寫,它只是我需要爲每種類型的設備創建的幾行代碼必須在我的遊戲中支持。我已經完成了objective-c和C++混合以獲取iOS設備ID。
編輯: 刪除我的一些評論我設法刪除包括DeviceInfoAndroid.cpp。我已經更正了上面的代碼。對於那個很抱歉。
我根據您的更改更新了我的答案。見下文。 – Asaf 2012-01-01 11:51:41