How to link Optick to a custom project with CMake?

Viewed 75

I relatively new to CMake and I'm trying to link different libraries to my projects. While I could handle some libraries like GLFW, I have troubles with Optick: https://github.com/bombomby/optick

The source tree is:

  • CMakeLists
  • src
    • CMakeLists
    • main.cpp
  • optick

The root CMakeList file:

cmake_minimum_required(VERSION 3.7)

project( OptickTest )

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

add_subdirectory(optick)
add_subdirectory(src)

And the CMakeLists in the src folder:

cmake_minimum_required(VERSION 3.7)

add_executable( OptickTest "main.cpp" )

target_link_libraries( OptickTest OptickCore )

I can generate a Visual Studio solution, but when I execute it, it cannot find "OptickCored.dll".

Optick seems to be a simple and lightweight library, so I must have done something wrong here, but I can't find it.

1 Answers

Here's how I set up a build to use it. First, I download the sources, build it, and install it to a local prefix.

$ git clone git@github.com:bombomby/optick.git
$ cmake -G "Ninja Multi-Config" -S optick -B build/optick
$ cmake --build build/optick --config Debug
$ cmake --build build/optick --config Release
$ cmake --install build/optick --config Debug --prefix local
-- Installing: /path/to/local/lib/libOptickCored.so
-- Installing: /path/to/local/include/optick.h
-- Installing: /path/to/local/include/optick.config.h
-- Installing: /path/to/local/lib/cmake/Optick/OptickTargets.cmake
-- Installing: /path/to/local/lib/cmake/Optick/OptickTargets-debug.cmake
-- Installing: /path/to/local/lib/cmake/Optick/OptickConfig.cmake
-- Installing: /path/to/local/lib/cmake/Optick/OptickConfigVersion.cmake
$ cmake --install build/optick --config Release --prefix local
-- Installing: /path/to/local/lib/libOptickCore.so
-- Up-to-date: /path/to/local/include/optick.h
-- Up-to-date: /path/to/local/include/optick.config.h
-- Up-to-date: /path/to/local/lib/cmake/Optick/OptickTargets.cmake
-- Installing: /path/to/local/lib/cmake/Optick/OptickTargets-release.cmake
-- Up-to-date: /path/to/local/lib/cmake/Optick/OptickConfig.cmake
-- Up-to-date: /path/to/local/lib/cmake/Optick/OptickConfigVersion.cmake

Then all I need to do is create a CMakeLists.txt containing the following:

cmake_minimum_required(VERSION 3.22)
project(example)

find_package(Optick 1.3 REQUIRED)

add_executable(OptickTest main.cpp)
target_link_libraries(OptickTest PRIVATE Optick::OptickCore)

To build this, I just ran:

$ cmake -G "Ninja Multi-Config" -S . -B build/example -DCMAKE_PREFIX_PATH=$PWD/local
$ cmake --build build/example --config Release --verbose
[1/2] /usr/bin/c++ -DCMAKE_INTDIR=\"Release\" -isystem /path/to/local/include -O3 -DNDEBUG -MD -MT CMakeFiles/OptickTest.dir/Release/main.cpp.o -MF CMakeFiles/OptickTest.dir/Release/main.cpp.o.d -o CMakeFiles/OptickTest.dir/Release/main.cpp.o -c /path/to/main.cpp
[2/2] : && /usr/bin/c++ -O3 -DNDEBUG  CMakeFiles/OptickTest.dir/Release/main.cpp.o -o Release/OptickTest  -Wl,-rpath,/path/to/local/lib  ../../local/lib/libOptickCore.so && :
$ cmake --build build/example --config Debug --verbose
[1/2] /usr/bin/c++ -DCMAKE_INTDIR=\"Debug\" -isystem /path/to/local/include -g -MD -MT CMakeFiles/OptickTest.dir/Debug/main.cpp.o -MF CMakeFiles/OptickTest.dir/Debug/main.cpp.o.d -o CMakeFiles/OptickTest.dir/Debug/main.cpp.o -c /path/to/main.cpp
[2/2] : && /usr/bin/c++ -g  CMakeFiles/OptickTest.dir/Debug/main.cpp.o -o Debug/OptickTest  -Wl,-rpath,/path/to/local/lib  ../../local/lib/libOptickCored.so && :

I tell CMake where to find my local (rather than system) dependencies by setting CMAKE_PREFIX_PATH to the absolute path /path/to/local.

If you use optick as a git submodule, then you can wrap the various CMake invocations in either (a) a shell script/batch file or (b) an ExternalProject-based super-build.

The instructions on Windows should be nearly the same... just replace -G "Ninja Multi-Config" with -G "Visual Studio 17 2022" or leave it out to use the default from your developer command prompt.

Related