简体   繁体   English

为iPhone设备而不是为模拟器编译的代码

[英]Code that compiles for the iPhone Device but not for the Simulator

I am using C++ to develop the algorithmic part of an iPhone application, and I am encountering a strange bug. 我正在使用C ++开发iPhone应用程序的算法部分,并且遇到了一个奇怪的错误。 The code that I have, compiles fine with gcc-4.2 both on Linux, on the Mac, and on the iPhone device, just not on the Simulator, which makes debugging and testing very difficult. 我拥有的代码可以在Linux,Mac和iPhone设备上使用gcc-4.2很好地编译,而不能在Simulator上编译,这使得调试和测试非常困难。

The error messages from the attempts to compile for the simulator resemble a known bug in 4.0.x, although that is not very clear why since I have explicitly set gcc-4.2 to be the default compiler. 尝试为模拟器进行编译的错误消息类似于4.0.x中的已知错误,尽管这不是很清楚,原因是因为我将gcc-4.2明确设置为默认编译器。

To demonstrate the bug, I have prepared the following small code snippet: 为了演示该错误,我准备了以下小代码段:

bug.cpp 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); 
} 

and then tried to compile it as follows: 然后尝试将其编译如下:

compile.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 I am using gcc-4.2 to compile for the simulator, I am getting the same error message as if I were compiling under gcc-4.0, namely: 尽管我正在使用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>&)

Any ideas as to why this gcc-4.0.x bug creeps into the simulator, when in fact the simulator is supposed to be using gcc-4.2.x where this bug has been fixed? 关于为什么此gcc-4.0.x错误蔓延到模拟器中的任何想法,而实际上模拟器应该在已修复此错误的情况下使用gcc-4.2.x?

Not sure if this is exactly the right answer, but this would probably explain why you're seeing the 4.0 behaviour while using 4.2: 不知道这是否是正确的答案,但这可能可以解释为什么在使用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 4.2.1@ -> 4.0.0

It looks like they're trying to use the 4.0 header set for both versions, at least on Snow Leopard with Xcode 3.2. 看来他们正在尝试为两个版本都使用4.0标头集,至少在使用Xcode 3.2的Snow Leopard上。

我将仔细检查模拟器正在引用的库(STL)标头。

Sometimes there are compiler issues in Xcode, maybe you have a problem analogous to the one described here. 有时Xcode中存在编译器问题,也许您遇到的问题与此处描述的类似。

UIKit SDK Error UIKit SDK错误

In this case you have to specify the compiler specifically for device and simulator. 在这种情况下,您必须专门为设备和模拟器指定编译器。 I know that this doesn't make any sense but it fixed my problem. 我知道这没有任何意义,但可以解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM