【发布时间】:2026-02-21 21:00:01
【问题描述】:
我正在尝试使用 SFML 编译一个简单的程序(基本的 helloworld,但带有一个大绿色圆圈)。我应该在 Linux 上为 Linux 和 Windows 构建程序。当我为 Linux 编译时,程序可以完美编译并运行,但是当我尝试编译 Windows 可执行文件时,我收到以下错误:
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-graphics
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-window
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lsfml-system
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/BrickShooter.dir/build.make:215: BrickShooter.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/BrickShooter.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
我尝试更改我的 Cmake 中的设置但没有成功(add_library、include_directories),但到目前为止没有任何效果。 这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(BrickShooter)
set(CMAKE_CXX_STANDARD 14)
add_executable(BrickShooter main.cpp GameObjects/Block.cpp GameObjects/Block.h GameObjects/Player.cpp GameObjects/Player.h GameObjects/Shoot.cpp GameObjects/Shoot.h SysObjects/Game.cpp SysObjects/Game.h SysObjects/Store.cpp SysObjects/Store.h SysObjects/Menu.cpp SysObjects/Menu.h SysObjects/Backup.cpp SysObjects/Backup.h SysObjects/SysGame.cpp SysObjects/SysGame.h)
target_link_libraries (BrickShooter sfml-graphics sfml-window sfml-system)
还有我的 Windows 工具链:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/windows.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C, C++ and Fortran
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
# target environment on the build host system
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})
# modify default behavior of FIND_XXX() commands
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(SFML_INCLUDE_DIR "gitlab_ci/SFML-2.5.1-WIN/include")
include_directories(gitlab_ci/SFML-2.5.1-WIN/include)
set(SFML_LIBRARY_DIR "gitlab_ci/SFML-2.5.1-WIN/lib")
set(SFML_DIR "gitlab_ci/SFML-2.5.1-WIN/lib/cmake/SFML")
我正在使用以下命令手动构建程序:
cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make
gitlab_ci/SFML-2.5.1-WIN 是我为 Windows 存储包含、库等的位置。我在 Ubuntu 20.04 LTS 上,我已经使用以下命令安装了我需要的一切:
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential libsfml-dev cmake doxygen mingw-w64 curl unzip
curl -O https://www.sfml-dev.org/files/SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
unzip SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
mv SFML-2.5.1 gitlab_ci/SFML-2.5.1-WIN
rm SFML-2.5.1-windows-gcc-7.3.0-mingw-64-bit.zip
【问题讨论】:
-
链接器的错误
cannot find -lsfml-graphics表示链接器在预定义的目录集中找不到库。您需要使用 absolute path 到库或调用link_directory将所需目录添加到搜索列表。
标签: c++ linux cmake cross-compiling sfml