This is a working example project to demonstrate how to define a cmake target for a non-cmake external library.
Our main file src/main.cpp is part of a cmake project, defined in CMakeLists.txt. It depends on a non-cmake library libfoo. We compile libfoo with a simple makefile foo/Makefile. To use libfoo in any project, the compiler and linker need to find the header file, and the archive file libfoo.a.
We would like to have a cmake target Foo::Foo for libfoo to make it accessible like any other cmake based library. This target is defined in cmake/FindFoo.cmake, which is used by cmake to find libfoo. See line find_package(Foo REQUIRED) of CMakeLists.txt.
The easiest way to build the example is to simply run build.sh which contains the instructions below.
Build dependency libfoo via makefile located in folder foo.
make -C fooCompile src/main.cpp using cmake. libfoo is found through FindFoo.cmake.
cmake -Bbuild -H.
make -C buildInvoke the built executable.
./build/mainHave fun exploring the example code.
- FindFoo.cmake is based on Daniel Pfeifers talk Effective CMake (slide 22).