简体   繁体   中英

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.

I am including the <string> header, and it runs fine. When I include the string data type, it will compile, but it will not run.

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
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. (Maybe not?)

I have googled and browsed stackoverflow for answers.

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

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.

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

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

Mingw has a long standing issue with certain consoles (see their faq ). It might be worth checking whether it's failing to output rather than execute by eg redirecting to a file ./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. (I really have no problem with Cygwin, I just wanted to get MinGW to work.)

I followed this site's instructions (after downloading the current source from https://gcc.gnu.org ).

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

  • I was missing some prerequisites, and had to cd into the source directory and run ./contrib/download_prerequisites from the Cygwin terminal. 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).

  • Don't forget the final make install command. 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. Then it would show no output. Maybe it was some problem with the old version (6.3.0). I deleted all the files and re-installed using the given tutorial : https://code.visualstudio.com/docs/cpp/config-mingw . This is a newer version (8.1.0). Now it works just fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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