CMake无法在Linux上静态链接(在Windows上完美适用)

时间:2018-02-05 20:01:59

标签: linux cmake static-libraries sfml static-linking

我确实有以下CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

project(testovnik)

include_directories(include)

file(GLOB SOURCES "src/*.cpp" "include/*.hpp")
file(GLOB TESTS "tests/*.cpp")

set(EXECUTABLE_NAME "testovnik")

add_executable(${EXECUTABLE_NAME} ${SOURCES})

if(WIN32)
    set(SFML_ROOT "C:/lib/SFML")
endif()

set_target_properties(${EXECUTABLE_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML 2 REQUIRED graphics window system)
if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
endif()

当我在Windows上cmake时,它完美无缺。创建一个.sln,然后我可以构建整个项目并且它可以工作。现在我想在Linux上做同样的事情,必须静态地链接它。但它不起作用。我收到以下错误:

[frynio@manjaro bin]$ cmake ..
CMake Error at /usr/share/cmake-3.10/Modules/FindSFML.cmake:355 (message):
  Could NOT find SFML (missing: SFML_GRAPHICS_LIBRARY SFML_WINDOW_LIBRARY
  SFML_SYSTEM_LIBRARY)
Call Stack (most recent call first):
  CMakeLists.txt:22 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/frynio/projects/cpp/testovnik_cpp/bin/CMakeFiles/CMakeOutput.log".

如果我评论/删除行set(SFML_STATIC_LIBRARIES TRUE),那么它可以正常工作。我可以将其更改为set(SFML_STATIC TRUE),然后它也有效,make也可以。但是如果我创建项目,我会获得可执行文件,但是如果我卸载sfml(sudo pacman -R sfml),那么在尝试启动它时会出现以下错误:

[frynio@manjaro bin]$ ./testovnik
./testovnik: error while loading shared libraries: libsfml-graphics.so.2.4: cannot open shared object file: No such file or directory

有没有办法构建我的项目并将其交给其他人而不必安装sfml

1 个答案:

答案 0 :(得分:0)

假设您正在使用Archlinux(pacman?),您可以看到sfml package仅包含共享库...

您应该考虑从源代码构建SFML库以获取静态库......

ps:你也可以使用pacman -Ql sfml
问:查询模式
l:列出文件