Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

cocos2d-x 打包 Android APK 常見錯誤速查表

編輯:關於 Android 程式設計

把 cocos2d-x 專案編譯成 Android APK 時,NDK 的報錯訊息往往很長又不太友善。以下把實作過程中遇到的錯誤依序整理成對照表,每條都附上原因與解法,遇到時直接查。

1. CCCommon.cpp 編譯失敗

[armeabi] Compile++ thumb: cocos2dx_static <= CCCommon.cpp
CCCommon.cpp:44:72: error: format not a string literal and no format
arguments [-Werror=format-security]
cc1plus.exe: some warnings being treated as errors

原因:新版 GCC 把 format-security 警告當成錯誤處理,舊版 cocos2d-x 的 CCLog 寫法觸發了它。

解法:在 jni/Application.mk 後面加上:

APP_CFLAGS += -Wno-error=format-security

2. 找不到 main.cpp

make: *** No rule to make target 'jni/FX/main.cpp',
needed by 'obj/local/armeabi/objs-debug/FX_shared/FX/main.o'. 停止。

原因:jni/Android.mk 裡的 LOCAL_SRC_PATH 定義有誤。

解法:對照 cocos2d-x 自帶 sample 專案裡的 Android.mk 寫法修正路徑。

3. 引用了 NDK 不支援的 Windows 標頭檔

jni/FX/main.h:7:21: fatal error: windows.h: No such file or directory
compilation terminated.

原因:NDK 只支援有限的 C/C++ 標準庫,windows.h 這種平台專屬標頭檔不存在。

解法:移除 main.cpp 裡對 main.h 的 include,改用跨平台寫法。

4. main.cpp 裡殘留 Windows 進入點

jni/FX/main.cpp:10:14: error: expected initializer before '_tWinMain'

原因:從 Windows 專案複製過來的進入點函數。

解法:直接照 cocos2d-x 提供的範例重寫 main.cpp。

5. 引入了不存在的標頭檔

jni/FX/main.cpp:6:29: fatal error: HelloWorldScene.h: No such file or directory

解法:把專案裡沒有的 include 拿掉,或補上對應檔案。

6. LOCAL_SRC_PATH 寫法錯誤導致多檔編譯失敗

arm-linux-androideabi-g++.exe: fatal error: cannot specify -o with -c,
-S or -E with multiple files
compilation terminated.

原因:用 *.cpp 這種萬用字元一次指定多個檔案,但 -o 只能對單一輸出檔。

解法:逐一列出檔案,或改用 LOCAL_SRC_FILES := $(call all-c-files-under, ...) 這類語法。

7. 找不到 Box2D 標頭檔

FX_Global.h:8:25: fatal error: Box2D/Box2D.h: No such file or directory

解法(三選一):移除該 include;或把 external/Box2D 複製到專案的 Classes 資料夾下;或在 Android.mk 中正確引入:

LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
$(call import-module,external/Box2D)

8. 找不到 SimpleAudioEngine

FX_Global.h:8:31: fatal error: SimpleAudioEngine.h: No such file or directory

原因:SimpleAudioEngine 位於 CocosDenshion/android 下,必須另外引入模組。

LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
$(call import-module,CocosDenshion/android)

9. build_native.sh 的 NDK_ROOT 定義格式錯誤

./build_native.sh: line 32: NDK_ROOT: command not found

原因:變數名稱與等號之間多打了空格。

NDK_ROOT=/path/to/android-ndk-r9d    # 正確,等號兩側不可有空格
NDK_ROOT = /path/to/...               # 錯誤

10. C++ 標準庫不支援的寫法

FX_StartGameScene.h:25:2: error: 'CCArray' does not name a type
FX_StartGame.cpp:63:2: error: 'pAniFrame' was not declared in this scope
FX_StartGame.cpp:69:31: error: 'sprintf_s' was not declared in this scope

解法:

  • CCArray does not name a type:加上命名空間,改寫成 cocos2d::CCArray。
  • sprintf_s was not declared:_s 系列是 MSVC 專屬的安全版本函數,NDK 不支援,改用標準的 sprintf(並自行注意緩衝區大小)。
  • was not declared in this scope:多為變數未宣告或標頭檔未引入,依錯誤位置補上即可。

11. Windows 專用的 C++ 寫法

FXSharedMethod.cpp:15:32: error: 'CP_ACP' was not declared in this scope
FXSharedMethod.cpp:15:62: error: 'MultiByteToWideChar' was not declared in this scope
FXSharedMethod.cpp:19:28: error: 'CP_UTF8' was not declared in this scope

原因:MultiByteToWideChar/WideCharToMultiByte 是 Windows API,Android 上沒有。

解法:改用跨平台的編碼轉換方式,例如 iconv 或自行實作 UTF-8 與 UTF-16 的轉換。

12. 建立專案後無法編譯(Visual Studio)

fatal error C1083: 無法開啟包括檔案: "CCStdC.h": No such file or directory
fatal error C1083: 無法開啟包括檔案: "SimpleAudioEngine.h": No such file or directory

解法:把 cocos2d-x 根目錄下的 cocos2dx 與 CocosDenshion 兩個資料夾完整複製到專案目錄下,並確認專案的 include 路徑有指向它們。

通用排查思路

把上面這些整理一下,會發現 cocos2d-x 的編譯錯誤其實只有四大類:

  1. 路徑與模組沒引入(第 2、7、8、12 條)——對照官方 sample 的 Android.mk 逐行比對,最快。
  2. 用了 Windows 專屬的 API 或標頭(第 3、4、11 條)——NDK 只支援 POSIX 與部分 C++ 標準庫,跨平台程式碼不能摻平台專屬呼叫。
  3. 編譯器版本較新、警告升級為錯誤(第 1 條)——用 APP_CFLAGS 針對特定警告放行。
  4. 語法或命名空間問題(第 10 條)——加上 cocos2d:: 前綴是最常見的修法。

遇到錯誤時,先看第一行的 fatal error 或 error:,那是根因;後面幾行通常只是連帶的編譯中止訊息。另外,把 NDK 與 cocos2d-x 的版本對齊官方支援的組合,能省掉大量這類相容性問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved