【问题标题】:make errors occur while using MXE CMake for compiling an SDL program使用 MXE CMake 编译 SDL 程序时出错
【发布时间】:2021-12-25 07:32:22
【问题描述】:

调用i686-w64-mingw32.static-cmake CMakeLists.txt,然后调用make 适用于不使用任何外部库的程序,但是在尝试交叉编译 Windows 的 SDL 程序时会出现以下错误。

CMake 日志。

== Using MXE wrapper: /home/tp/ext-git/mxe/usr/bin/i686-w64-mingw32.static-cmake
     - cmake version 3.20.1
     - warnings for unused CMAKE_POLICY_DEFAULT variables can be ignored
== Using MXE toolchain: /home/tp/ext-git/mxe/usr/i686-w64-mingw32.static/share/cmake/mxe-conf.cmake
== Using MXE runresult: /home/tp/ext-git/mxe/usr/share/cmake/modules/TryRunResults.cmake
== Adding "-DCMAKE_BUILD_TYPE=Release"
loading initial cache file /home/tp/ext-git/mxe/usr/share/cmake/modules/TryRunResults.cmake
-- Configuring done
CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0111 is not set: An imported target missing its location property
  fails during generation.  Run "cmake --help-policy CMP0111" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  IMPORTED_IMPLIB not set for imported target "SDL2::SDL2" configuration
  "Release".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /home/tp/Desktop

犯错误。

CMakeFiles/untitled.dir/build.make:96: *** target pattern contains no '%'.  Stop.
make[1]: *** [CMakeFiles/Makefile2:82: CMakeFiles/untitled.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

我的CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(untitled LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(SDL2 REQUIRED)

add_executable(untitled main.cpp)
target_link_libraries(untitled SDL2::SDL2)

同样的CMakeLists.txt 文件在*nix 上工作得很好,虽然在这里失败了。怎么回事?

编辑

新的细节。

CMake 工具链文件。

# This file is part of MXE. See LICENSE.md for licensing information.

# https://cmake.org/cmake/help/latest

# Can't set `cmake_minimum_required` or `cmake_policy` in toolchain
# since toolchain is read before CMakeLists.txt
# See `target-cmake.in` for CMAKE_POLICY_DEFAULT_CMPNNNN

# Check if we are using mxe supplied version
#   - toolchain is included multiple times so set a guard in
#     environment to suppress duplicate messages
if(NOT ${CMAKE_COMMAND} STREQUAL /home/tp/ext-git/mxe/usr/x86_64-pc-linux-gnu/bin/cmake AND NOT DEFINED ENV{_MXE_CMAKE_TOOLCHAIN_INCLUDED})
    message(WARNING "
** Warning: direct use of toolchain file is deprecated
** Please use prefixed wrapper script instead:
     i686-w64-mingw32.static-cmake [options] <path-to-source>
       - uses mxe supplied cmake version 3.20.1
       - loads toolchain
       - loads common run results
       - sets various policy defaults
    ")
    set(ENV{_MXE_CMAKE_TOOLCHAIN_INCLUDED} TRUE)
endif()

# Use CACHE variables to allow user setting with `-D`
# Use CACHE FORCE in rare cases of misconfigured CMakeLists.txt
#   - e.g include(FindPkgConfig)
#     https://github.com/mxe/mxe/issues/1023
#   - projects may still set these in which case FORCE doesn't have
#     any advantage, just causes inconvenience
#     https://github.com/mxe/mxe/pull/1621#discussion_r106937505
# Use normal variables expected to be set by toolchain/system
#   - projects should test for these values and not try to override

## General configuration
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR i686 CACHE STRING "System Processor")
set(MSYS 1)
set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)
# Workaround for https://www.cmake.org/Bug/view.php?id=14075
set(CMAKE_CROSS_COMPILING ON)


## Library config
set(BUILD_SHARED_LIBS OFF CACHE BOOL "BUILD_SHARED_LIBS")
set(BUILD_STATIC_LIBS ON CACHE BOOL "BUILD_STATIC_LIBS")
set(BUILD_SHARED OFF CACHE BOOL "BUILD_SHARED")
set(BUILD_STATIC ON CACHE BOOL "BUILD_STATIC")
set(LIBTYPE STATIC)


## Paths etc.
# These MODEs shouldn't be changed by users, we only want headers/libs
# from cross-build and "never" want binaries. We do, however, want
# `*-config` scripts but there's no way to instruct cmake to do that.
#
# The best solution may be to whitelist utilities
#   https://github.com/mxe/mxe/issues/1667
# and symlink them to an additional root path, changing PROGRAM to ONLY

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# Allow user to specify list of locations to search
set(CMAKE_FIND_ROOT_PATH /home/tp/ext-git/mxe/usr/i686-w64-mingw32.static CACHE PATH "List of root paths to search on the filesystem")
set(CMAKE_PREFIX_PATH /home/tp/ext-git/mxe/usr/i686-w64-mingw32.static CACHE PATH "List of directories specifying installation prefixes to be searched")
set(CMAKE_INSTALL_PREFIX /home/tp/ext-git/mxe/usr/i686-w64-mingw32.static CACHE PATH "Installation Prefix")
# For custom mxe FindPackage scripts
set(CMAKE_MODULE_PATH "/home/tp/ext-git/mxe/usr/share/cmake/modules" ${CMAKE_MODULE_PATH})

# projects (mis)use `-isystem` to silence warnings from 3rd-party
# source (among other things). gcc6 introduces changes to search
# order which breaks this usage.
#   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70129
#   https://gitlab.kitware.com/cmake/cmake/issues/16291
#   https://gitlab.kitware.com/cmake/cmake/issues/16919
set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES /home/tp/ext-git/mxe/usr/i686-w64-mingw32.static/include)
set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES /home/tp/ext-git/mxe/usr/i686-w64-mingw32.static/include)


