使用 libpam0g-dev 和 cmake 构建应用程序

Dav*_*agl 4 c++ linux build cmake pam

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

apt-get install libpam0g-dev
Run Code Online (Sandbox Code Playgroud)

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

undefined reference to `pam_start`
undefined reference to `pam_authenticate`
undefined reference to `pam_end`
Run Code Online (Sandbox Code Playgroud)

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

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

Vas*_*yev 5

我找到了一个很好的解决方案,其中的find_package选项来自CMake. CMake提供了一种查找具有指定FindModule.cmake文件的包/库的方法。

一个真正的好消息是有很多现有的模块文件。您可以使用此版本在 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)

Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!