为 iPhone 设备编译但不适用于模拟器的代码

发布于 2024-08-05 16:53:06 字数 3053 浏览 1 评论 0原文

我正在使用 C++ 开发 iPhone 应用程序的算法部分,并且遇到一个奇怪的错误。我的代码在 Linux、Mac 和 iPhone 设备上都可以用 gcc-4.2 编译得很好,只是不能在模拟器上编译,这使得调试和测试变得非常困难。

尝试编译模拟器时出现的错误消息类似于 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); 
} 

,然后尝试按如下方式编译它:

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 

尽管我使用的是 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 bug 潜入模拟器的任何想法,而实际上模拟器应该正在使用 gcc-4.2.x 来修复此错误?

I am using C++ to develop the algorithmic part of an iPhone application, and I am encountering a strange bug. 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.

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.

To demonstrate the bug, I have prepared the following small code snippet:

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高跟鞋的旋律 2024-08-12 16:53:07

不确定这是否是正确的答案,但这可能可以解释为什么您在使用 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

看起来他们正在尝试为两个版本使用 4.0 标头集,至少在 Snow Leopard 上Xcode 3.2。

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:

> 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.

想你的星星会说话 2024-08-12 16:53:07

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

I would be checking carefully the library (STL) headers that the simulator is referencing.

天煞孤星 2024-08-12 16:53:07

有时 Xcode 中会出现编译器问题,也许您遇到的问题与此处描述的问题类似。

UIKit SDK 错误

在这种情况下,您必须专门为设备和模拟器指定编译器。
我知道这没有任何意义,但它解决了我的问题。

Sometimes there are compiler issues in Xcode, maybe you have a problem analogous to the one described here.

UIKit SDK Error

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文