Linux下用MinGW交叉编译wxWidgets Windows版HelloWorld遇CMake错误
Let's work through your issues step by step, fix the errors, and break down how cross-compilation with CMake and wxWidgets actually works.
First: Understand the Root Cause of Your Initial Errors
Your first problem (missing wx/msw/libraries.h) happened because CMake was trying to use your local Linux wxWidgets installation (/usr/include/wx-3.0) instead of the cross-compiled Windows version you installed to /usr/x86_64-w64-mingw32. The second set of errors (undefined macros, precision casts) came from missing the correct wxWidgets build configuration flags that are tailored for Windows targets.
Cross-compilation requires telling CMake explicitly:
- Which compiler toolchain to use for Windows (MinGW)
- Where to find cross-compiled libraries and headers
- To ignore your host Linux system's libraries/headers
Step 1: Create a CMake Cross-Compilation Toolchain File
Create a file named mingw-w64-x86_64.cmake in your project root. This file tells CMake to target Windows and use the MinGW toolchain:
# Set target OS to Windows set(CMAKE_SYSTEM_NAME Windows) # Specify MinGW cross-compilers set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres) # For Windows resource files # Root directory where your cross-compiled Windows libraries live set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32) # Tell CMake to prioritize cross-compiled resources over host Linux ones set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # Don't look for Windows executables on Linux set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) # Only look for libs in cross-compile root set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) # Only look for headers in cross-compile root set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) # Only look for packages in cross-compile root
Step 2: Update Your CMakeLists.txt to Use the Toolchain
Replace your existing CMakeLists.txt with this version, which properly locates your cross-compiled wxWidgets:
cmake_minimum_required(VERSION 3.16) project(wxWidgetsHelloWorld) # Set C++ standard (match what wxWidgets was compiled with) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Point CMake to our cross-compilation toolchain set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/mingw-w64-x86_64.cmake) # Find the cross-compiled wxWidgets installation # "core" and "base" are the minimal components for a HelloWorld app find_package(wxWidgets REQUIRED COMPONENTS core base) # Import wxWidgets' build configuration (defines, compiler flags, etc.) include(${wxWidgets_USE_FILE}) # Add your executable add_executable(wxWidgetsHelloWorld main.cpp) # Link against the cross-compiled wxWidgets libraries target_link_libraries(wxWidgetsHelloWorld PRIVATE ${wxWidgets_LIBRARIES})
Step 3: Configure CLion to Use the Cross-Compiler
If you're using CLion:
- Open Settings > Build, Execution, Deployment > Toolchains
- Click the
+button and select MinGW - For the compiler paths, point to:
- C Compiler:
/usr/bin/x86_64-w64-mingw32-gcc - C++ Compiler:
/usr/bin/x86_64-w64-mingw32-g++ - Windres:
/usr/bin/x86_64-w64-mingw32-windres
- C Compiler:
- Go to Settings > Build, Execution, Deployment > CMake
- Create a new profile, select your MinGW toolchain, and add
-DCMAKE_TOOLCHAIN_FILE=mingw-w64-x86_64.cmaketo the CMake options.
Step 4: Verify the Build
- From the command line (or let CLion handle it):
mkdir build && cd build cmake -DCMAKE_TOOLCHAIN_FILE=../mingw-w64-x86_64.cmake .. make - Test the resulting
.exewith Wine (if you have it installed):wine wxWidgetsHelloWorld.exe
Why This Fixes Your Errors
- Missing headers: The toolchain file forces CMake to look in
/usr/x86_64-w64-mingw32/include(where your cross-compiled wxWidgets headers live) instead of the Linux system headers. - Undefined macros:
include(${wxWidgets_USE_FILE})imports all the wxWidgets build defines (likewxUSE_SECRETSTORE) that were set when you ran./configurefor Windows. - Precision cast errors: The x86_64 MinGW compiler targets 64-bit Windows, so
wxUIntPtris correctly sized as a 64-bit type, eliminating the cast precision loss.
Key Cross-Compilation Takeaways
Cross-compiling isn't just swapping compilers—it's about isolating your build from the host system entirely:
- Use a toolchain file to define the target OS and cross-compilers.
- Ensure all libraries and headers come from the target platform's cross-compiled root directory.
- Let the library's CMake configuration handle platform-specific flags (like wxWidgets'
wxWidgets_USE_FILE).
内容的提问来源于stack exchange,提问作者Laurent

