C++ 使用 cmake 构建库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11217800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:57:30  来源:igfitidea点击:

Building library with cmake

c++buildcmakesharedsfml

提问by Cuthalion

I apologize for bothering you all, but I have a little compilation problem with cmake.

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

I have a CMakeLists.txt file I'm using to build a test executable, and a shared library. They both have dependency to another library (SFML).

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

I'm using cmake on window with MinGW.

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

I know the name of the lib I'm building is kinda confusing with the sfml one, but it's supposed to be a SFML wrapper, so, I didn't find a better name!

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

Here the CMakeLists.txt

这里是 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}
)

And what I get in the terminal :

我在终端得到了什么:

"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

If anybody have a clue on what's happening, I'd be very gratefull!

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

回答by Fraser

At a guess, your SFMLwindowlibrary needs linked to some or all of sfml-main, sfml-system, sfml-window.

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

You could try changing the end of your CMakeLists.txt to:

您可以尝试将 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}
)


As an aside, file(GLOB_RECURSE...is generally frowned upon as a way to gather a list of sources. From the docs for file:


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.

我们不建议使用 GLOB 从源树中收集源文件列表。如果在添加或删除源时没有 CMakeLists.txt 文件更改,则生成的构建系统无法知道何时要求 CMake 重新生成。


Also, find_libraryshould be preferred to link_directoriesin this case. From the docs for link_directories:


此外,在这种情况下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.

请注意,很少需要此命令。find_package() 和 find_library() 返回的库位置是绝对路径。将这些绝对库文件路径直接传递给 target_link_libraries() 命令。CMake 将确保链接器找到它们。