2.1.4. CMake build with Shared Library

Now, let’s extend CMake build with library to make a Shared Library and use that.

Updated CMakeLists.txt for that purpose would look like this.

 1CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
 2
 3# Top level project / solution.
 4PROJECT(greeting)
 5
 6# Create Enlglish library
 7ADD_LIBRARY(
 8    greeting_en_lib SHARED
 9    greeting_en.c
10)
11
12# Create French library
13ADD_LIBRARY(
14    greeting_fr_lib SHARED
15    greeting_fr.c
16)
17
18# Create Spanish library
19ADD_LIBRARY(
20    greeting_es_lib SHARED
21    greeting_es.c
22)
23
24# Declare English Executable
25ADD_EXECUTABLE(
26    greeting_en
27    main.c
28)
29# Ensure English Executable links to English Library.
30TARGET_LINK_LIBRARIES(greeting_en greeting_en_lib)
31
32# Declare French Executable
33ADD_EXECUTABLE(
34    greeting_fr
35    main.c
36)
37# Ensure French Executable links to French Library.
38TARGET_LINK_LIBRARIES(greeting_fr greeting_fr_lib)
39
40# Declare Spanish Executable
41ADD_EXECUTABLE(
42    greeting_es
43    main.c
44)
45# Ensure Spanish Executable links to Spanish Library.
46TARGET_LINK_LIBRARIES(greeting_es greeting_es_lib)

To compile that file with GCC, the command would be.

mkdir _build
cd _build
cmake .. -G "Unix Makefiles"

When the above command completes successfully, the compilation output would be:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0220_cmake_shared_lib/_build

Tree structure of generated file would be:

_build/
|-- CMakeCache.txt
|-- cmake_install.cmake
|-- greeting_en*
|-- greeting_es*
|-- greeting_fr*
|-- libgreeting_en_lib.so*
|-- libgreeting_es_lib.so*
|-- libgreeting_fr_lib.so*
|-- Makefile
`-- CMakeFiles/
    |-- cmake.check_cache
    |-- CMakeDirectoryInformation.cmake
    |-- CMakeOutput.log
    |-- Makefile2
    |-- Makefile.cmake
    |-- progress.marks
    |-- TargetDirectories.txt
    |-- 3.22.1/
    |   |-- CMakeCCompiler.cmake
    |   |-- CMakeCXXCompiler.cmake
    |   |-- CMakeDetermineCompilerABI_C.bin*
    |   |-- CMakeDetermineCompilerABI_CXX.bin*
    |   |-- CMakeSystem.cmake
    |   |-- CompilerIdC/
    |   |   |-- a.out*
    |   |   |-- CMakeCCompilerId.c
    |   |   `-- tmp/
    |   `-- CompilerIdCXX/
    |       |-- a.out*
    |       |-- CMakeCXXCompilerId.cpp
    |       `-- tmp/
    |-- CMakeTmp/
    |-- greeting_en.dir/
    |   |-- build.make
    |   |-- cmake_clean.cmake
    |   |-- compiler_depend.make
    |   |-- compiler_depend.ts
    |   |-- DependInfo.cmake
    |   |-- depend.make
    |   |-- flags.make
    |   |-- link.txt
    |   |-- main.c.o
    |   |-- main.c.o.d
    |   `-- progress.make
    |-- greeting_en_lib.dir/
    |   |-- build.make
    |   |-- cmake_clean.cmake
    |   |-- compiler_depend.make
    |   |-- compiler_depend.ts
    |   |-- DependInfo.cmake
    |   |-- depend.make
    |   |-- flags.make
    |   |-- greeting_en.c.o
    |   |-- greeting_en.c.o.d
    |   |-- link.txt
    |   `-- progress.make
    |-- greeting_es.dir/
    |   |-- build.make
    |   |-- cmake_clean.cmake
    |   |-- compiler_depend.make
    |   |-- compiler_depend.ts
    |   |-- DependInfo.cmake
    |   |-- depend.make
    |   |-- flags.make
    |   |-- link.txt
    |   |-- main.c.o
    |   |-- main.c.o.d
    |   `-- progress.make
    |-- greeting_es_lib.dir/
    |   |-- build.make
    |   |-- cmake_clean.cmake
    |   |-- compiler_depend.make
    |   |-- compiler_depend.ts
    |   |-- DependInfo.cmake
    |   |-- depend.make
    |   |-- flags.make
    |   |-- greeting_es.c.o
    |   |-- greeting_es.c.o.d
    |   |-- link.txt
    |   `-- progress.make
    |-- greeting_fr.dir/
    |   |-- build.make
    |   |-- cmake_clean.cmake
    |   |-- compiler_depend.make
    |   |-- compiler_depend.ts
    |   |-- DependInfo.cmake
    |   |-- depend.make
    |   |-- flags.make
    |   |-- link.txt
    |   |-- main.c.o
    |   |-- main.c.o.d
    |   `-- progress.make
    `-- greeting_fr_lib.dir/
        |-- build.make
        |-- cmake_clean.cmake
        |-- compiler_depend.make
        |-- compiler_depend.ts
        |-- DependInfo.cmake
        |-- depend.make
        |-- flags.make
        |-- greeting_fr.c.o
        |-- greeting_fr.c.o.d
        |-- link.txt
        `-- progress.make

13 directories, 91 files

If we just diff this with CMake build with library, here are the changes

7,9c7,9
< |-- libgreeting_en_lib.a
< |-- libgreeting_es_lib.a
< |-- libgreeting_fr_lib.a
---
> |-- libgreeting_en_lib.so*
> |-- libgreeting_es_lib.so*
> |-- libgreeting_fr_lib.so*
49d48
<     |   |-- cmake_clean_target.cmake
74d72
<     |   |-- cmake_clean_target.cmake
99d96
<         |-- cmake_clean_target.cmake
110c107
< 13 directories, 94 files
---
> 13 directories, 91 files