Add External Library | Clion
The IDE’s CMake cache viewer will show you exactly where FetchContent stored the library ( cmake-build-debug/_deps/ ), and code navigation works instantly. Method 5: vcpkg – The Missing Package Manager Best for: Large, complex dependencies with many variants (OpenCV, PCL, CGAL).
find_package(PkgConfig REQUIRED) pkg_check_modules(LIBUSB REQUIRED libusb-1.0) target_link_libraries(my_app PRIVATE $LIBUSB_LIBRARIES) target_include_directories(my_app PRIVATE $LIBUSB_INCLUDE_DIRS) Best for: Automatically downloading libraries from GitHub during configuration.
Example: Adding the popular fmt library: clion add external library
You have two files: a header ( .h ) and a library binary ( .lib , .a , .so , .dylib ). You need to tell CMake both where to find the headers and which library file to link. # Tell compiler where headers are target_include_directories(my_app PRIVATE /path/to/library/include) Tell linker where the .lib file is target_link_libraries(my_app PRIVATE /path/to/library/lib/mylib.lib) For a shared library ( .dll / .so ): Same as above, but after building, you must ensure the .dll or .so is in the same folder as your executable or in your system PATH / LD_LIBRARY_PATH . The Cleaner Way: Using Variables set(MY_LIB_DIR "/usr/local/mylib") target_include_directories(my_app PRIVATE $MY_LIB_DIR/include) target_link_libraries(my_app PRIVATE $MY_LIB_DIR/lib/mylib.so) Pro tip: Use add_library(IMPORTED) for maximum control:
include(FetchContent) FetchContent_Declare( fmt GIT_REPOSITORY https://github.com/fmtlib/fmt.git GIT_TAG master ) FetchContent_MakeAvailable(fmt) target_link_libraries(my_app PRIVATE fmt::fmt) The IDE’s CMake cache viewer will show you
find_package(Boost 1.75 REQUIRED COMPONENTS filesystem system) if(Boost_FOUND) target_link_libraries(my_app PRIVATE Boost::filesystem Boost::system) endif() CMake will automatically search standard system paths, or paths you hint via -DCMAKE_PREFIX_PATH . Not every library provides CMake configs. For those, you can use pkg-config (common on Linux):
One of the first hurdles every C++ developer faces when moving from a simple "Hello World" to a real-world project is dependency management. You need logging, networking, graphics, or maybe just a handy utility library. But how do you tell your IDE and compiler where to find these external libraries? Example: Adding the popular fmt library: You have
When you reload CMake in CLion, it will clone fmt , build it, and link it—all automatically.