Search code examples
c++cmakebuildlibrariesdependency-management

How to install external library from source for cmake to find it?


Say I am writing a library my_project that uses some other library dependency_lib. What I'm currently doing during development is: I've cloned the code of the dependency to some normal, human accessible directory in my file system. I've built that library according to the instructions from its developers, and now I have a shared library dependency_lib.so somewhere in that directory. Then in my CMakeLists.txt I have:

find_library(DEPENDENCY_LIB dependency_lib HINTS /home/my_name/Documents/path/to/the/built/dependency/lib)
target_link_libraries(my_project PRIVATE ${DEPENDENCY_LIB})

This works great for me, but obviously when other people clone my code and try to build it, they will have that external library at some other location on their system, so this won't work for them.

  1. Is there some standard location to put built libraries in for cmake to find them, where I can assume other developers also put their built libraries in?
  2. Is it even reasonable for me to expect other developers/users of my library to also clone the source code of the dependency and build it, or is there some other solution for external dependencies on C++? Any comments, explanations and resources on dependency management with cmake are very appreciated.

Bonus question: Why can't this be as simple as Python with pip, Rust with cargo or JavaScript with npm?


Solution

  • Mostly, developers add SDKs folder to let others use the required SDKs. It may be because they did minor/major changes inside the library and it became a custom build version and special for them. So, it means, they have to pack it inside the project since others can't find the modified version of that library. And I think, this is what you're looking for:

    find_package(Library ${LIB_VERSION} EXACT REQUIRED PATHS "${LIB_DIR}")
    

    By declaring PATHS you can have CMake look for the libraries specific directories.