7

When I compile this with G.C.C.:

#include <iostream>
#include <string>

int main()
{
    std::cout << std::string("\r\n");
    return 0;
}

By using the following batch:

g++ -Wall main.cc

And attempt executing the output (a.exe), then Windows crashes the initialization with this error:

Run time error

If I avoid using std::string in the C++ code it executes normally, even including <string>. Any ideas?

Note, first time testing std::string.

I run Windows 8 / 64 bits. My compiler includes this file build-info.txt:

# **************************************************************************

version : MinGW-W64-builds-4.3.0
user    : nixman
date    : 03.30.2017- 1:01:08 PM
args    : --mode=gcc-6.3.0 --buildroot=/c/mingw630 --jobs=2 --rev=2 --threads=win32 --exceptions=sjlj --arch=i686 --bin-compress

[much more here...]

# **************************************************************************

Also note that I'm used to disable and uninstall all possible anti-virus utilities (e.g., Windows Defender).

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 2
    Maybe this discussion will help: https://stackoverflow.com/questions/8904722/gcc-linker-cant-find-standard-library – Michaël Roy Jun 15 '17 at 18:09
  • @MichaëlRoy Thanks for the link, although it's not my problem since I'm already using `g++.exe` as the compiler everytime. In my real project this is similiar, I use makefiles and generate objects too. –  Jun 15 '17 at 18:14
  • 2
    You are not properly linking your runtime library. As an aside, `#include ` does not generate **any** code. It's not at all surprising, that including that header file without using it does not exhibit the error you see. – IInspectable Jun 15 '17 at 18:18
  • @IInspectable Hm, it's hard to explain, but for me it's hard to learn C++ if I can't buy books or find easy references. Every guide I find is verbose and only talks about C++ history... :v and so I end up finding wrong compiling examples. Currently they say just running the GCC C++ compiler will work; as in the question, I'm not telling the compiler to directly generate object binaries, so that I would execute the compiler again to merge these object binaries. –  Jun 15 '17 at 18:40
  • 2
    @Matheus -- You have a broken compiler installation, or you are not linking the proper runtime library with the object code you built with the compiler. You will probably see that many, if not all C++ library features will not work properly, so it isn't just `std::string` that will be an issue. – PaulMcKenzie Jun 15 '17 at 18:47
  • @PaulMcKenzie I've downloaded a pre-compiled version of GCC (not source). Maybe you're correct at this point :/. Thanks for your guys help. So I think my final decision is that I'll give up learning/writting/using C++ as it's hard to find built compilers (as I need the wildcard * feature too). –  Jun 15 '17 at 18:58
  • 2
    C++ *is* hard. Using [Visual Studio](https://www.visualstudio.com/downloads/) takes out a bit of the hassle, when targeting Windows. – IInspectable Jun 15 '17 at 19:11
  • @IInspectable Thanks for the idea, though it is heavy to get downloaded in my network. Does it have a separate/individual C++ compiler? –  Jun 15 '17 at 19:17
  • 3
    You can download the [Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools). That's a complete set of build tools without an IDE. – IInspectable Jun 15 '17 at 19:23
  • 4
    Don't give up on C++ it's a great language. For small pieces of code, online compilers like http://coliru.stacked-crooked.com/ can be useful. There's lots of great reading at https://isocpp.org/ or look for books with "Modern C++" in the title or outline. – Frank Boyne Jun 15 '17 at 20:41
  • The answer is actually that window has an an anti-virus so that it does not allow any std to enter. – Harry Jun 24 '17 at 19:32
  • @Harry I disabled Windows Defender and don't use any anti-virus bcuz of performance. So, by "window" you meant my Windows? If that's what you refer to, then that's not the case ;). –  Jun 24 '17 at 23:21

1 Answers1

1

It was hard to find a solution (zZZzzZzZzZz), but finally, it's on this answer.

g++ -Wall -D_GLIBCXX_USE_CXX11_ABI=0 main.cc
  • 2
    that would suggest that you are using a mismatched version of gcc and glibc... what this does is tells it to not use c++11 name mangling for calls to the standard library implementation in glibc... – jheriko Jun 20 '17 at 22:32
  • 1
    @jheriko Hmm, so "name mangling" is the same as converting `std::string` to `stdstring`, for example? –  Jun 20 '17 at 23:57
  • 1
    nope. its how the complier encodes the names of symbols (functions, variables, classes etc.) so that they can be used in libraries or across compilation units. gcc added a different abi for c++ 11, controlled with this macro, and then made it enabled by default at some point... https://en.wikipedia.org/wiki/Name_mangling – jheriko Jun 21 '17 at 08:58
  • It would be better to fix the broken compiler installation than to use this workaround – M.M May 19 '18 at 07:05