2009-09-20 29 views
8

我正在使用C++開發iPhone應用程序的算法部分,並遇到一個奇怪的錯誤。我擁有的代碼,在Linux,Mac和iPhone設備上均可使用gcc-4.2進行編譯,而不是在Simulator上,這使得調試和測試變得非常困難。爲iPhone設備編譯但不是用於模擬器的代碼

來自嘗試編譯模擬器的錯誤消息類似於4.0.x中的已知錯誤,儘管這並不是很明確,因爲我已明確地將gcc-4.2設置爲默認編譯器。

爲了證明錯誤,我已經準備了下面的一小段代碼:

bug.cpp

#include <tr1/unordered_map> 
#include <iostream> 

/* a hash key for the visitedTrip table */ 
struct X { 
    int x; 

    X() : x(0){}; 
    X(int x) : x(x){}; 
}; 


typedef std::tr1::unordered_map<int,X> dict; 

int main() 
{ 
    dict c1; 

    X a(0); 
    X b(1); 
    X c(2); 

    c1[0] = a; 
    c1[1] = b; 
    c1[2] = c; 

    dict::const_iterator it; 

    for(it = c1.begin(); it != c1.end(); it++) 
     std::cout << it->first << std::endl; 

    return (0); 
} 

,然後試圖進行如下編譯:

編譯。 sh

#!/bin/bash 

# 
# Compiling for the simulator and compiling under gcc-4.0 return the same error message 
# 

#SUCCESS 
c++-4.2 -arch i386 bug.cpp 

#FAIL 
c++-4.0 -arch i386 bug.cpp 

#SUCCESS 
gcc-4.2 -arch i386 -c bug.cpp 

#FAIL 
gcc-4.0 -arch i386 -c bug.cpp 

#FAIL 
gcc-4.2 -arch i386 -c bug.cpp -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk 

Eventhough我使用gcc-4.2編譯爲模擬器,我收到相同的錯誤消息,如果我是下的gcc-4.0編譯,即:

bug.cpp:27: error: no matching function for call to ‘Internal::hashtable_iterator<std::pair<const int, X>, false, false>::hashtable_iterator()’ 
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:236: note: candidates are: Internal::hashtable_iterator<Value, is_const, cache>::hashtable_iterator(const Internal::hashtable_iterator<Value, true, cache>&) [with Value = std::pair<const int, X>, bool is_const = false, bool cache = false] 
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:234: note:     Internal::hashtable_iterator<Value, is_const, cache>::hashtable_iterator(Internal::hash_node<Value, cache>**) [with Value = std::pair<const int, X>, bool is_const = false, bool cache = false] 
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:232: note:     Internal::hashtable_iterator<Value, is_const, cache>::hashtable_iterator(Internal::hash_node<Value, cache>*, Internal::hash_node<Value, cache>**) [with Value = std::pair<const int, X>, bool is_const = false, bool cache = false] 
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++/4.2.1/tr1/hashtable:225: note:     Internal::hashtable_iterator<std::pair<const int, X>, false, false>::hashtable_iterator(const Internal::hashtable_iterator<std::pair<const int, X>, false, false>&) 

任何想法,爲什麼這GCC-4.0 .x錯誤悄悄進入模擬器,實際上模擬器應該使用gcc-4.2.x,這個錯誤已經修復了。

+2

+1最小的和簡單的清樣代碼 - 應該詢問代碼相關的問題 – sbk 2009-09-20 05:35:04

+0

xcode 3.1或3.2?雪豹或豹子? gcc/g ++附帶xcode或其他? – slf 2009-09-21 21:52:42

回答

4

不知道這是否是完全正確的答案,但是這可能可以解釋爲什麼您在使用4.2看到4.0的行爲:

> pwd 
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/usr/include/c++ 
> ls -l 
total 4 
drwxr-xr-x 10 root wheel 2278 10 Sep 09:32 4.0.0/ 
lrwxr-xr-x 1 root wheel  5 10 Sep 09:30 [email protected] -> 4.0.0 

看起來他們正在嘗試使用的4.0頭爲兩個版本設置,至少在雪豹與Xcode 3.2。

0

我會仔細檢查模擬器引用的庫(STL)頭。

0

有時Xcode有編譯器問題,也許你遇到類似於這裏描述的問題。

UIKit SDK Error

在這種情況下,你必須明確指定編譯器爲設備和模擬器。 我知道這沒有任何意義,但它解決了我的問題。

相關問題