1

I'm trying to compile my C++ app for Windows, using my Linux machine. My issue occurs when I'm executing the compiled exe.

Here's a quick example of my issue.

helloworld.cpp:

#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

This code compiles and runs perfectly using g++ on Linux. But when I try compiling it with mingw: i686-w64-mingw32-g++ helloworld.cpp -o helloworld.exeit compiles, but when I try running Windows tells me libgcc_s_dw2-1.ddl is missing.

I resolved this problem using the -static-libgcc -static-libstdc++ compiler flags to static link all the needed libraries, but Windows still gives me an error: libwinpthread-1.dll is missing.

I haven't found anything useful yet, to answer my question, so does anyone know how do I correctly compile this code to Win32 using MinGW-w64? Thanks!

grialion
  • 13
  • 1
  • 3
  • If windows is missing the .dll then you haven't statically linked all of the necessary libs for cross compile deployment. [Here is a similar post with solution](https://stackoverflow.com/questions/13768515/how-to-do-static-linking-of-libwinpthread-1-dll-in-mingw) They recommend to link pthread which is what I would also try. So hopefully that works! If you are going to grow your project in complexity. I would also reccomend to look into CMake and cross compiling – Frebreeze Oct 12 '21 at 18:51

1 Answers1

1

To build a fully static file you should not only use -static-libgcc and/or -static-libstdc++, but also -static to tell the linker to include static versions of any other libraries.

Or you can just build the shared library, but then you will need to distribute any DLL the EXE depends on it with the EXE and put the DLL in the same path as the EXE (or anywhere in a location listed in the PATH environment variable).

I wrote a tool called copypedeps as part of https://github.com/brechtsanders/pedeps to copy just those dependencies.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40
  • Static linking dramatically increases the file size. I know, this comment already answered my question, but I'm curious, can you tell Windows to use it's own libs, and make my executable's size below 1MB? It's currently 1.2MB with the compiler options: `-static-libgcc -static-libstdc++ -static -Os -s`. – grialion Oct 14 '21 at 15:48
  • With shared linking you need to distribute dependeny DLLs. Static linking may make the EXE bigger because the dependencies are rolled into it, but if the linker does its job well it will not include unused stuff, so in total it may be smaller. Also building without debugging info (no `-g`) and stripping (`-s`) will further reduce size. Using compile flag `-Os` may also help to optimize for size. – Brecht Sanders Oct 14 '21 at 16:27