简体   繁体   English

Linux 上 Windows 的 SFML 交叉编译

[英]SFML Cross-compilation for Windows on Linux

I'm trying to compile a simple program with SFML (basic helloworld but with a big green circle).我正在尝试用 SFML 编译一个简单的程序(基本的 helloworld,但有一个大的绿色圆圈)。 I'm supposed to build the program on Linux for Linux and Windows.我应该在 Linux 上为 Linux 和 Windows 构建程序。 When I compile for Linux the program compile and run perfectly, however when I try to compile the Windows Executable, I get the following error:当我为 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

I tried to change the setting in my Cmake without success (add_library, include_directories) but so far nothing worked.我试图更改我的 Cmake 中的设置但没有成功(add_library,include_directories),但到目前为止没有任何效果。 Here is my CMakeLists.txt:这是我的 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)

And my toolchain for Windows:我的 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")

I'm using the following command to manually build the program:我正在使用以下命令手动构建程序:

cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make

Where gitlab_ci/SFML-2.5.1-WIN is where I stored the includes, libs, etc for Windows. gitlab_ci/SFML-2.5.1-WIN是我存储 Windows 的包含、库等的地方。 I'm on Ubuntu 20.04 LTS and I have installed everything I needed with the following commands:我在 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

Thanks to @tsyvarev here is the solution that worked for me: I added this line to my toolchain for Windows感谢@tsyvarev,这是对我有用的解决方案:我将此行添加到 Windows 的工具链中

link_directories(gitlab_ci/SFML-2.5.1-WIN/lib)

The final result is:最终结果是:

# 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")
link_directories(gitlab_ci/SFML-2.5.1-WIN/lib)
set(SFML_DIR "gitlab_ci/SFML-2.5.1-WIN/lib/cmake/SFML")

and this give the following output:这给出了以下 output:

cmake gitlab_ci/SFML-2.5.1-WIN/ -DCMAKE_TOOLCHAIN_FILE=gitlab_ci/windows.cmake CMakeLists.txt && make
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /mnt/shared/school/Ynov/Home_Work/uf_dev_log_13/repo_gitlab
Scanning dependencies of target BrickShooter
[ 10%] Building CXX object CMakeFiles/BrickShooter.dir/main.cpp.obj
[ 20%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Block.cpp.obj
[ 30%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Player.cpp.obj
[ 40%] Building CXX object CMakeFiles/BrickShooter.dir/GameObjects/Shoot.cpp.obj
[ 50%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Game.cpp.obj
[ 60%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Store.cpp.obj
[ 70%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Menu.cpp.obj
[ 80%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/Backup.cpp.obj
[ 90%] Building CXX object CMakeFiles/BrickShooter.dir/SysObjects/SysGame.cpp.obj
[100%] Linking CXX executable BrickShooter.exe
[100%] Built target BrickShooter

Here is the link to the CMake docs about link_directories() 这是 CMake 文档关于link_directories()的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM