Build Application using libpam0g-dev with cmake
Asked Answered
I

1

5

I'm trying to build a C++ application which uses the library libpamg0-dev. I installed it with the following command on my elementaryOS VM.

apt-get install libpam0g-dev

When I try to compile the application, the compiler spits out the following errors:

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

My CMakeLists.txt looks like this:

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)

Maybe one of you knows how to link the library in the right way.

Istle answered 31/7, 2018 at 10:51 Comment(0)
S
6

I have found a nice solution with find_package option from CMake. CMake provides a way to find packages/libraries with specified FindModule.cmake file.

A really good news is that there are a lot of existing module files. You can use this version to find PAM package on Linux. Put it to cmake/modules/ in your project folder and update your 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)

Hope this helps!

Siderosis answered 21/10, 2019 at 20:26 Comment(1)
We shouldn't need the "Check if we found PAM" block, since find_package(... REQUIRED) will stop execution in that case. cmake.org/cmake/help/latest/command/…Multiplicand

© 2022 - 2024 — McMap. All rights reserved.