cmake Getting started with cmake "Hello World" as a library


This example shows how to deploy the "Hello World" program as a library and how to link it with other targets.

Say we have the same set of source/header files as in the example. Instead of building from multiple source files, we can first deploy foo.cpp as a library by using add_library() and afterwards linking it with the main program with target_link_libraries().

We modify CMakeLists.txt to

cmake_minimum_required(VERSION 2.4)


add_library(applib foo.cpp)
add_executable(app main.cpp)
target_link_libraries(app applib)

and following the same steps, we'll get the same result.