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.
Bonus question: Why can't this be as simple as Python with pip
, Rust with cargo
or JavaScript with npm
?
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.