我一直在努力理解为什么CMake不再编译我的程序。我制作了一个容器,用于交叉编译Windows的C++程序。环境:
- Fedora 31
cmake
3.17.4x86_64-w64-mingw32-g++
9.2.1boost
1.69
以下代码是我当前用于mingw
:的cmake
工具链文件
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PFX x86_64-w64-mingw32)
set(Boost_ARCHITECTURE -x64)
set(Boost_DEBUG ON)
set(Boost_INCLUDE_DIR /usr/${TOOLCHAIN_PFX}/sys-root/mingw/include)
set(Boost_LIBRARY_DIR /usr/${TOOLCHAIN_PFX}/sys-root/mingw/lib)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.69 REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include -L/usr/local/lib")
set(CMAKE_C_COMPILER ${TOOLCHAIN_PFX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PFX}-g++)
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PFX})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
链接可执行文件时,构建过程会中断。它产生:
/usr/lib/gcc/x86_64-w64-mingw32/9.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_thread-mt
/usr/lib/gcc/x86_64-w64-mingw32/9.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_log-mt
/usr/lib/gcc/x86_64-w64-mingw32/9.2.1/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lboost_system
最让我震惊的是,它过去是构建的(没有对环境进行任何更改(,boost
是从RPM安装的。有趣的是,如果我省略了-lboost_thread-mt
,它当然会抱怨。
您正在覆盖许多应该由find_package()
正确设置的变量,例如Boost_LIBRARY_DIR
。
此外,您不使用find_package指定任何组件,这将是标准的方式,除非您只想在boost中使用头部分。
因此,您正在以不受支持/未指定的方式使用find_package-boost,这些方式在过去可能偶尔会起作用,但后来会失效。
在Fedora 36上肯定有效的是以下内容:
cmake_minimum_required(VERSION 3.0...3.22.1)
project(some_project CXX)
# work-around: FindBoost: Boost_ARCHITECTURE not detected on MinGW
# https://gitlab.kitware.com/cmake/cmake/-/issues/20587
if(MINGW)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(Boost_ARCHITECTURE "-x64")
else()
set(Boost_ARCHITECTURE "-x32")
endif()
endif()
# works with/without
set(Boost_USE_STATIC_LIBS ON)
# set(Boost_DEBUG ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.54
REQUIRED COMPONENTS
system
filesystem) # just an example, add what you need
add_executable(somebinary main.cc sometranslationunit.cc)
target_link_libraries(somebinary ${Boost_LIBRARIES})
构建方式:
mkdir build-cross
cd build-cross
mingw64-cmake -DCMAKE_BUILD_TYPE=Debug .. # or whatever build type
mingw64-make somebinary