c++ – MinGW .exe requires a few gcc dlls regardless of the code?

c++ – MinGW .exe requires a few gcc dlls regardless of the code?

Your commands are wrong !

Go to the directory where your main.cpp file is, and try the following.

g++.exe -Wall -c -g main.cpp -o objDebugmain.o
g++.exe -static -static-libgcc -static-libstdc++ -o binDebugHello World.exe objDebugmain.o

then youll no longer need to copy the DLLs (for your Hello World program).

Other notes:

The MinGW installation instructions recommends setting

c:minGW;c:MinGWbin;

to the PATH environment variable.

Normally the

-static -static-libgcc -static-libstdc++

linker options should work (try all 3 of them at once). But not for libwinpthread-1.dll.

Also, try to clean before recompiling.

Theres no -static-something command.

Only standard libraries libgcc and libstdc++ can be set to static linking.

For other libraries, you first switch to static linking with -static and then list the libraries to include with separate commands, i.e. -lpthread.

Cmake users should try adding:

set(CMAKE_CXX_STANDARD_LIBRARIES -static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CXX_STANDARD_LIBRARIES})

set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive)

-static-libgcc may be a bad idea if exceptions are used. link options documentation states that

There are several situations in which an application should use the
shared libgcc instead of the static version. The most common of these
is when the application wishes to throw and catch exceptions across
different shared libraries. In that case, each of the libraries as
well as the application itself should use the shared libgcc.

c++ – MinGW .exe requires a few gcc dlls regardless of the code?

The comments to the answer above contain the full solution, so I would like to merely add the CodeBlocks perspective. I verified it on Windows7 x64 with CodeBlocks16 and MinGW-W64 8.1.0 i686-posix-dwarf.

This solves the OPs question

  • Create new project and name it Hello World

  • accept all defaults in the wizard

  • select Project/BuildOptions/ and select Hello World. Out edits will be valid for both Debug and Release

  • add the following at Other linker option in the Linker Tab

    -static
    -static-libgcc
    -static-strc++
    -lwinpthread
    
  • On the Toolbar select Debug and press Build (the yellow gear icon)

  • Press the green run icon and confirm that the build was ok

Testing

  • open a terminal window and go to the HelloWorldbindebug folder
  • start hello world there.
  • Confirm it works without asking for any DLLs.

You could also start it from an explorer window and confirm that it also does not ask for DLLs.

Note: On my Win7x64 system when starting the HelloWorld.exe from the explorer adding the -lwinpthread line causes CodeBlocks to ignore the setting in Projects/Properties/Tab_BuildTargets/ Pause when execution ends. So the Hello World output is hardly visible because the window immediately closes after execution (mabye someone knows why)

Note that if you do not have the winpthread.dll not found problem of the OP then you likely do not use a MinGW-W64 compiler with a posix thread model. Both Code blocks MinGW-W64-bundled install packages use such versions. For CB20.03 the relevant downloads from MinGW-W64 download page would be

  • 32bit: 8.1.0 i686-posix-dwarf
  • 64bit: 8.1.0 x86_64-posix-seh

For example if I set setup compilers with Codeblocks direcly and chose the 32-bit compiler package i686-win32-dwarf, only the first 2 DLLs would go missing. In that case the fix is to set the linker options only to

  -static-libgcc
  -static-strc++

Leave a Reply

Your email address will not be published.