1

Recently, I decided to try dabbling with C++, following my teacher's suggestion. I downloaded MinGW and started compiling some simple code with g++, but after some time any program I compiled wouldn't run, no matter how much I tried. I always got errors similar to this:

Error message in portuguese

(My computer is in portuguese, I apologize. The message above translates to "The procedure [name of procedure] could not be located in the dynamic link library [name of compiled executable]". This particular error message shows up on a header generator tool I made, which was a single .cpp file. Other programs I compiled always show "Procedure entry point" errors when executing)

I'm compiling using the command g++ -o <filename>.exe -g -Wall *.cpp (to debug and stuff), and I only have the mingw32-base-bin and mingw32-gcc-g++-bin packages installed. The only way I found for the compiled code to actually run is if I either compile the code with the command g++ <file> -static-libstdc++ -static-libgcc (which results in bloated executable sizes even with simpler programs) or if I use a tool such as Visual Studio 2019. I also tried reinstalling MinGW, but it didn't work either.

Since I'm not doing anything larger than some class exercises, I'd really like to be able to compile my codes without having to use heavy-duty tools like Visual Studio, and I'd also like to know why it is necessary to add -static-libstdc++ and -static-libgcc for it to work properly.

PS.: Another thing to note, is that compiling on my brand new laptop (that has the same MinGW configuration) worked fine. I talked with my teacher about this, and he thinks that the Microsoft C++ Redistributables (installed from things like tools and games) may be conflicting with the standard C++ library provided by MinGW. He wasn't really sure about that, though.

PS-2.: It seems extremely simple code works (things like Hello World). Anything more complex breaks apart. This is the simplest code I made where the error appeared: https://hastebin.com/aqamidoxom.cpp

Carmo
  • 95
  • 1
  • 6
  • Reinstall mingw. Possible some runtime is missing. – rifkin Nov 29 '19 at 02:04
  • Normally nothing to do with VS or Redistributables. Try to find msvcrt.dll and copy it to your source directory. – rifkin Nov 29 '19 at 02:06
  • Where exactly would the source directory be? I also tried reinstalling already, to no avail. – Carmo Nov 29 '19 at 02:11
  • The directory where your source is. Where you compile and link your source code. Make first a simple test run before "make" a complicated project. When you can't do an Hello World program then your compiler system is broken. – rifkin Nov 29 '19 at 02:13
  • A hello world program worked, but another simple program involving pointers didn't. I added the code to the main question body. Also, I added msvcrt,dll to the source directory and it still didn't work. – Carmo Nov 29 '19 at 02:20
  • You can compile it? But it does not run? – rifkin Nov 29 '19 at 02:27
  • @rifkin Indeed, I can compile it, it just gives me an error when I try to run it. – Carmo Nov 29 '19 at 02:31
  • @ Lightness Races with Monica As I know mingw uses the system c runtime from the windows install. Nothing to do with the VS install. I suspect that he has several mingw installs. – rifkin Nov 29 '19 at 02:31
  • @Carmo search for libstdc++-6.dll ("maybe not the exact filename") and copy it into your directory with the executable. – rifkin Nov 29 '19 at 02:35
  • It does actually use an old version of msvcrt, true ([ref](https://sourceforge.net/p/mingw-w64/wiki2/The%20case%20against%20msvcrt.dll/)) but that's not a redistributable and you shouldn't be copying it anywhere – Lightness Races in Orbit Nov 29 '19 at 02:35
  • @rifkin Please limit answers to the answer section. I in fact already linked to an exploration of that requirement in my answer. – Lightness Races in Orbit Nov 29 '19 at 02:35
  • @Lightness Races with Monica: I was irritated by the "Program entry" error. So I thought there some kind of program loader / libc error. – rifkin Nov 29 '19 at 02:36
  • @rifkin I only have one MinGW install on my PC, at C:\MinGW. My `PATH` variables only point to this one, too – Carmo Nov 29 '19 at 02:36

1 Answers1

2

Some standard library features are "header-only", so they are baked into your executable.

Others are more complex and live in the MinGW "runtime" DLL, which you must ship with your executable. Usually just putting it in the same directory as your executable, or putting it somewhere on your PC's %PATH, is enough.

When you statically link the standard library, you sidestep that, because now everything from it is baked into your executable.

This has absolutely nothing to do with Visual Studio or its DLLs. (You will be using mcvcrt.dll, in addition to the MinGW DLLs, but that is not to do with Visual Studio and it is always available on Windows.)

Further reading:

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • But since it is in the MinGW runtime DLL, shouldn't it run on my computer, which has MinGW set up on it? – Carmo Nov 29 '19 at 02:35
  • @Carmo "has MinGW set up on it" is an oversimplification :) There are many components to MinGW. The DLLs required to execute a program that it's built are one part of that ecosystem. I don't remember whether they are put on the PATH automatically as part of the _build_ environment - I personally would have thought yes, but it appears not. Either way, you're always going to need to distribute these DLLs with your executable anyway as not all your users will have any of MinGW installed. That, or static link them, as you've found. – Lightness Races in Orbit Nov 29 '19 at 02:36
  • @Carmo: I don't know with the normal MinGW distribution. Maybe uninstall and try this one: https://nuwen.net/mingw.html Normally it can't be a big problem here with Mingw either. Something is missing i guess. – rifkin Nov 29 '19 at 02:40
  • The only thing I'm trying to understand is why exactly the code ran normally on my laptop. The MinGW installation on it wasn't different or anything – Carmo Nov 29 '19 at 02:44
  • Also @LightnessRaceswithMonica which DLLs should I pick? All of them? – Carmo Nov 29 '19 at 02:44
  • Without full reproduction steps in both cases we cannot say. – Lightness Races in Orbit Nov 29 '19 at 02:47
  • Yes, why not all of them? Or if you don't want to do that, copy them one by one until it works... – Lightness Races in Orbit Nov 29 '19 at 02:47
  • @LightnessRaceswithMonica I wouldn't say there were many reproduction steps. I just installed it MinGW normally and added C:\MinGW and C:\MinGW\bin to the environment variables – Carmo Nov 29 '19 at 02:49
  • @Lightness Races with Monica You know that is not "normal" on a regular mingw installation to put all dlls into the directory or? He's not distribute his program. He's developing. I never encountered that problem. – rifkin Nov 29 '19 at 02:50
  • 1
    @Carmo Only `C:\MinGW`? What about the `bin` directory? Are you following [the instructions](http://www.mingw.org/wiki/HOWTO_Install_the_MinGW_GCC_Compiler_Suite)? – Lightness Races in Orbit Nov 29 '19 at 02:54
  • I did add the bin directory. I only added the `C:\MinGW` path later because someone suggested me to do so. – Carmo Nov 29 '19 at 03:01