简体   繁体   English

为什么字符串类型会导致c++程序无法运行?

[英]Why does string type cause c++ program to not run?

I have a simple program, written in C++, on a Windows 10 machine, compiled with the MinGW g++ compiler.我有一个简单的程序,用 C++ 编写,在 Windows 10 机器上,用 MinGW g++ 编译器编译。

I am including the <string> header, and it runs fine.我包含了<string>标头,它运行良好。 When I include the string data type, it will compile, but it will not run.当我包含string数据类型时,它会编译,但不会运行。

The minimum amount of code to reproduce this is:重现这一点的最少代码是:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string greeting;
    cout << "hi" << endl;
    return 1;
}

Please note, this works fine:请注意,很好用:

#include <iostream>
#include <string>
using namespace std;

int main() {
    //string greeting;
    cout << "hi" << endl;
    return 1;
}

The version of the compiler is 6.3.0编译器版本为6.3.0
g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0

I downloaded it 3 days ago, so I assume it is the most recent version.我是 3 天前下载的,所以我认为它是最新版本。 (Maybe not?) (也许不吧?)

I have googled and browsed stackoverflow for answers.我用谷歌搜索并浏览了 stackoverflow 以获得答案。

The closest question I could find, the person gave up and changed operating systems.我能找到的最接近的问题是,该人放弃并更改了操作系统。

One other solution I found was to use Cygwin's compiler.我发现的另一种解决方案是使用 Cygwin 的编译器。 I would rather not, as I already have a seemingly otherwise fine compiler.我宁愿不这样做,因为我已经有了一个看起来不错的编译器。 It seems that MinGW tools are widely used enough that I should be able to use the compiler.看来 MinGW 工具使用得足够广泛,我应该能够使用编译器。

Any other forum/blog/etc resources have problems about converting strings or calling string methods.任何其他论坛/博客/等资源在转换字符串或调用字符串方法方面都有问题。

NOTE: The same exact code runs fine on Ubuntu 16.04, with the included compiler.注意:相同的代码在 Ubuntu 16.04 上运行良好,包含编译器。

EDITS:编辑:

While I realize that "It does not run" is not helpful, I don't know how else to describe it.虽然我意识到“它不运行”没有帮助,但我不知道如何描述它。 I run the compiled program, and it behaves the same as if I entered echo '' .我运行编译后的程序,它的行为与我输入echo ''行为相同。 There is no output, no indication that anything has happened.没有输出,没有任何迹象表明发生了任何事情。 What is the most helpful way to phrase that behavior?描述这种行为的最有帮助的方式是什么?

If I use a debugger, I get program exited with code 0xc0000139如果我使用调试器,我会program exited with code 0xc0000139

A quick google search returns results indication that it is a problem with the compiler.快速谷歌搜索返回结果表明这编译器的问题。 Same as the comments below about my compiler version...与下面关于我的编译器版本的评论相同......

To compile the program, I run g++ main.cpp -o b.exe To execute it, I run ./b.exe要编译程序,我运行g++ main.cpp -o b.exe要执行它,我运行./b.exe

Mingw has a long standing issue with certain consoles (see their faq ). Mingw对于某些游戏机存在长期存在的问题(请参阅常见问题解答)。 It might be worth checking whether it's failing to output rather than execute by eg redirecting to a file ./b.exe >out.txt . 可能值得检查它是否输出失败而不是通过例如重定向到文件./b.exe >out.txt

After some useful comments, and much frustration, I decided to try to build the most current compiler. 经过一些有用的评论和无奈之后,我决定尝试构建最新的编译器。 I caved and used Cygwin. 我陷下并使用了Cygwin。 (I really have no problem with Cygwin, I just wanted to get MinGW to work.) (我真的对Cygwin没问题,我只是想让MinGW工作。)

I followed this site's instructions (after downloading the current source from https://gcc.gnu.org ). 我按照本网站的说明进行操作(从https://gcc.gnu.org下载当前源之后)。

http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/ http://preshing.com/20141108/how-to-install-the-latest-gcc-on-windows/

Notes: 笔记:

  • This is for an older version of gcc, but I replaced the appropriate version numbers with 8.2.0 (current at this time). 这是针对较旧版本的gcc,但我将相应的版本号替换为8.2.0(当前为当前版本)。

  • I was missing some prerequisites, and had to cd into the source directory and run ./contrib/download_prerequisites from the Cygwin terminal. 我丢失了一些先决条件,而不得不cd到源代码目录并运行./contrib/download_prerequisites从Cygwin的终端。 It handled everything seamlessly, and while it took a little bit, I am now able to use string data types and run the program (successfully). 它无缝地处理了所有事情,虽然花了一点时间,但我现在能够使用string数据类型并运行程序(成功)。

  • Don't forget the final make install command. 不要忘记最终的make install命令。 I did, and it was a headache. 我做到了,这很头疼。

Lastly, thanks to all the helpful comments, and those that asked for clarification. 最后,感谢所有有用的评论以及需要澄清的评论。 When I call the mechanic, and say, "my car won't run", they ask helpful questions, and we work together to get them the relevant information they need. 当我打电话给机械师并说“我的汽车无法行驶”时,他们会提出有益的问题,我们将共同努力,为他们提供所需的相关信息。 That's what happened above, and I learned some things (and solved my issue). 上面就是这样,我学到了一些东西(并解决了我的问题)。

I had the same issue.我遇到过同样的问题。 I downloaded mingw a few days from sourceforge and everything complied fine except when I declared a string.我从 sourceforge 下载了 mingw 几天,除了我声明一个字符串外,一切都很好。 Then it would show no output.然后它不会显示任何输出。 Maybe it was some problem with the old version (6.3.0).也许是旧版本(6.3.0)有问题。 I deleted all the files and re-installed using the given tutorial : https://code.visualstudio.com/docs/cpp/config-mingw .我删除了所有文件并使用给定的教程重新安装: https : //code.visualstudio.com/docs/cpp/config-mingw This is a newer version (8.1.0).这是一个较新的版本 (8.1.0)。 Now it works just fine!现在它工作得很好!

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

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