Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
421 views
in Technique[技术] by (71.8m points)

c++ - MinGW .exe requires a few gcc dll's regardless of the code?

When compiling with MinGW, I have to copy over certain dll files from the MinGW bin directory before the exe will run (Even when using "-static" and/or "-static-libstdc++".) How do I change that? Is there a special build of MinGW that I have to use? Ultimately I want to be able to run the program with nothing but the exe in the directory (and no windows environment variables set.) These File's are:

  • libstdc++-6.dll
  • libgcc_s_seh-1.dll
  • libwinpthread-1.dll

And here is the complete list of step's I fallow:

  1. Open Up Code::Blocks
  2. Select "File->New->Project->Console"
  3. Fill out the project settings for project "Hello World"
  4. Right click Project->Build Options...->Hello World (Root target)->Other Options
  5. Enter "-static" (or "-static-libstdc++") under the already set "-fexceptions"
  6. CTRL-F9 : Build Project (Without executing)
  7. Navigate to, in Windows Explorer, and run the built "Hello World.exe" file.
  8. Click "OK" when a message pop's up saying "Error: libstdc++-6.dll is missing from your computer."
  9. Copy "libstdc++-6.dll" from the /MinGW/bin/ directory, into the "Hello World.exe" directory.
  10. Run "Hello World.exe"
  11. Click "OK" for the message saying "Error: libgcc_s_seh-1.dll is missing from your computer."
  12. Copy "libgcc_s_seh-1.dll" into the "Hello World.exe" directory.
  13. Repeat and end up copying "libwinpthread-1.dll" over aswell.
  14. View the message

    Hello World!
    

Edit: My command line is:

g++.exe -Wall -fexceptions -static -static-libgcc -static-libstdc++ -g -static-libgcc -static-libstdc++ -L. -c "C:Users\______DesktopHello Worldmain.cpp" -o objDebugmain.o
g++.exe -o "binDebugHello World.exe" objDebugmain.o

With all the dll files mentioned above required. And, just to be safe, the code is:

// main.cpp
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 you'll no longer need to copy the DLLs (for your Hello World program).

Other notes:

The MinGW installation instructions recommends setting

c:minGW;c:MinGWin;

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.

There's 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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...