Context
I was going through Step 5 of the CMake tutorial, and after completing TODO items 1 through 4 to understand the installation process I noticed what I thought was some strange behavior in the generated Make buildsystem. Specifically, after running the following commands:
cmake ../Step5
cmake --build .
from my Step5_build
folder, on the first run of cmake --install . --prefix /my/install/directory
the resulting output was the following:
-- Install configuration: ""
-- Installing: /my/install/directory/lib/libMathFunctions.a
-- Installing: /my/install/directory/include/MathFunctions.h
-- Installing: /my/install/directory/bin/Tutorial
-- Installing: /my/install/directory/include/TutorialConfig.h
On the second run of cmake --install . --prefix /my/install/directory
, the output was the following:
-- Install configuration: ""
-- Installing: /my/install/directory/lib/libMathFunctions.a
-- Up-to-date: /my/install/directory/include/MathFunctions.h
-- Up-to-date: /my/install/directory/bin/Tutorial
-- Up-to-date: /my/install/directory/include/TutorialConfig.h
However, when I decided to change the following line from Step5/MathFunctions/CMakeLists.txt
:
add_library(MathFunctions mysqrt.cxx)
to this:
add_library(MathFunctions SHARED mysqrt.cxx)
after regenerating the buildsystem and re-building the targets, I got the following output on the next run of cmake --install . --prefix /my/install/directory
:
-- Install configuration: ""
-- Installing: /my/install/directory/lib/libMathFunctions.dylib
-- Up-to-date: /my/install/directory/include/MathFunctions.h
-- Installing: /my/install/directory/bin/Tutorial
-- Up-to-date: /my/install/directory/include/TutorialConfig.h
and this output on the subsequent run of cmake --install . --prefix /my/install/directory
:
-- Install configuration: ""
-- Up-to-date: /my/install/directory/lib/libMathFunctions.dylib
-- Up-to-date: /my/install/directory/include/MathFunctions.h
-- Installing: /my/install/directory/bin/Tutorial
-- Up-to-date: /my/install/directory/include/TutorialConfig.h
Question
I am wondering why making a shared library would cause it to install once and show as up-to-date on subsequent invocations of cmake --install
whereas using the default library type (which is ostensibly STATIC
on my system) would cause it to have to be installed regardless of whether cmake --install
had already been invoked.
Additionally, given the details from the context above, I would like to know why turning my library into a shared library would cause a dependent target executable to repeatedly install on each invocation of cmake --install
whereas using a static library would cause the dependent executable to install only once.
I am very new to CMake and building C/C++ projects in general, so I would greatly appreciate any clarifications that would help me clear my doubts.
My Environment
CMake: 3.26.4
OS: macOS Ventura 13.4