Το CMake είναι το πιο διαδεδομένο εργαλείο για να "χτίσεις" projects γραμμένα σε C++ για το 2021.
Το CMake δε μεταγλωτίζει το ίδιο τον κώδικα αλλά παράγει τις κατάλληλες παραμέτρους για άλλα εργαλεία (π.χ. make) τα οποία αναλαμβάνουν τη μεταγλώτισση.
Η χρήση εργαλείων όπως το CMake είναι μονόδρομος όταν ένα έργο σε C++ περιλαμβάνει πολλά αρχεία, διάφορες παραμέτρους, εξωτερικά dependencies κλπ. Σε αυτή την περίπτωση η ανάπτυξή του γίνεται εκθετικά δυσκολότερη όσο το μέγεθός του αυξάνεται, εάν δεν υιοθετηθεί χρήση εργαλείων όπως το CMake.
邏 Στο εργαστήριο θα δείξουμε πως μπορούμε να στήσουμε ένα τυπικό project γραμμένο σε C++ και θα καλύψουμε τα πιο βασικά σενάρια που χρειάζεται να γνωρίζει κάποιος όπως:
✅ Παραγωγή εκτελέσιμου αρχείου
✅ Καθορισμός του include path
✅ Δημιουργία βιβλιοθήκης για static ή dynamic linking
✅ Ελεγχος των διάφορων compilation flags
✅ Δημιουργία functions εντός του CMake
✅ Παραμετροποίηση μέσω options
5. What is CMake?
CMake is a software that manages the
way C/C++ projects are built. It has the
ability to simplify the build process of
projects with large or complex code
layouts and is compiler-agnostic.
It is considered the de facto standard for
building C/C++ projects.
6. When is a build
system
necessary?
● Avoid hard-coding paths
● Build a package on more than one
computer
● Support multiple operating systems and
compilers
● Describe how your program is
structured logically, not flags and
commands
Source: An Introduction to Modern CMake
7. Why do you
need to learn
CMake?
● 55 to 80% of C++ developers use it
● The vast majority professional C++
projects use it
● Cross-platform
● Well-supported by IDEs
● Relatively easy to get started with
● Makefiles and building with the command
line simply do not scale
9. Create binary with command line
// src/main.cpp
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
$ g++ src/main.cpp
$ ./a.out
10. Create binary with CMake
cmake_minimum_required(VERSION 3.12)
project(IntroToCmake)
add_executable(hello_world src/main.cpp)
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ./hello_world
11. Generated build/Makefile
# Default target executed when no arguments are given to make.
default_target: all
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/me/projects/intro-to-cmake/build/CMakeFiles
/home/me/projects/intro-to-cmake/build//CMakeFiles/progress.marks
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/me/projects/intro-to-cmake/build/CMakeFiles 0
# Build rule for target.
hello_world: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 hello_world
18. libgreeter.a (static library)
add_library(greeter src/greeter.cpp)
target_include_directories(greeter PUBLIC include)
add_executable(hello_world src/main.cpp)
target_link_libraries(hello_world PUBLIC greeter)
$ make greeter
$ ls -l libgreeter.a
> -rw-r--r-- 1 me me 3088 libgreeter.a
$ make hello_world
$ ldd
19. libgreeter.so (dynamic library)
add_library(greeter SHARED src/greeter.cpp)
target_include_directories(greeter PUBLIC include)
add_executable(hello_world src/main.cpp)
target_link_libraries(hello_world PUBLIC greeter)
$ make greeter
$ ls -l libgreeter.so
-rw-r--r-- 1 me me 16856 libgreeter.so
$ make hello_world
$ ldd hello_world
libgreeter.so =>
/home/me/intro-to-cmake/build/libgreeter.so
27. Customize your build steps
function(configure_test testExecutable)
# Link against gtest library
target_link_libraries(${testExecutable}
gtest gtest_main gmock_main)
# Disable variadic macro warnings
target_compile_options(${testExecutable}
PRIVATE -Wno-gnu-zero-variadic-macro-arguments)
# Create test name as the capitalized form
string(TOUPPER ${testExecutable} testName)
# Add executable to test suite
add_test(${testName} ${testExecutable}
${GTEST_RUN_FLAGS})
endfunction(configure_test)
add_executable(dummy_test DummyTest.cpp)
configure_test(dummy_test)
28. Takeaways
● We covered only the surface
● Easy things are simple with CMake
● Integration with custom build systems or other
libraries is where the trickery begins
● Start using CMake (or equivalent, e.g. Bazel)
even for personal projects
29.
30. Let's keep in touch!
https://www.linkedin.com/in/platisd/
dimitris@platis.solutions
@PlatisSolutions
31. JetBrains lottery
1 year license for any Jetbrains IDE!
1. Go to: http://plat.is/jetbrains
2. If you won, please stick around until I
contact you
3. If you did not win, better luck next time!
Did you know that as a university student you
can get a free JetBrains license anyway?