You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux Mint虚拟机编译调用Windows API的C++程序为exe的问题

解决Linux Mint下用MinGW编译Windows C++程序的问题

1. 安装完整的MinGW-w64工具链

你之前的组件安装不完整,导致windows.h找不到。需要安装对应目标架构的完整g++工具链:

  • 编译64位Windows程序:
    sudo apt install g++-mingw-w64-x86-64
    
  • 编译32位Windows程序:
    sudo apt install g++-mingw-w64-i686
    

这会自动包含所有必需的Windows头文件和库文件,解决头文件缺失问题。

2. 正确编译(解决DLL缺失问题)

编译时必须添加静态链接参数,把依赖的运行时库打包进EXE,避免Windows上找不到libgcc_s_seh-1.dll和libstdc++-6.dll:

64位程序编译命令:

x86_64-w64-mingw32-g++ -static-libgcc -static-libstdc++ hack.cpp -o output.exe

32位程序编译命令:

i686-w64-mingw32-g++ -static-libgcc -static-libstdc++ hack.cpp -o output.exe

参数说明:

  • -static-libgcc:静态链接GCC运行时库
  • -static-libstdc++:静态链接C++标准库

3. 示例代码验证

确保你的hack.cpp代码正确引用头文件:

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

int main() {
    int test_var = 123;
    std::cout << "变量地址:" << &test_var << std::endl;
    std::cout << "当前进程ID:" << GetCurrentProcessId() << std::endl;
    return 0;
}

4. 排查残留问题

  • 若仍提示windows.h缺失:先卸载旧组件再重装
    sudo apt purge gcc-mingw-w64 g++-mingw-w64
    sudo apt install g++-mingw-w64-x86-64
    
  • 链接错误:检查Windows API函数拼写,GetCurrentProcessId()属于kernel32.dll,MinGW会自动处理链接,无需额外参数。

内容的提问来源于stack exchange,提问作者3u4ia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 15:07:06