【问题标题】:Build Application using libpam0g-dev with cmake使用 libpam0g-dev 和 cmake 构建应用程序
【发布时间】:2018-07-31 10:51:31
【问题描述】:

我正在尝试构建一个使用库 libpamg0-dev 的 C++ 应用程序。 我在我的elementaryOS VM 上使用以下命令安装了它。

apt-get install libpam0g-dev

当我尝试编译应用程序时,编译器会吐出以下错误:

undefined reference to `pam_start`
undefined reference to `pam_authenticate`
undefined reference to `pam_end`

我的 CMakeLists.txt 看起来像这样:

cmake_minimum_required(VERSION 3.10)
project(application)

set(CMAKE_CXX_STANDARD 11)

INCLUDE_DIRECTORIES(/home/dnagl/dev/libs/restbed/distribution/include /usr/include/security)
LINK_DIRECTORIES(/home/dnagl/dev/libs/restbed/distribution/library /usr/lib/x86_64-linux-gnu)

add_executable(application main.cpp Utils/Json/Json.cpp Utils/Json/Json.h Utils/Stringhelper/Stringhelper.cpp Utils/Stringhelper/Stringhelper.h Utils/File/Filehelper.cpp Utils/File/Filehelper.h Utils/System/SystemHelper.cpp Utils/System/SystemHelper.h  Controller/Info/InfoController.cpp Controller/Info/InfoController.h Rest/ResourceHandler/ResourceHandler.cpp Rest/ResourceHandler/ResourceHandler.h Controller/System/SystemController.cpp Controller/System/SystemController.h Rest/Log/RequestLogger.cpp Rest/Log/RequestLogger.h Controller/Authentication/AuthenticationController.cpp Controller/Authentication/AuthenticationController.h Controller/Log/LogController.cpp Controller/Log/LogController.h)

target_link_libraries(application restbed)

也许你们当中有人知道如何以正确的方式链接库。

【问题讨论】:

    标签: c++ linux build cmake pam


    【解决方案1】:

    我从CMake 找到了带有find_package 选项的不错的解决方案。 CMake 提供了一种查找具有指定 FindModule.cmake 文件的包/库的方法。

    一个非常好的消息是有很多现有的模块文件。您可以使用this version 在 Linux 上查找 PAM 包。将其放入项目文件夹中的cmake/modules/ 并更新您的CMakeLists.txt

    cmake_minimum_required(VERSION 3.10)
    project(restbed)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_EXTENSIONS OFF)
    
    # Notify CMake that we have module files to find packages/libs.
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
    
    find_package(PAM REQUIRED)
    
    # Check if we found PAM.
    if (NOT PAM_FOUND)
        message(FATAL_ERROR "PAM library was not found.")
    endif ()
    
    # Source configuration.
    include_directories(
       ${PAM_INCLUDE_DIR}
       ${CMAKE_BINARY_DIR}
       ${CMAKE_CURRENT_BINARY_DIR}
    )
    
    set(EXECUTABLE_NAME "application")
    
    # Add sources to this project's executable.
    add_executable(${EXECUTABLE_NAME}
        "main.cpp"
        "Utils/Json/Json.cpp"
        "Utils/Json/Json.h"
        "Utils/Stringhelper/Stringhelper.cpp"
        "Utils/Stringhelper/Stringhelper.h"
        "Utils/File/Filehelper.cpp"
        "Utils/File/Filehelper.h"
        "Utils/System/SystemHelper.cpp"
        "Utils/System/SystemHelper.h"
        "Controller/Info/InfoController.cpp"
        "Controller/Info/InfoController.h"
        "Rest/ResourceHandler/ResourceHandler.cpp"
        "Rest/ResourceHandler/ResourceHandler.h"
        "Controller/System/SystemController.cpp"
        "Controller/System/SystemController.h"
        "Rest/Log/RequestLogger.cpp"
        "Rest/Log/RequestLogger.h"
        "Controller/Authentication/AuthenticationController.cpp"
        "Controller/Authentication/AuthenticationController.h"
        "Controller/Log/LogController.cpp"
        "Controller/Log/LogController.h"
    )
    
    target_link_libraries(${EXECUTABLE_NAME}
        ${PAM_LIBRARIES}
    )
    
    set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINKER_LANGUAGE CXX)
    
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 1970-01-01
      • 2021-12-01
      • 2022-06-14
      • 2012-10-02
      • 2021-11-23
      • 1970-01-01
      • 2017-04-09
      • 2020-01-08
      相关资源
      最近更新 更多