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

标签 c++ linux build cmake pam

我正在尝试构建一个使用库 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)

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

最佳答案

我通过 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)

希望这有帮助!

关于c++ - 使用 libpam0g-dev 和 cmake 构建应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51611649/

相关文章:

linux - 阻塞和非阻塞 I/O

file - "otool"和 "file"可以是 't show architecture of the ".a"文件

Android App Gradle 构建错误 "Could not find com.github.championswimmer:SimpleFingerGestures_Android_Library:1.2"

c++ - 从文本文件中读取行并将字符串放入 vector 中?

c++ - 没有索引号的多维数组的声明

c++ - 在每个系统日志 C++ 之前调用 openlog

linux - 如果您使用 nginx,请删除 apache2?

c++ - "stat"函数更新慢?

linux - 在 Linux 中使用计时器停止内核线程

build - 如何在 Geany 文本编辑器中添加 4 个以上的 build "independent commands "? (在“构建”>“设置构建命令”中)