gpt4 book ai didi

c++ - 使用 cmake 构建库

转载 作者:可可西里 更新时间:2023-11-01 18:19:03 32 4
gpt4 key购买 nike

很抱歉打扰了大家,但是我在使用 cmake 时遇到了一点编译问题。

我有一个用于构建测试可执行文件的 CMakeLists.txt 文件和一个共享库。它们都依赖于另一个库 (SFML)。

我在带有 MinGW 的窗口上使用 cmake。

我知道我正在构建的库的名称与 sfml 有点混淆,但它应该是一个 SFML 包装器,所以,我没有找到更好的名称!

这里是 CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(projectName)

set(EXECUTABLE_NAME testSFML)
set(LIBRARY_NAME SFMLwindow)

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin/)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include /
${CMAKE_CURRENT_SOURCE_DIR}/../../include
)

link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../lib/)

file(
GLOB_RECURSE
SRC_FILES
src/*
)

file(
GLOB_RECURSE
INCLUDE_FILES
include/*
)

add_executable(
${EXECUTABLE_NAME}
main.cpp
${SRC_FILES}
${INCLUDE_FILES}
)

target_link_libraries(
${EXECUTABLE_NAME}
sfml-main
sfml-system
sfml-window
)


add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
)

以及我在终端中得到的:

"C:\MinGW\bin\mingw32-make.exe" 
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/iksemel/docs/WorkBench/programming/projets/TestSFML/cmake
Linking CXX shared library libSFMLwindow.dll
Creating library file: libSFMLwindow.dll.a
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x59):undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0xda): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKSsjRKNS_15ContextSettingsE'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x163): undefined reference to `_imp___ZN2sf6Window5closeEv'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1bd): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
CMakeFiles\SFMLwindow.dir/objects.a(SFMLWindow.cpp.obj):SFMLWindow.cpp:(.text+0x1d8): undefined reference to `_imp___ZN2sf6Window7displayEv'
collect2: ld a retourné 1 code d'état d'exécution
mingw32-make.exe[2]: *** [libSFMLwindow.dll] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/SFMLwindow.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2

如果有人知道发生了什么,我将不胜感激!

最佳答案

据推测,您的 SFMLwindow 库需要链接到 sfml-main、sfml-system、sfml-window 的部分或全部。

您可以尝试将 CMakeLists.txt 的末尾更改为:

add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
${INCLUDE_FILES}
)

add_executable(
${EXECUTABLE_NAME}
main.cpp
)

target_link_libraries(
${LIBRARY_NAME}
sfml-main
sfml-system
sfml-window
)

target_link_libraries(
${EXECUTABLE_NAME}
${LIBRARY_NAME}
)


顺便说一句,file(GLOB_RECURSE... 作为收集源列表的一种方式通常不受欢迎。来自 file 的文档:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.


另外,find_library应该优先于 link_directories在这种情况下。来自 link_directories 的文档:

Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.

关于c++ - 使用 cmake 构建库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11217800/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com