ndk 編譯 c++ 兼容性問題匯總整理


轉自:http://blog.csdn.net/wenrenwang/article/details/12003671

 

1.__int64找不到符號

采用int64_t來代替:
1 #if defined(__ANDROID__)
2 typedef int64_t __int64;
3 #endif

 

2.<sys/io.h>找不到

1 android下不需要直接引用該文件,用下面的宏去掉即可
2 #if !defined(__APPLE__) && !defined(__ANDROID__)
3 #include <sys/io.h>
4 #endif

 

 

3.SO_NOSIGPIPE找不到

SO_NOSIGPIPE在mac中存在,可惜在android中不存在。請使用MSG_NOSIGNAL來代替
1 #if defined(__ANDROID__)
2 #define SO_NOSIGPIPE MSG_NOSIGNAL
3 #endif

 

4.uint64_t, int64_t, uint32_t, int32_t等類似類型找不到

請檢查你的頭文件包含,將系統的頭文件放在自已的頭文件之前。因為你自己的頭文件有可以定義了重復的類型,導致系統頭文件出錯。
 

5.S_IREAD、S_IWRITE或者__S_IREAD、__S_IWRITE找不到

請用S_IRUSR、S_IWUSR代替
 

6.pthread_cancel找不到

這個android並未實現,有一些替代方法,具體見:http://bbs.rosoo.net/thread-10289-1-1.html
 

7.getifaddrs, <ifaddr.h> 找不到

android並沒有實現。不過謝天謝地,有人已經幫我們實現了。感謝一下他:
https://github.com/kmackay/android-ifaddrs
 

8.<sys/statvfs.h>找不到

請用此來代替:
 
1 #if defined(__ANDROID__)
2 #  include <sys/vfs.h>
3 #  define statvfs statfs
4 #else
5 #  include <sys/statvfs.h>
6 #endif

 

轉自:http://www.52rd.com/Blog/Detail_RD.Blog_DamonKabo_68286.html

 

在下載編譯android源碼的過程中,將碰到的問題整理如下:

一.

frameworks/base/include/utils/KeyedVector.h:193:31: error: ‘indexOfKey’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
frameworks/base/include/utils/KeyedVector.h:193:31: note: declarations in dependent base ‘android::KeyedVector<android::String8, android::sp<AaptDir> >’ are not found by unqualified lookup
frameworks/base/include/utils/KeyedVector.h:193:31: note: use ‘this->indexOfKey’ instead
make: *** [out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/AaptAssets.o] Error 1

 

解決辦法

引起此問題的大致原因是編譯器是不識別某些nonconformance code,解決辦法是在相應位置加上-fpermissive來屏蔽編譯器對此類代碼的排斥。

frameworks/base/tools/aapt/Android.mk

Add '-fpermissive' to line 31:
LOCAL_CFLAGS += -Wno-format-y2k -fpermissive

但是繼續報錯:cc1plus: warning: unrecognized command line option "-Wno-format-y2k-fpermissive"

解決辦法

這類錯誤是找不到所需要的庫文件:cannot find -lxxxxx ,其中lxxxxx代表的是libxxxxx.so,引起的原因一般有兩個方面:

a)由於.so是編譯臨時生成的文件,如果前面的文件編譯出錯,libxxxxx.so沒有生成,就會報錯。

b)由於本機環境所造成的:  

    1.系統缺乏對應的庫文件-->下載相應的庫文件,ubuntu一般通過命令apt-get install libxxxxx-dev來安裝
    2.版本不對應----------------->同上
    3.庫文件的鏈接錯誤-------->通過find或者locate指令定位到鏈接文件,查看鏈接文件是否正確的指向了我們希望的lib,如果不是,用 ln -sf */libxxx.so.x */libxxx.so 指令修改它。
    4.庫文件路徑設置問題------>如果是庫文件路徑引發的問題,可以到/etc/ld.so.conf.d目錄下,修改其中任意一份conf文件,(可以自建conf,以方便識別)將lib所在目錄寫進去,然后在終端輸入 ldconfig 更新緩存

三.

host C++: obbtool <= frameworks/base/tools/obbtool/Main.cpp
<command-line>:0:0: error: "_FORTIFY_SOURCE" redefined [-Werror]
frameworks/base/tools/obbtool/Main.cpp:1:0: note: this is the location of the previous definition
cc1plus: all warnings being treated as errors
make: *** [out/host/linux-x86/obj/EXECUTABLES/obbtool_intermediates/Main.o] Error 1

解決辦法

原因是GCC版本太高,需要吧GCC版本修改為4.4.6

sudo apt-get install gcc-4.4
sudo apt-get install g++-4.4

