Different Linking Methods for CMake Libraries

1. Different Libraries Have Different Linking Methods

OpenCV uses ${OpenCV_LIBS}

target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

protobuf uses ${PROTOBUF_LIBRARIES}

target_link_libraries(${PROJECT_NAME} ${PROTOBUF_LIBRARIES})

jpeg-turbo uses the target name libjpeg-turbo::turbojpeg-static

target_link_libraries(${PROJECT_NAME} libjpeg-turbo::turbojpeg-static)

2. How to Determine Which Method to Use

Generally, the following methods can be used:

  1. Check the CMake file for usage instructions. (Well-written CMake files usually include usage instructions)
  2. Look for how others are using it online
  3. Search the CMake files for relevant variables, such as checking if OpenCV’s CMake files contain the “OpenCV_LIBS” variable
  4. Check the relevant target names in the CMake files

2.1 Check the CMake File for Usage Instructions

For example, OpenCV has usage instructions at the top of the OpenCVConfig.cmake file as follows:

Different Linking Methods for CMake Libraries

2.2 Search CMake Files for Relevant Variables

For example, protobuf can find the PROTOBUF_LIBRARIES variable in protobuf-module.cmake Different Linking Methods for CMake Libraries

2.3 Check the Relevant Target Names in the CMake Files

For example, jpeg-turbo can find the relevant target in the libjpeg-turboTargets.cmake file: libjpeg-turbo::turbojpeg-static Different Linking Methods for CMake Libraries

How does everyone determine this?

Leave a Comment