1

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

jay
  • 177
  • 1
  • 12
  • 3
    What error message do you get when you run the program? Is there a missing C++ runtime library? – alter_igel Aug 29 '18 at 19:10
  • "but it will not run" - what error message are you getting? Also, your compiler is a bit old - an easy to install and more up-to-date one can be got at https://nuwen.net/mingw.html –  Aug 29 '18 at 19:10
  • 4
    1) OT: "_so I assume it is the most recent version._" The release date of GCC 6.3.0 is December 21, 2016, so, no, it's far behind the recent version. 2) "Program will not run" is useless problem description. It's the same, as calling into a repair shop, while stating that your car won't run, and asking them to explain what is wrong with it. – Algirdas Preidžius Aug 29 '18 at 19:16
  • 3
    "Why does string type cause c++ program to not run?" - Using a `std::string` does not on its own cause a program to not run. You have some other problem – Jesper Juhl Aug 29 '18 at 19:18
  • Can you post what command you used to compile the program? – Galik Aug 29 '18 at 19:18
  • Btw, as of this moment, gcc 8.2 is the latest version - see https://gcc.gnu.org – Jesper Juhl Aug 29 '18 at 19:20
  • 2
    The compiler may optimize-away the `string` variable since it is not used in the program, making both examples equivalent. – Thomas Matthews Aug 29 '18 at 19:26
  • But exactly _how_ are you compiling and running the code? Post the actual commands! –  Aug 29 '18 at 19:30
  • OK, that's how you are compiling it - how are you running the executable? –  Aug 29 '18 at 19:34
  • https://stackoverflow.com/questions/41847967/during-startup-program-exited-with-code-0xc0000139 could help – Iłya Bursov Aug 29 '18 at 19:35
  • 2
    Use `g++ -S main.cpp`. Open up `main.s`. Search for "greeting". I have found that g++ 5.3.0 (on Cygwin, Windows 7), is optimizing-away the `string`. Also, the program works correctly for me; not able to reproduce the issue. – Thomas Matthews Aug 29 '18 at 19:41
  • 1
    @Thomas Why should that cause the program to fail to execute? –  Aug 29 '18 at 19:43
  • 1
    @NeilButterworth: The cause of the program failing to execute could be anything from an incorrect translation of the source code to operating system issues. In *theory*, inclusion of the `string` should not affect the operation of the program. – Thomas Matthews Aug 29 '18 at 19:46
  • 2
    @Thomas Yes, it _could_ be anything at all - I don't see how this is helpful, however. –  Aug 29 '18 at 19:52
  • "If I use a debugger, I get program exited with code 0xc0000139" - a debugger is not some magic tool that tells you where the bug is. You have to use it to inspect your program (you need to do some work). Take a look at the stack trace when it breaks your program, think about how you got there. Then run it again, but this time step through it line by line, don't just wait for it to fail, and observe the state of your program (variable values etc) as you step through it. – Jesper Juhl Aug 29 '18 at 20:20
  • @JesperJuhl That would be great advice if it got to the first line of code before exiting. As far as the stack trace part goes: _that_ is useful advice for my situation. – jay Aug 29 '18 at 20:27
  • 1
    Note that returning a value other than 0 from `main()` is generally used to signal that the program did not run successfully. – eesiraed Aug 29 '18 at 22:48

3 Answers3

0

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 e.g. redirecting to a file ./b.exe >out.txt.

ChrisD
  • 927
  • 4
  • 10
0

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

jay
  • 177
  • 1
  • 12
0

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!

  • 1
    Did you consider installing [Debian](http://debian.org/) on your laptop, with a *recent* [GCC 10](https://gcc.gnu.org/gcc-10/) on your laptop? You would invoke it as `g++ -Wall -Wextra -g` to get warnings, then you would use the [GDB](https://www.gnu.org/software/gdb/) debugger. Of course you need to read *books* about C++ (a very difficult programming language) – Basile Starynkevitch May 08 '21 at 07:50