Hello,
I’m learning how to use cmake for future projects. So far, i built a sample executable, library and linked them together. Wrote a ctest test case, ran code coverage, memcheck and generated a html report, all good up to this point. Next, I wanted to link to an existing library on my ubuntu machine like UUID, I found the uuid.h file and the libuuid.so.
I tried everything recommended by google AI search and nothing seem to work, when compiling, my include <uuid/uuid> is never found. I checked the generated Makefile and it has no mention of uuid. Tried to locate a simple compile/linking example with cmake.
Once you start consuming third-party libraries is where things get a little “interesting”, shall we say.
IMO, the easiest thing to do is to start using a package manager like vcpkg to get access to third party libraries like uuid.
Also, I note that the library you’re using is only available on linux through vcpkg, whereas there are other uuid libraries that are supported by linux, macOS and Windows, such as stduuid.
If you’re not willing to use vcpkg, then your next step would be to write a CMake find module that locates uuid and sets up an imported target that you can link against with target_link_libraries. This isn’t much more complicated than the CMake you’ve already been writing, but there’s lots of little things that can go wrong and you’re more likely to gain more by learning a package manager like vcpkg than by learning how to write find modules.
There’s no need to do any of that and there’s nothing complex about this use case.
If they want to build with libuuid, installed from their standard upstream distro repositories, it will be as trivial as target_link_libraries(<tgt> PRIVATE uuid)
If they want to get very fancy with it, uuid ships a pkg-config file, so one can use the various pkg-config support mechanism in CMake to construct an imported target from that.
@vito.gamberini - Thank you for taking the time to put this example together.
I cloned and tried to build on my system but i’m on an older cmake version 3.22. I updated accordingly, and got a successful build. 3.22 doesn’t support FILE_SET.
They didn’t specify a C++ version, relying on the compiler default is fine. And yes they explicitly stated they wanted to use their system libuuid from Ubuntu.
target_link_libraries and target_include_directories on latest cmake version support full paths, but 3.22 cmake does not mention such usage.
cmake latest says:
If an imported library has the IMPORTED_NO_SONAME target property set, CMake may ask the linker to search for the library instead of using the full path (e.g. /usr/lib/libfoo.so becomes -lfoo).
The full path to the target’s artifact will be quoted/escaped for the shell automatically.