c++windowsunixgcc

Statically compile a C++ app with GCC into a binary


How do I statically compile an app with GCC on a Ubuntu machine targeting unix? And how would I target 32-bit/64-bit machines and machines with different versions of GLIBC or whatever a unix C++ app is typically dependent on? I want to then distribute this app in binary form and run it on a unix machine without needing to compile from source.

Similarly, can I compile this app on Windows such that it will run on unix?


Solution

  • This is how you can create statically compiled 32-bit only executable, which should work on any known Linux without complaining for missing libs:

    g++ -m32 -static -o myprog myprog.cpp
    

    One downside to this is that minimum size for executable will be at least 600 KB.

    Note: if you are getting compilation errors, be sure to have package g++-multilib installed.