c++linuxcmakeclion

How to link lz4 library in CMake?


I trying to configure my C++ project in Clion on Ubuntu 22.04 using this cmake commands

find_package(lz4 CONFIG REQUIRED)

include_directories(${LZ4_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} lz4::lz4)

and also I installed zl4 using sudo apt -y install liblz4-dev

And during configuration I got an error

CMake Error at CMakeLists.txt:12 (find_package):
 Could not find a package configuration file provided by "lz4" with any of
 the following names:

   lz4Config.cmake
   lz4-config.cmake

 Add the installation prefix of "lz4" to CMAKE_PREFIX_PATH or set "lz4_DIR"
 to a directory containing one of the above files.  If "lz4" provides a
 separate development package or SDK, be sure it has been installed.

Solution

  • The liblz4-dev package doesn't provide any cmake configuration files, you'll need to use pkg-config instead.

    For example:

    find_package(PkgConfig REQUIRED)
    pkg_check_modules(lz4 REQUIRED IMPORTED_TARGET liblz4)
    target_link_libraries(${PROJECT_NAME} PkgConfig::lz4)
    

    you'll also need to install the pkg-config package.