Clion Add External Library Site

Now go forth and link without fear! Have a library that just won’t cooperate? Drop the error message in the comments, and I’ll help you debug it.

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. 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: Now go forth and link without fear

By Alex Mitchell | Estimated read time: 8 minutes but after building

This is the gold standard. Instead of hardcoding paths, you write:

When you reload CMake in CLion, it will clone fmt , build it, and link it—all automatically.

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):

Top
Hey, wait a minute.

This is awkward, but...

It looks like you're using an ad blocker. We get it, but (1) terrylove.com can't live without ads, and (2) ad blockers can cause issues with videos and comments. If you'd like to support the site, please allow ads.

If any particular ad is your REASON for blocking ads, please let us know. We might be able to do something about it. Thanks.
I've Disabled AdBlock    No Thanks