Building Portable C++ Executables: Creating Standalone .exe Files for All Systems

To build a standalone Windows executable (.exe) file from a C++ source code that can run on systems without MinGW installed, you need to create a statically linked binary. By statically linking, you will include all the necessary libraries within the executable itself, making it independent of external dependencies like MinGW.

Here's a step-by-step guide on how to create a statically linked .exe file using MinGW:

  1. Install MinGW: If you haven't installed MinGW already, download and install it on your development machine. You can find the MinGW installer at https://osdn.net/projects/mingw/releases/.

  2. Set Up Environment Variables: Ensure that the MinGW bin directory is added to your system's PATH environment variable. This step allows you to use the MinGW tools from the command line.

  3. Write C++ Code: Create your C++ source code using a text editor or an Integrated Development Environment (IDE) like Code::Blocks, Dev-C++, or Visual Studio Code.

  4. Compile the Code: Open a command prompt or terminal, navigate to the directory containing your C++ file, and compile it using the following command:

g++ -static -o your_program.exe your_program.cpp

The -static flag instructs the compiler to statically link the necessary libraries. Replace your_program.exe with the desired name for your executable, and your_program.cpp with the actual name of your C++ file.

  1. Distribute the Executable: Now, you have a standalone Windows executable that includes all the required libraries. This .exe file can be distributed and run on systems without MinGW installed.

Note: Building statically linked executables can result in larger file sizes compared to dynamically linked ones, as the required libraries are bundled within the executable. However, it ensures that the program is self-contained and can run on systems without additional dependencies.

Keep in mind that the executable will still require the appropriate version of the Windows operating system to run correctly. If you need to build executables that work on multiple versions of Windows, you may need to consider additional compatibility settings or testing on different versions.

Also, if your code relies on other external libraries, make sure they are also statically linked or bundled with the executable to ensure complete portability.

in my case main.cpp file

#include <iostream>
#include <windows.h>

#pragma pack(push, 1)
struct MyLASTINPUTINFO {
    UINT cbSize = sizeof(MyLASTINPUTINFO);
    DWORD dwTime = 0;
};
#pragma pack(pop)

double getIdleDuration() {
    MyLASTINPUTINFO lastInputInfo;
    GetLastInputInfo(reinterpret_cast<PLASTINPUTINFO>(&lastInputInfo));
    DWORD millis = GetTickCount() - lastInputInfo.dwTime;
    return static_cast<double>(millis) / 1000.0;
}

int main() {
    double idleDuration = getIdleDuration();
    std::cout << idleDuration << std::endl;
    return 0;
}

Did you find this article valuable?

Support Mandeep Singh by becoming a sponsor. Any amount is appreciated!