## Programs
set(CMAKE_C_COMPILER /home/tp/ext-git/mxe/usr/bin/i686-w64-mingw32.static-gcc)
set(CMAKE_CXX_COMPILER /home/tp/ext-git/mxe/usr/bin/i686-w64-mingw32.static-g++)
set(CMAKE_Fortran_COMPILER /home/tp/ext-git/mxe/usr/bin/i686-w64-mingw32.static-gfortran)
set(CMAKE_RC_COMPILER /home/tp/ext-git/mxe/usr/bin/i686-w64-mingw32.static-windres)
# CMAKE_RC_COMPILE_OBJECT is defined in:
#     <cmake root>/share/cmake-X.Y/Modules/Platform/Windows-windres.cmake
set(CPACK_NSIS_EXECUTABLE i686-w64-mingw32.static-makensis)

## Individual package configuration
file(GLOB mxe_cmake_files
    "/home/tp/ext-git/mxe/usr/i686-w64-mingw32.static/share/cmake/mxe-conf.d/*.cmake"
)
foreach(mxe_cmake_file ${mxe_cmake_files})
    include(${mxe_cmake_file})
endforeach()

和makefile的部分

# External object files for target untitled
untitled_EXTERNAL_OBJECTS =

untitled.exe: CMakeFiles/untitled.dir/main.obj
untitled.exe: CMakeFiles/untitled.dir/build.make
untitled.exe: SDL2::SDL2-NOTFOUND
untitled.exe: CMakeFiles/untitled.dir/linklibs.rsp
untitled.exe: CMakeFiles/untitled.dir/objects1.rsp
untitled.exe: CMakeFiles/untitled.dir/link.txt

SDL2 的行是目标模式错误的行。

【问题讨论】:

  • 既然是交叉编译,能不能把CMake工具链文件的细节包括进去?
  • target pattern contains no '%' 你能显示生成的makefile的那部分吗?
  • 添加了更多信息。
  • MXE_CCACHE_DIR := $(PWD)/.ccache 看起来合法。 make --version 打印什么?另外,回复时要加@username,否则收不到通知。
  • @HolyBlackCat make的版本是4.3

标签: c++ cmake sdl-2 mxe


【解决方案1】:

不知道这是否算作正确答案,但在我的情况下,使用链接到 here 的交叉编译环境 @HolyBlackCat 效果很好。

# Install prerequisites:
# make, wget, tar, zstd, clang, lld

# Clone
git clone https://github.com/holyblackcat/quasi-msys2
# Launch environment
quasi-msys2/env/shell.sh
# Install packages
pacmake install _gcc _SDL2

# Build your cmake-based project
win-cmake -S . -B build
make -C build

【讨论】:

  • 很高兴你喜欢它!我在一个小例子中进行了编辑,仅链接的答案是不受欢迎的。
猜你喜欢
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多