如何使用 cmake link linux 上的共享库?

How to link shared library on linux using cmake?

如何在linux平台上link共享库?我使用 apt cmd 下载了 sfml 库,并尝试 运行 简单示例:

main.cpp

#include <SFML/Graphics.hpp>

int main()
{
    // Make a window that is 800 by 200 pixels
    // And has the title "Hello from SFML"
    sf::RenderWindow window(sf::VideoMode(800, 200), "Hello from SFML");
    return 0;
}

但是即使 vs 代码看到文件并让我使用 ctrl 按钮直接跳转到它们,我仍然得到未定义的引用。

cmake:

cmake_minimum_required(VERSION 3.0.0)
project(sflmProject VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(sflmProject main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

编译错误:

main] Building folder: firstSFLMProject 
[build] Starting build
[main] Changes were detected in CMakeLists.txt but we could not reconfigure the project because another operation is already in progress.
[proc] Executing command: /usr/bin/cmake --build /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/build --config Debug --target all -j 10 --
[build] -- Configuring done
[build] -- Generating done
[build] -- Build files have been written to: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/build
[build] Consolidate compiler generated dependencies of target sflmProject
[build] [ 50%] Building CXX object CMakeFiles/sflmProject.dir/main.cpp.o
[build] [100%] Linking CXX executable sflmProject
[build] /usr/bin/ld: CMakeFiles/sflmProject.dir/main.cpp.o: in function `main':
[build] /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:16: undefined reference to `sf::String::String(char const*, std::locale const&)'
[build] /usr/bin/ld: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:16: undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
[build] /usr/bin/ld: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:16: undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
[build] /usr/bin/ld: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:63: undefined reference to `sf::RenderWindow::~RenderWindow()'
[build] collect2: error: ld returned 1 exit status
[build] gmake[2]: *** [CMakeFiles/sflmProject.dir/build.make:97: sflmProject] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:839: CMakeFiles/sflmProject.dir/all] Error 2
[build] gmake: *** [Makefile:121: all] Error 2
[build] Build finished with exit code 2

我试过像这样修改cmake,但我不知道应该添加哪个文件:

cmake_minimum_required(VERSION 3.0.0)
project(sflmProject VERSION 0.1.0)

include(CTest)
enable_testing()

add_library(sfml SHARED /usr/include/SFML/*)
add_executable(sflmProject main.cpp)

target_link_libraries(sflmProject PRIVATE sfml)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

这里是 linux

上 sfml 的 cmake 文件示例
cmake_minimum_required(VERSION 3.22)

project(sfml-program LANGUAGES CXX)

add_executable(${PROJECT_NAME} main.cpp)

find_package (SFML 2.5 COMPONENTS graphics audio network REQUIRED)

target_link_libraries (${PROJECT_NAME} PUBLIC sfml-system sfml-graphics sfml-audio sfml-network)

或者这也有效:

target_link_libraries(${PROJECT_NAME}
    /usr/lib/x86_64-linux-gnu/libsfml-window.so
    /usr/lib/x86_64-linux-gnu/libsfml-graphics.so 
    /usr/lib/x86_64-linux-gnu/libsfml-audio.so
    /usr/lib/x86_64-linux-gnu/libsfml-network.so
    /usr/lib/x86_64-linux-gnu/libsfml-system.so
)