2009-09-12 17 views
8

升級到Xcode 3.2和Snow Leopard後,我的調試版本在運行時被破壞並失敗。 Stringstreams似乎不起作用。他們在發佈模式下工作。C++ Debug版本在Snow Leopard中破解了Xcode

我已經縮小到GCC 4.2,OSX SDK 10.6和_GLIBCXX_DEBUG預處理器符號的組合。這些是新Xcode項目的調試配置的默認值。

這段代碼顯示問題:

#include <iostream> 
#include <string> 
#include <sstream> 

int main (int argc, char * const argv[]) { 

    std::stringstream stream; 
    std::cout << "    expected actual" << std::endl; 
    std::cout << "stream.bad: 0   " << stream.bad() << std::endl; 
    std::cout << "stream.fail: 0   " << stream.fail() << std::endl; 
    std::cout << "stream.eof: 0   " << stream.eof() << std::endl; 
    std::cout << "stream.good: 1   " << stream.good() << std::endl; 

    stream.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit); 
    try{ 
     stream << 11;  //< Does not work as expected (see output) 
    }catch (std::bad_cast &e) { 
     std::cout << "Unexpected bad_cast: " << e.what() << std::endl; 
    }catch(std::exception &e){ 
     std::cout << "Unexpected exception: " << e.what() << std::endl; 
    } 

    std::cout << "    expected actual" << std::endl; 
    std::cout << "stream.bad: 0   " << stream.bad() << std::endl; 
    std::cout << "stream.fail: 0   " << stream.fail() << std::endl; 
    std::cout << "stream.eof: 0   " << stream.eof() << std::endl; 
    std::cout << "stream.good: 1   " << stream.good() << std::endl; 
    std::cout << std::endl; 
    std::cout << "EXPECT: " << 11 << std::endl; 
    std::cout << "ACTUAL: " << stream.str() << std::endl; 

    std::cout << std::endl << "Done" << std::endl; 
    return 0; 
} 

的字符串流插入應該工作,但使用GCC 4.2和_GLIBCXX_DEBUG時,在「< <」運營商拋出一個異常,而壞的和失敗位設置。

我已經試過了以下結果的編譯器和SDK的各種組合: - 使用GCC 4.2,LLVM-GCC或CLANG與SDK 10.6不起作用。 - 使用GCC 4.2,LLVM-GCC或CLANG與SDK 10.5一起工作。 - 將GCC 4.0與SDK 10.5或10.6一起使用。

如果_GLIBCXX_DEBUG中斷或不支持(使用SDK 10.6和GCC 4.2),那麼爲什麼這是新項目(C++命令行)中調試配置的默認值?

回答

7

此時,gcc 4.2不支持STL調試模式。您可以在STL調試模式下使用gcc 4.0,或從Debug配置中刪除調試模式預處理器宏,並繼續使用gcc 4.2。

+0

當然,因爲GCC是開源的,你可以隨時修復它自己。 – Crashworks 2009-09-13 07:06:54

+0

謝謝, 這是僅適用於Apple的GCC嗎?它適用於此版本的其他平臺。 如果不支持,那麼爲什麼它是XCode中的默認值? 蘋果是否在任何地方記錄這一點? – crmoore 2009-09-13 11:39:28

+0

@cdespinosa,你有沒有說它不支持STL調試模式的任何來源。另外,我嘗試了OSX Lion,但它仍然無法正常工作。 – highBandWidth 2011-12-15 23:34:07

0

不要忘記配置每個目標,如果你有很多(我有這個問題),因爲projet構建配置不覆蓋目標構建配置。

我真的特使終於找到解決這個問題,我使用XP VirtualMachine和Studio 2005來避免這個問題!

2

這是編譯器中已知並報告的錯誤。唯一的解決方法是:

  1. 按照您的建議刪除標誌。這是可以的,但是這些標誌有時候非常有用,並且你不想將它們從項目中移除,並且在錯誤修復後,請返回並重新更新它們!

  2. 在發佈模式下執行測試,直到您確實需要調試器符號,然後暫時刪除標誌。

我選擇了#2,這樣當修復程序出來,但項目不會丟失標誌。欲瞭解更多信息,請參閱:

Apple Discussions

BTW,代碼中,我不得不說有這個問題是如此簡單:

#include <iostream> 
#include <string> 

using namespace std; 

int main() { 
    string firstName; 
    string lastName; 
    int age; 
    char gender; 

    cout << "Enter First Name: " << endl; 
    cin >> firstName; // <----- error happens right here 

    cout << "Enter Last Name: "; 
    cin >> lastName; 

    cout << "Enter age: "; 
    cin >> age; 

    cout << "Enter gender: (m or f) "; 
    cin >> gender; 

    cout << firstName << lastName << age << gender; 

    return 0; 
}