CMake compile options for libpng

后端 未结 2 740
无人及你
无人及你 2020-12-03 20:12

I am using libpng in my project. Right now, I can compile my project with: g++ *.cpp `libpng-config --ldflags`

I want to switch to using CMake for easy recompiling

相关标签:
2条回答
  • 2020-12-03 20:46

    I finally solved it using find_package. Thanks to this blog post.

    find_package(PNG REQUIRED)
    include_directories(${PNG_INCLUDE_DIR})
    target_link_libraries(${MY_EXEC} ${PNG_LIBRARY})
    
    0 讨论(0)
  • 2020-12-03 20:47

    I think the recommended and portable way it should be done using pkg-config, something like this:

    # search for pkg-config
    include (FindPkgConfig)
    if (NOT PKG_CONFIG_FOUND)
        message (FATAL_ERROR "pkg-config not found")
    endif ()
    
    # check for libpng
    pkg_check_modules (LIBPNG libpng16 REQUIRED)
    if (NOT LIBPNG_FOUND)
        message(FATAL_ERROR "You don't seem to have libpng16 development libraries installed")
    else ()
        include_directories (${LIBPNG_INCLUDE_DIRS})
        link_directories (${LIBPNG_LIBRARY_DIRS})
        link_libraries (${LIBPNG_LIBRARIES})
    endif ()
    add_executable (app_png ${_MYSOURCES} ${LIBPNG_LINK_FLAGS})
    
    0 讨论(0)
提交回复
热议问题