尝试使用CMake编译项目时出错,其中SFML库由mingw提供

Error when I try to compile project using CMake with SFML library by mingw(尝试使用CMake编译项目时出错,其中SFML库由mingw提供)
本文介绍了尝试使用CMake编译项目时出错,其中SFML库由mingw提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想用CMake和最新版本的SFML库一起编译项目时,我发现了很多奇怪的错误(c++)。

C生成代码:

cmake_minimum_required(VERSION 3.0)

project(MLproject)

SET(CMAKE_SYSTEM_NAME    Windows)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER     "i686-w64-mingw32-gcc")
SET(CMAKE_CXX_COMPILER   "i686-w64-mingw32-g++")
SET(CMAKE_RC_COMPILER    "i686-w64-mingw32-windres")
SET(CMAKE_RANLIB         "i686-w64-mingw32-ranlib")
set(CMAKE_CXX_STANDARD 17)

SET(CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32")
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(SOURCE_EXE src/main.cpp)

set(SOURCE_GAME
  src/Boss/Boss.cpp
  *...*
  src/Game/Game.cpp)

set(SOURCE_UI
  src/UI/ScrollBar/ScrollBar.cpp)

add_library(UI SHARED ${SOURCE_UI})
add_library(Game SHARED ${SOURCE_GAME})

add_executable(main ${SOURCE_EXE})

target_link_libraries(main Game UI
  sfml-graphics sfml-system sfml-windos sfml-audio)

错误:

CMakeFiles/UI.dir/src/UI/ScrollBar/ScrollBar.cpp.o:ScrollBar.cpp:(.text+0x6d): undefined reference to `_imp___ZN2sf14RectangleShapeC1ERKNS_7Vector2IfEE'
CMakeFiles/UI.dir/src/UI/ScrollBar/ScrollBar.cpp.o:ScrollBar.cpp:(.text+0x399): undefined reference to `_imp___ZN2sf12RenderStates7DefaultE.....

我如何才能修复它?我会因为C++版本太旧而出错吗?(一切都针对Linux成功编译,但我想做跨平台)

推荐答案

错误:

CMakeFiles/UI.dir/src/UI/ScrollBar/ScrollBar.cpp.o:ScrollBar.cpp:(.text+0x6d): undefined reference to `_imp___ZN2sf14RectangleShapeC1ERKNS_7Vector2IfEE'

表示UI库使用了一些SFML组件。但是,您只将SFML库链接到main可执行文件,因此该库不知道这些SFML引用是在哪里定义的。因此,您必须将任何相关的SFML库链接到UI目标:

target_link_libraries(UI PUBLIC sfml-graphics sfml-system sfml-windos sfml-audio)

在这里使用PUBLIC将SFML库传播给UI目标的使用者,例如您的main可执行文件。这样就不需要显式地将它们链接到main。以下是您进行以下更改的完整C Make:

cmake_minimum_required(VERSION 3.0)

project(MLproject)

# As commented, consider moving these calls to your toolchain file.
SET(CMAKE_SYSTEM_NAME    Windows)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER     "i686-w64-mingw32-gcc")
SET(CMAKE_CXX_COMPILER   "i686-w64-mingw32-g++")
SET(CMAKE_RC_COMPILER    "i686-w64-mingw32-windres")
SET(CMAKE_RANLIB         "i686-w64-mingw32-ranlib")

SET(CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32")
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(CMAKE_CXX_STANDARD 17)

set(SOURCE_EXE src/main.cpp)

set(SOURCE_GAME
  src/Boss/Boss.cpp
  *...*
  src/Game/Game.cpp)

set(SOURCE_UI
  src/UI/ScrollBar/ScrollBar.cpp)

add_library(UI SHARED ${SOURCE_UI})
# Link SFML libraries to UI.
target_link_libraries(UI PUBLIC sfml-graphics sfml-system sfml-windos sfml-audio)

add_library(Game SHARED ${SOURCE_GAME})

add_executable(main ${SOURCE_EXE})

# Link Game and UI to the executable, which propagates the SFML libraries from UI also.
target_link_libraries(main PRIVATE Game UI)
注意,CMake顶部的许多调用都属于工具链文件(如注释所示),通常用于cross-compiling。我鼓励您通读文档以确定您是否需要它,并了解如何设置它。

这篇关于尝试使用CMake编译项目时出错,其中SFML库由mingw提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Unknown type name __m256 - Intel intrinsics for AVX not recognized?(未知类型名称__M256-英特尔AVX内部功能无法识别?)
How can an declare an array in a function that returns an array in c++(如何在用C++返回数组的函数中声明数组)
Is it possible to define a class in 2 or more file in C++?(在C++中可以在两个或多个文件中定义一个类吗?)
Why can#39;t I create an array of automatic variables?(为什么我不能创建一个自动变量数组?)
zeromq: reset REQ/REP socket state(Zeromq:重置REQ/REP套接字状态)
Can I resize a vector that was moved from?(我可以调整从中移出的矢量的大小吗?)