如何使用cmake在linux上链接共享库?

Gam*_*ker 1 c++ linux external cmake visual-studio-code

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

主程序

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

生成:

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)
Run Code Online (Sandbox Code Playgroud)

编译错误:

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
Run Code Online (Sandbox Code Playgroud)

我尝试像这样修改 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)
Run Code Online (Sandbox Code Playgroud)

naf*_*ali 5

这是 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)
Run Code Online (Sandbox Code Playgroud)