There is more

Let us now show the use of the object library functionality made available in CMake. We will use the same source files, but modify CMakeLists.txt:

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-03 LANGUAGES CXX)add_library(message-objs  OBJECT    Message.hpp    Message.cpp  )# this is only needed for older compilers# but doesn't hurt either to have itset_target_properties(message-objs  PROPERTIES    POSITION_INDEPENDENT_CODE 1  )add_library(message-shared  SHARED    $<TARGET_OBJECTS:message-objs>  )add_library(message-static  STATIC    $<TARGET_OBJECTS:message-objs>  )add_executable(hello-world hello-world.cpp)target_link_libraries(hello-world message-static)

First, notice that the add_library command changed to add_library(message-objs ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.