添加libsystemd依赖失败:CMake无法找到libsystemd包求助
问题描述
尝试添加libsystemd依赖以使用头文件#include <systemd/sd-daemon.h>,但构建时出错,报错日志及对应的CMakeLists.txt代码如下:
构建报错日志
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found version "1.71.0") found components: system thread filesystem -- Checking for module 'libsystemd' -- No package 'libsystemd' found -- Configuring incomplete, errors occurred! See also "/home/src/build-tiles-Desktop_Qt_5_15_2_GCC_64bit-Debug/CMakeFiles/CMakeOutput.log". See also "/home/src/build-tiles-Desktop_Qt_5_15_2_GCC_64bit-Debug/CMakeFiles/CMakeError.log". SIMPLE_WEB_SERVER include dir =/home/src/polaris-tiles/style-server/Simple-Web-Server SIMPLE_WEB_SERVER include dir =/home/src/polaris-tiles/tile-server/Simple-Web-Server CMake Error at /home/Qt/Tools/CMake/share/cmake-3.21/Modules/FindPkgConfig.cmake:554 (message): A required package was not found Call Stack (most recent call first): /home/Qt/Tools/CMake/share/cmake-3.21/Modules/FindPkgConfig.cmake:776 (_pkg_check_modules_internal) Simple-Web-Server/CMakeLists.txt:22 (pkg_check_modules)
关联的CMakeLists.txt代码
cmake_minimum_required(VERSION 3.0) project(Simple-Web-Server) include(GNUInstallDirs) option(USE_STANDALONE_ASIO "set ON to use standalone Asio instead of Boost.Asio" OFF) if(CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") option(BUILD_TESTING "set ON to build library tests" ON) else() option(BUILD_TESTING "set ON to build library tests" OFF) endif() option(BUILD_FUZZING "set ON to build library fuzzers" OFF) option(USE_OPENSSL "set OFF to build without OpenSSL" ON) add_library(simple-web-server INTERFACE) target_include_directories(simple-web-server INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) find_package(PkgConfig REQUIRED) pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd) find_package(Threads REQUIRED) target_link_libraries(simple-web-server INTERFACE PkgConfig::SYSTEMD) target_link_libraries(simple-web-server INTERFACE ${CMAKE_THREAD_LIBS_INIT}) # TODO 2020 when Debian Jessie LTS ends: # Remove Boost system, thread, regex components; use Boost::<component> aliases; remove Boost target_include_directories if(USE_STANDALONE_ASIO) message("USING STANDALONE") target_compile_definitions(simple-web-server INTERFACE ASIO_STANDALONE) find_path(ASIO_PATH asio.hpp) if(NOT ASIO_PATH) message(FATAL_ERROR "Standalone Asio not found") else() target_include_directories(simple-web-server INTERFACE ${ASIO_PATH}) endif() else() message("NOT USING STANDALONE") find_package(Boost 1.53.0 COMPONENTS system thread REQUIRED) target_link_libraries(simple-web-server INTERFACE ${Boost_LIBRARIES}) target_include_directories(simple-web-server INTERFACE ${Boost_INCLUDE_DIR}) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) target_compile_definitions(simple-web-server INTERFACE USE_BOOST_REGEX) find_package(Boost 1.53.0 COMPONENTS regex REQUIRED) target_link_libraries(simple-web-server INTERFACE ${Boost_LIBRARIES}) target_include_directories(simple-web-server INTERFACE ${Boost_INCLUDE_DIR}) endif() endif() if(WIN32) target_link_libraries(simple-web-server INTERFACE ws2_32 wsock32) endif() if(APPLE) set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl") endif() if(USE_OPENSSL) find_package(OpenSSL) endif() if(OPENSSL_FOUND) target_compile_definitions(simple-web-server INTERFACE HAVE_OPENSSL) target_link_libraries(simple-web-server INTERFACE ${OPENSSL_LIBRARIES}) target_include_directories(simple-web-server INTERFACE ${OPENSSL_INCLUDE_DIR}) endif() # If Simple-Web-Server is not a sub-project: if(CMAKE_SOURCE_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") message("NOT A SUB-PROJECT") if(NOT MSVC) add_compile_options(-std=c++11 -Wall -Wextra) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wthread-safety) endif() else() add_compile_options(/W1) endif() find_package(Boost 1.53.0 COMPONENTS system thread filesystem) if(Boost_FOUND) add_executable(http_examples http_examples.cpp) target_link_libraries(http_examples simple-web-server) target_link_libraries(http_examples ${Boost_LIBRARIES}) target_include_directories(http_examples PRIVATE ${Boost_INCLUDE_DIR}) if(OPENSSL_FOUND) add_executable(https_examples https_examples.cpp) target_link_libraries(https_examples simple-web-server) target_link_libraries(https_examples ${Boost_LIBRARIES}) target_include_directories(https_examples PRIVATE ${Boost_INCLUDE_DIR}) endif() endif() install(FILES asio_compatibility.hpp server_http.hpp client_http.hpp server_https.hpp client_https.hpp crypto.hpp utility.hpp status_code.hpp mutex.hpp DESTINATION include/simple-web-server) endif() if(BUILD_TESTING OR BUILD_FUZZING) if(BUILD_TESTING) enable_testing() endif() add_subdirectory(tests) endif()
解决方案
1. 安装libsystemd开发依赖
报错核心是系统找不到libsystemd的pkgconfig配置文件,这是因为缺少对应的开发包,根据你的发行版执行以下命令安装:
- Debian/Ubuntu:
sudo apt-get install libsystemd-dev
- RHEL/CentOS:
sudo yum install systemd-devel
- Fedora:
sudo dnf install systemd-devel
2. 修正CMake配置(可选)
如果安装后仍报错,可能是pkgconfig包名差异,部分系统中libsystemd的pkgconfig文件名为systemd而非libsystemd,修改CMakeLists.txt中的对应行:
将:
pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
替换为:
pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET systemd)
如果libsystemd安装在非标准路径,需手动指定pkgconfig路径:
set(ENV{PKG_CONFIG_PATH} "/自定义路径/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}") pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
3. 验证头文件路径
确保/usr/include/systemd/sd-daemon.h存在,若不存在,检查开发包是否安装正确,或手动添加头文件路径到CMake配置:
target_include_directories(simple-web-server INTERFACE /usr/include/systemd)
内容的提问来源于stack exchange,提问作者island slangz
相关产品推荐
相关产品推荐

