c++cmakecmake-modules

CMake BUILD undefined reference (findpng)


I'm still very new to CMake so feedback is definitely welcome. So, I'm trying to build a simple application that should eventually create a pdf using the library libharu.

I think i figured it out how to link the library. But I still receive build errors for the findpng module (I suppose libharu depends on it)

build

CMakeLists.txt:

cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)     # current latest stable version (if lower give FATAL_ERROR)
project(pdf_generator VERSION 0.1.0)                  # name of the project, version.

file(GLOB TARGET_SRC "./src/*.cpp")                   # Creates variable, using globbing.

include_directories(${PROJECT_SOURCE_DIR}/include)    # list of directories to be used as header search paths.
add_executable(main ${TARGET_SRC})                    # Create an executable of set of source files [exe name files to bundle].



find_library(libhpdf_location NAMES libhpdf.a)        # find the location of libhpdf.a and save the value in the variable libhpdf_location.
message(STATUS ${libhpdf_location})                   # print status of variable.

add_library(libhpdf STATIC IMPORTED)                  # Add library via a static import.
set_target_properties(
  libhpdf PROPERTIES
  IMPORTED_LOCATION ${libhpdf_location}
)

target_link_libraries(main libhpdf)

Solution

  • I've never worked with that particular library before, but skimming their CMakeLists.txt on GitHub it seems like libharu has optional dependencies on libpdf and zlib. Without knowing how you built your version of libharu I'm going to assume that both are needed.

    Luckily, CMake comes with find-modules for both libpng and zlib, so adding the following should work:

    find_package(PNG REQUIRED)
    find_package(ZLIB REQUIRED)
    
    set_target_properties(libhpdf
      PROPERTIES
        INTERFACE_LINK_LIBRARIES "ZLIB::ZLIB;PNG::PNG"
    )