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

Linux下用MinGW交叉编译wxWidgets Windows版HelloWorld遇CMake错误

Fixing Cross-Compilation of wxWidgets HelloWorld with MinGW on Linux (CLion/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:

  1. Which compiler toolchain to use for Windows (MinGW)
  2. Where to find cross-compiled libraries and headers
  3. 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:

  1. Open Settings > Build, Execution, Deployment > Toolchains
  2. Click the + button and select MinGW
  3. 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
  4. Go to Settings > Build, Execution, Deployment > CMake
  5. Create a new profile, select your MinGW toolchain, and add -DCMAKE_TOOLCHAIN_FILE=mingw-w64-x86_64.cmake to the CMake options.

Step 4: Verify the Build

  1. From the command line (or let CLion handle it):
    mkdir build && cd build
    cmake -DCMAKE_TOOLCHAIN_FILE=../mingw-w64-x86_64.cmake ..
    make
    
  2. Test the resulting .exe with 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 (like wxUSE_SECRETSTORE) that were set when you ran ./configure for Windows.
  • Precision cast errors: The x86_64 MinGW compiler targets 64-bit Windows, so wxUIntPtr is 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:

  1. Use a toolchain file to define the target OS and cross-compilers.
  2. Ensure all libraries and headers come from the target platform's cross-compiled root directory.
  3. Let the library's CMake configuration handle platform-specific flags (like wxWidgets' wxWidgets_USE_FILE).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 20:12:27