Search code examples
c++mingwcross-compiling

Can't cross compile C++ files in MinGW properly


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!


Solution

  • 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.