在这篇文章中,我将描述在 Ubuntu 上使用 MinGW32 工具链为 Windows 构建库和应用程序的过程。
安装wine、mingw:
sudo apt-get install wine mingw-w64
此后,您就可以为 Windows 构建 C/C++ 应用程序了:
# C
i686-w64-mingw32-gcc helloWorld.c -o helloWorld32.exe # 32-bit
x86_64-w64-mingw32-gcc helloWorld.c -o helloWorld64.exe # 64-bit
# C++
i686-w64-mingw32-g++ helloWorld.cc -o helloWorld32.exe # 32-bit
x86_64-w64-mingw32-g++ helloWorld.cc -o helloWorld64.exe # 64-bit
可以使用wine检查收集的exe。
接下来,让我们看看对 CMake 构建、CMakeLists.txt 文件的更改,将 MinGW 特定的内容添加到构建文件中:
if (MINGW32)
set(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
SET(CMAKE_RC_COMPILER i686-w64-mingw32-windres)
set(CMAKE_RANLIB i686-w64-mingw32-ranlib)
endif()
// для сборки shared dll
elseif (MINGW32)
add_library(FlameSteelEngineGameToolkit.dll SHARED ${SOURCE_FILES})
else()
// обязательно линкуем со всеми зависимостями
if (MINGW32)
target_link_libraries(
FlameSteelEngineGameToolkit.dll
-static-libgcc
-static-libstdc++
SDL2
SDL2_mixer
/home/demensdeum/Sources/cube-art-project-bootstrap/FlameSteelFramework/FlameSteelCore/FlameSteelCore.dll
/home/demensdeum/Sources/cube-art-project-bootstrap/FlameSteelFramework/FlameSteelBattleHorn/FlameSteelBattleHorn.dll
/home/demensdeum/Sources/cube-art-project-bootstrap/FlameSteelFramework/FlameSteelCommonTraits/FlameSteelCommonTraits.dll)
set_target_properties(FlameSteelEngineGameToolkit.dll PROPERTIES
PREFIX ""
SUFFIX ""
LINK_FLAGS "-Wl,--add-stdcall-alias"
POSITION_INDEPENDENT_CODE 0 # this is to avoid MinGW warning;
# MinGW generates position-independent-code for DLL by default
)
else()
收集:
cmake -DMINGW32=1 .
make
输出将是 dll 或 exe,具体取决于您要收集的内容。对于一个工作示例,您可以查看新的 Cube-Art-Project 及其库的存储库:
https://gitlab.com/demensdeum/cube-art-project
https://gitlab.com/demensdeum/FlameSteelEngineGameToolkitFSGL
https://gitlab.com/demensdeum/cube-art-project-bootstrap
来源
https://arrayfire.com/cross-compile-to-windows-from-linux/