在ubuntu上將默認的GCC和G++版本改為4.4使用以下命令

 

 

1.sudo rm /usr/bin/gcc 

2.sudo ln -s /usr/bin/gcc-4.4 /usr/bin/gcc

四.

target thumb C++: libmedia <= frameworks/base/media/libmedia/MediaScanner.cpp
frameworks/base/media/libmedia/MediaScanner.cpp: In function ‘bool android::fileMatchesExtension(const char*, const char*)’:
frameworks/base/media/libmedia/MediaScanner.cpp:84: error: invalid conversion from ‘const char*’ to ‘char*’
frameworks/base/media/libmedia/MediaScanner.cpp:90: error: invalid conversion from ‘const char*’ to ‘char*’
make: *** [out/debug/host/linux-x86/pr/sim/obj/SHARED_LIBRARIES/libmedia_intermediates/MediaScanner.o] Error 1

解決辦法

因為GCC4.4版本在編譯時不支持const char* 和char*的轉換,在其它的GCC版本上應該都能編譯過。具體解釋http://gcc.gnu.org/gcc-4.4/porting_to.html

frameworks/base/media/libmedia/MediaScanner.cpp:84&90:

-char*

+const char*

五.

development/simulator/app/DeviceManager.cpp:8: fatal error: wx/wxprec.h: No such file or directory
compilation terminated.
development/simulator/app/DeviceWindow.cpp:8: fatal error: wx/wxprec.h: No such file or directorymake: *** [out/host/linux-x86/obj/EXECUTABLES/simulator_intermediates/DeviceManager.o] Error 1

ubuntu13.04無法支持libwxgkt2.6-dev 所以需要安裝高版本的wx2.8

解決辦法:

apt-file search wxprec.h

sudo apt-get install wx2.8
wx-config --cflags

六.同步內部環境

. build/envsetup.sh
lunch

    Lunch menu... pick a combo:
     1. generic-eng
     2. simulator
     3. full_passion-userdebug
     4. full_crespo-userdebug

choose 2

make -j4

 

 

轉自:http://blog.csdn.net/xiaominghimi/article/details/7637530

 

本篇介紹在Cocos2dx中加入網絡通信相關代碼,然后編譯到Android時出現的一些細節和需要注意的地方總結。不多廢話了,直接進入正題;

   1.  首先介紹在Cocos2dx中使用pthread編譯時應注意:

由於NDK明確指明不支持 pthread_cancel() 函數,編譯的錯誤提示如下:

解決方案:

使用 pthread_kill()  或者 return NULL; 兩種方法進行解決。

其中的區別如下:

pthread_exit() : 可以指定返回值,以便其他線程通過 pthread_join()   函數獲取該線程的返回值;

return:  在線程函數中使用 return,會退出線程;

這里Himi推薦使用 return ,直接 return NULL;

 

 

  2. 編譯提示找不到 sockaddr_in   與 htons等定義,錯誤提示如下:

出現此類問題,主要是頭文件沒有加入,雖然在ios編譯環境不抱錯,但是編譯會出現此問題。

 解決方案:

在使用這些函數的類中,加入如下兩個頭文件:

 

[cpp]  view plain copy
 
 
  1. #include "netdb.h"  
  2. #include "netinet/in.h"  



 

3. Curl.h 頭文件找不到。注意,這里是編譯出現頭文件找不到的問題,如果你是xcode編譯找不到頭文件請參考如下博文:(這里講解的是在ndk編譯android過程中出現此類問題的解決辦法)

【C/S通信交互之Http篇】Cocos2dx(Client)使用Curl與Jetty(Server)實現手機網游Http通信框架(內含解決curl.h頭文件找不到問題)

先觀察錯誤的日志:

解決方案:

在你使用curl的類中,雖然導入了curl類,那么你肯定是include “curl/curl.h”導入的吧!OK,改變如下即可,指定到完整相對路徑即可:

 

[cpp]  view plain copy
 
 
  1. "../../libs/cocos2dx/platform/third_party/ios/curl/curl.h"  

 

 

4. 有時候編譯報錯,錯誤提示說 curl_global_init 、curl_easy_init、curl_easy_setopt等沒有定義undefined !這個問題是因為沒有加入curl關聯lib包;錯誤提示如下:

    解決方案:

修改jni下的helloworld下的.mk文件,整個路徑如下:

your project//android/jni/helloworld/Android.mk

打開整個mk文件,找到如下內容:

修改成如下內容:

其實就是添加了兩句話!但是要注意!

你仔細看添加的第一句下面一句中間的符號,之前是 “  :=  ”現在是 “+=”  一定要注意,這個問題困擾Himi好幾個小時 = =。

 OK,基本這些足夠解決問題的啦~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM