Home > Enterprise >  Creating a target for a non-CMake module [duplicate]
Creating a target for a non-CMake module [duplicate]

Time:09-21

I'm pretty new to CMake and am trying to learn about modern CMake.

I'm creating a project using the poppler-cpp libraries on ubuntu. The libraries are installed using sudo apt install libpoppler-cpp-dev so they should all be available on the system paths.

My goal is to make this build work on multiple platforms eventually. But baby steps...

Previously I was setting up to import the library as follows using pkg-config

find_package(PkgConfig REQUIRED)
pkg_check_modules(POPPLER_CPP REQUIRED poppler-cpp)

target_link_libraries(${PROJECT_NAME} 
    # prefer to statically link poppler
    # the other option would be POPPLER_LIBRARIES
    PRIVATE ${POPPLER_CPP_STATIC_LIBRARIES}
)

target_include_directories(${PROJECT_NAME} 
    PUBLIC ${PROJECT_SOURCE_DIR}/include
    # Include output from pkg_config
    PUBLIC ${POPPLER_CPP_INCLUDE_DIRS}
    # Need to add this to use 
    # generated export headers
    ${PROJECT_BINARY_DIR}
)

target_compile_options(${PROJECT_NAME} 
    PUBLIC ${POPPLER_CPP_CFLAGS_OTHER})

After reading some more about modern CMake, I decided to make this a target instead. So I created a FindPopplerCpp.cmake and added the following

find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_POPPLER_CPP REQUIRED poppler-cpp)


set(PopplerCpp_VERSION ${PC_POPPLER_CPP_VERSION})
set(PopplerCpp_INCLUDE_DIRS ${PC_POPPLER_CPP_INCLUDE_DIRS})
set(PopplerCpp_CFLAGS_OTHER ${PC_POPPLER_CPP_CFLAGS_OTHER})


if(PC_POPPLER_CPP_FOUND AND NOT TARGET poppler::Cpp)
  add_library(poppler::Cpp INTERFACE IMPORTED)
  set_target_properties(poppler::Cpp PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
        INTERFACE_INCLUDE_DIRECTORIES "${PC_POPPLER_CPP_INCLUDE_DIRS}"
        INTERFACE_LINK_LIBRARIES "${PC_POPPLER_CPP_STATIC_LIBRARIES}"
        INTERFACE_COMPILE_OPTIONS "${PC_POPPLER_CPP_CFLAGS}"
        VERSION "${PC_POPPLER_CPP_VERSION}"
    )
ELSE (PC_POPPLER_CPP_FOUND)
    MESSAGE(FATAL_ERROR "Could not find library")
endif()

mark_as_advanced(PopplerCpp_FOUND PopplerCpp_INCLUDE_DIRS PopplerCpp_VERSION)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PopplerCpp
    REQUIRED_VARS PopplerCpp_INCLUDE_DIRS
    VERSION_VAR PopplerCpp_VERSION
)

And then I am importing the target

target_link_libraries(${PROJECT_NAME} 
    PRIVATE poppler::Cpp
)

My understanding is that this will take care of all the linker and include paths. I am able to build the target fine and it seems to work.

The use of the static libs is intentional. I am statically linking the poppler-cpp libraries to my shared libraries.

I just wanted to be sure that I'm setting this up properly, especially considering I would like to build this project on the mac and on windows at some point. I know pkg-config won't work here. But I'll cross that bridge when I get there.

Thanks for your help

CodePudding user response:

You should just use the IMPORTED_TARGET option of pkg_check_modules...

find_package(PkgConfig REQUIRED)
pkg_check_modules(poppler-cpp REQUIRED IMPORTED_TARGET poppler-cpp)

target_link_libraries(my_target PRIVATE PkgConfig::poppler-cpp)
  • Related