2.1.2. CMake build with library¶
Now, let’s extend Basic build with CMake to make a Library and use that for linking.
Updated CMakeLists.txt
for that purpose would look like this.
1CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
2
3PROJECT(greeting)
4
5ADD_LIBRARY(
6 greeting_en_lib
7 greeting_en.c
8)
9ADD_LIBRARY(
10 greeting_fr_lib
11 greeting_fr.c
12)
13ADD_LIBRARY(
14 greeting_es_lib
15 greeting_es.c
16)
17
18ADD_EXECUTABLE(
19 greeting_en
20 main.c
21)
22TARGET_LINK_LIBRARIES(greeting_en greeting_en_lib)
23
24ADD_EXECUTABLE(
25 greeting_fr
26 main.c
27)
28TARGET_LINK_LIBRARIES(greeting_fr greeting_fr_lib)
29
30ADD_EXECUTABLE(
31 greeting_es
32 main.c
33)
34TARGET_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/0210_cmake_lib/_build
Tree structure of generated file would be:
_build/
|-- CMakeCache.txt
|-- cmake_install.cmake
|-- greeting_en*
|-- greeting_es*
|-- greeting_fr*
|-- libgreeting_en_lib.a
|-- libgreeting_es_lib.a
|-- libgreeting_fr_lib.a
|-- 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
| |-- cmake_clean_target.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
| |-- cmake_clean_target.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
|-- cmake_clean_target.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, 94 files
If we just diff this with Basic build with CMake, here are the changes
6a7,9
> |-- libgreeting_en_lib.a
> |-- libgreeting_es_lib.a
> |-- libgreeting_fr_lib.a
38a42,54
> | |-- link.txt
> | |-- main.c.o
> | |-- main.c.o.d
> | `-- progress.make
> |-- greeting_en_lib.dir/
> | |-- build.make
> | |-- cmake_clean.cmake
> | |-- cmake_clean_target.cmake
> | |-- compiler_depend.make
> | |-- compiler_depend.ts
> | |-- DependInfo.cmake
> | |-- depend.make
> | |-- flags.make
41a58,67
> | `-- progress.make
> |-- greeting_es.dir/
> | |-- build.make
> | |-- cmake_clean.cmake
> | |-- compiler_depend.make
> | |-- compiler_depend.ts
> | |-- DependInfo.cmake
> | |-- depend.make
> | |-- flags.make
> | |-- link.txt
45c71
< |-- greeting_es.dir/
---
> |-- greeting_es_lib.dir/
47a74
> | |-- cmake_clean_target.cmake
55a83,92
> | `-- progress.make
> |-- greeting_fr.dir/
> | |-- build.make
> | |-- cmake_clean.cmake
> | |-- compiler_depend.make
> | |-- compiler_depend.ts
> | |-- DependInfo.cmake
> | |-- depend.make
> | |-- flags.make
> | |-- link.txt
59c96
< `-- greeting_fr.dir/
---
> `-- greeting_fr_lib.dir/
61a99
> |-- cmake_clean_target.cmake
70,71d107
< |-- main.c.o
< |-- main.c.o.d
74c110
< 10 directories, 61 files
---
> 13 directories, 94 files