I'm trying to get a cross-plattform build system working using CMake. Now the software has a few dependencies. I compiled them myself and installed them on my system.
Some example files which got installed:
-- Installing: /usr/local/share/SomeLib/SomeDir/somefile
-- Installing: /usr/local/share/SomeLib/SomeDir/someotherfile
-- Installing: /usr/local/lib/SomeLib/somesharedlibrary
-- Installing: /usr/local/lib/SomeLib/cmake/FindSomeLib.cmake
-- Installing: /usr/local/lib/SomeLib/cmake/HelperFile.cmake
Now CMake has a find_package()
which opens a Find*.cmake
file and searches after the library on the system and defines some variables like SomeLib_FOUND
etc.
My CMakeLists.txt contains something like this:
set(CMAKE_MODULE_PATH "/usr/local/lib/SomeLib/cmake/;${CMAKE_MODULE_PATH}")
find_package(SomeLib REQUIRED)
The first command defines where CMake searches after the Find*.cmake
and I added the directory of SomeLib
where the FindSomeLib.cmake
can be found, so find_package()
works
as expected.
But this is kind of weird because one of the reasons why find_package()
exists is to get away from non-cross-plattform hard coded paths.
How is this usually done? Should I copy the cmake/
directory of SomeLib
into my project and set the CMAKE_MODULE_PATH
relatively?
Best Answer
Command
find_package
has two modes:Module
mode andConfig
mode. You are trying to useModule
mode when you actually needConfig
mode.Module mode
Find<package>.cmake
file located within your project. Something like this:CMakeLists.txt
content:Note that
CMAKE_MODULE_PATH
has high priority and may be usefull when you need to rewrite standardFind<package>.cmake
file.Config mode (install)
<package>Config.cmake
file located outside and produced byinstall
command of other project (Foo
for example).foo
library:Simplified version of config file:
By default project installed in
CMAKE_INSTALL_PREFIX
directory:Config mode (use)
Use
find_package(... CONFIG)
to includeFooConfig.cmake
with imported targetfoo
:Note that imported target is highly configurable. See my answer.
Update