Xcode 3.2.1 和 C++ 字符串失败!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1603300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:54:54  来源:igfitidea点击:

Xcode 3.2.1 and C++ string fails!

c++xcode

提问by Yuval Karmi

In Xcode 3.2.1 on Mac OS X Snow Leopard, I open a project under: Command Line Tool of type C++ stdc++. I have the following simple code:

在 Mac OS X Snow Leopard 上的 Xcode 3.2.1 中,我打开了一个项目:C++ stdc++ 类型的命令行工具。我有以下简单的代码:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string myvar;
        cout << "Enter something: " << endl;
    cin >> myvar;
    cout << endl << myvar << endl;
    return 0;
}

The program compiles fine, and prompts me to "Enter Something". When I type in something, and then press enter, I get the following error:

该程序编译良好,并提示我“输入某物”。当我输入内容,然后按回车键时,出现以下错误:

myproject(766) malloc: *** error for object 0x1000041c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb) 

When compiling on an earlier version of Xcode (3.1.2) on a different computer (opened the project with the 'command line utility' option, which does not exist in 3.2.1), the code runs with NO PROBLEM.

在另一台计算机上的早期版本的 Xcode (3.1.2) 上编译时(使用“命令行实用程序”选项打开项目,该选项在 3.2.1 中不存在),代码运行没有问题。

Does anybody know what's going on? Thanks, Yuval

有人知道这是怎么回事吗?谢谢,尤瓦尔

回答by user7116

As far as I can tell, I'm not experiencing this issue in Release mode for x86_64. But I am seeing the issue in Debug x86_64. If I follow the directions given by Howard in this post, I'm able to get it running in debug mode:

据我所知,我在x86_64. 但我在 Debug 中看到了这个问题x86_64。如果我按照Howard 在这篇文章中给出的说明进行操作,我就可以让它在调试模式下运行:

  1. Project -> Edit Active Target ...
  2. Click Build tab
  3. Search for "preprocessor"
  4. Delete _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1
  1. 项目 -> 编辑活动目标 ...
  2. 单击构建选项卡
  3. 搜索“预处理器”
  4. 删除 _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

Build and run, you'll notice it works. Another interesting observation is that using __gnu_debug::string(from the <debug/string>header) alone does not trigger the error.

构建并运行,你会发现它是有效的。另一个有趣的观察是,单独使用__gnu_debug::string(从<debug/string>标题)不会触发错误。

EDIT: from the horses mouth (known issues in XCode 3.2.1)

编辑:来自马口(XCode 3.2.1 中的已知问题

The default gcc 4.2 compiler is not compatible with the Standard C++ Library Debug Mode. C++ programs compiled with Xcode 3.2 may not work in the Debug configuration. To fix this, set the Compiler Version to 4.0, or edit the Debug configuration's Preprocessor Macros and remove the entries: _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

默认的 gcc 4.2 编译器与标准 C++ 库调试模式不兼容。使用 Xcode 3.2 编译的 C++ 程序可能无法在 Debug 配置中运行。要解决此问题,请将编译器版本设置为 4.0,或编辑调试配置的预处理器宏并删除条目: _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

You can do this for all projects by navigating to /Developer/Library/Xcode/Project Templates/Application/Command Line Tool/C++ Tool/C++Tool.xcodeproj/and editing project.pbxprojand deleting the lines around line 138:

您可以通过导航到/Developer/Library/Xcode/Project Templates/Application/Command Line Tool/C++ Tool/C++Tool.xcodeproj/并编辑project.pbxproj和删除第 138 行周围的行来为所有项目执行此操作:

"_GLIBCXX_DEBUG=1",
"_GLIBCXX_DEBUG_PEDANTIC=1",

回答by nightfire

An easier way of accomplishing the same thing: paste these lines at the very beginning of your program (before any #include statements):

完成同一件事的一种更简单的方法:将这些行粘贴到程序的开头(在任何 #include 语句之前):

#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
#undef _GLIBCXX_DEBUG
#undef _GLIBCXX_DEBUG_PEDANTIC

Obviously, this will only affect the current project.

显然,这只会影响当前的项目。