Giter Site home page Giter Site logo

modern-cmake-for-cpp's Introduction

Modern CMake for C++

This is the code repository for Modern CMake for C++, published by Packt.

Discover a better approach to building, testing, and packaging your software

What is this book about?

Creating top-notch software is an extremely difficult undertaking. Developers researching the subject have difficulty determining which advice is up-to-date and which approaches have already been replaced by easier, better practices. At the same time, most online resources offer limited explanation, while also lacking the proper context and structure.

This book covers the following exciting features:

  • Understand best practices for building C++ code
  • Gain practical knowledge of the CMake language by focusing on the most useful aspects
  • Use cutting-edge tooling to guarantee code quality with the help of tests and static and dynamic analysis
  • Discover how to manage, discover, download, and link dependencies with CMake
  • Build solutions that can be reused and maintained in the long term
  • Understand how to optimize build artifacts and the build process itself

If you feel this book is for you, get your copy today!

Instructions and Navigations

All of the code is organized into folders. For example, Chapter02.

The code will look like the following:

function(Inner)
  message("  > Inner: ${V}")
  set(V 3)
  message("  < Inner: ${V}")
endfunction()

Following is what you need for this book: The book is for build engineers and software developers with knowledge of C/C++ programming who are looking to learn CMake to automate the process of building small and large software solutions. If you are someone who's just getting started with CMake, a long-time GNU Make user, or simply looking to brush up on the latest best practices, this book is for you.

With the following software and hardware list you can run all code files present in the book (Chapter 1-13).

Software and Hardware List

Chapter Software/Hardware required OS required
1-13 Cmake 3.20 Windows, Mac OS X, and Linux

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. Click here to download it.

Errata

  • Page 46 (Paragraph 1, line 1): Multiline comments get their name from their symbol – they start with an opening square bracket ([) ... should be Multiline comments get their name from their symbol – they start with an # and an opening square bracket (#[) ...

  • Page 75 (line 10): main() should be endmacro()

  • Page 107 (Paragraph 2, line 14 and 15): Calling test_run() isn't very complicated at all. We start by setting the required standard, after which we call test_run() and print the collected information to the user: should be Calling try_run() isn't very complicated at all. We start by setting the required standard, after which we call try_run() and print the collected information to the user:

  • Page 137 (Paragraph 1, line 1): generation expression should be generator expression

  • Page 168 (Paragraph 5, line 19): • -finline-functions-called-once: GCC only • -finline-functions: Clang and GCC • -finline-hint-functions: Clang only • -finline-functions-called-once: GCC only should be • -finline-functions-called-once: GCC only • -finline-functions: Clang and GCC • -finline-hint-functions: Clang only

  • Page 195 (line 12): chapter06/02-odr-fail/two.cpp should be chapter06/02-odr-fail/CMakeLists.txt

  • Page 198 (third code block): add_library( SHARED [...]) should be add_library( [...])

  • Page 208 (line 13): • Discovering legacy packages with FindPkgConfig0 should be • Discovering legacy packages with FindPkgConfig

  • Page 213 (line 11): <PKG_NAME>_LIBRARIES or <PKG_NAME>_LIBRARIES or <PKG_NAME>_LIBS should be <PKG_NAME>_LIBRARIES or <PKG_NAME>_LIBS

  • Page 222 (line 9): # deliberately used in mind-module against the documentation should be # deliberately used in find-module against the documentation

  • Page 222 (Paragraph 2,line 16): If a matching file is found, its path will be stored in the PQXX_LIBRARY_PATH variable. Otherwise, the variable will be set to -NOTFOUND, or PQXX_HEADER_PATH-NOTFOUND in this case. should be If a matching file is found, its path will be stored in the PQXX_LIBRARY_PATH variable. Otherwise, the variable will be set to -NOTFOUND, or PQXX_LIBRARY_PATH-NOTFOUND in this case.

  • Page 231 (line 7): Downloading the step options should be The downloading step options

  • Page 265 (Paragraph 1,line 3): shave should be save

  • Page 265 (Paragraph 2,line 14): allow should be disallow

  • Page 354 (Paragraph 3,line 31): install(FILES) should be configure_package_config_file

  • Page 362 (Paragraph 1,line 3): ...but there are also are procedural steps... should be ...but there are also procedural steps...

  • Page 384 (line 28): calc_command should be calc_console

  • Page 395 (Paragraph 1,line 5): CPack should be CTest

Related products

Get to Know the Author

Rafał Świdziński works as a staff engineer at Google. With over 10 years of professional experience as a full stack developer, he has been able to experiment with a vast multitude of programming languages and technologies. During this time, he has been building software under his own company and for corporations including Cisco Meraki, Amazon, and Ericsson. Originally from Łódź, Poland, he now lives in London, UK, from where he runs a YouTube channel, “Smok,” discussing topics of software development. He tackles technical problems, including real-life and work-related challenges encountered by many people in the field. Throughout his work, he explains the technical concepts in detail and demystifies the art of being a software engineer. His primary focus is on high-quality code and the craftsmanship of programming.

Download a free PDF

If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.

https://packt.link/free-ebook/9781801070058

modern-cmake-for-cpp's People

Contributors

divyavijayan123 avatar drakemor avatar josepha-packt avatar jubit-packt avatar keagancarneiro avatar packt-itservice avatar packtutkarshr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

modern-cmake-for-cpp's Issues

False statement on page 64, "The evaluation of a string and a variable"

Directly from the book

    set(VAR1 FALSE)
    set(VAR2 "VAR1")
    if(${VAR2})

The if() condition works in a bit of a convoluted way here – first, it will evaluate ${VAR2} to VAR1, which is a recognized variable, and this in turn is evaluated to the FALSE string.

Correction: ${VAR2} is evaluated to "VAR1" which is not a variable name. I checked it out in CLion CMake debugger.

Debugger:
1

Result:
2

Evaluation of conditional expressions

Chapter 2, Understanding control structures in CMake, Conditional blocks, The evaluation of a string and a variable:

set(VAR1 FALSE)
set(VAR2 "VAR1")
if(${VAR2})

The if() condition works in a bit of a convoluted way here – first, it will evaluate
${VAR2} to VAR1, which is a recognized variable, and this in turn is evaluated to the
FALSE string.

The CMake manual, on the other hand, states:

if(<variable>)

True if given a variable that is defined to a value that is not a false constant. False otherwise, including if the variable is undefined.

Nothing about checking if the value matches any of the visible variable names, to evaluate it in turn.

So could you clarify, what exactly did you mean here?

Thanks!

Chapter 05 configure.cpp incorrect.

The book is correct, but the configure.spp that is in this project is not. You show in the book:

cout << "FOO_ENABLE1: " << xstr(FOO_STRING1) << endl;
cout << "FOO_ENABLE2: " << xstr(FOO_STRING2) << endl;

This works. However in the code here you have:
cout << "FOO_ENABLE1: " << xstr(FOO_ENABLE1) << endl;
cout << "FOO_ENABLE2: " << xstr(FOO_ENABLE2) << endl;

This does not work.

How does cmake handle interpolation

at
“Chapter 02”
“Working with variable“
"Variable references"

set(MyInner "Hello")
set(MyOuter "${My")
message("${MyOuter}Inner} World")

The above code cannot be run:

when parsing string
  ${My
There is an unterminated variable reference.

using cmake 3.20.0 also 3.26.0

chapter08/06-coverage fail

When I follow 06-coverage example. it fails. Here are my steps:

cmake -B build -DCMAKE_BUILD_TYPE=Debug
06-coverage$ cmake --build build/ --clean-first -t coverage
[  7%] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 14%] Linking CXX static library ../../../lib/libgtestd.a
[ 14%] Built target gtest
[ 21%] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 28%] Linking CXX static library ../../../lib/libgtest_maind.a
[ 28%] Built target gtest_main
[ 35%] Building CXX object _deps/googletest-build/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 42%] Linking CXX static library ../../../lib/libgmockd.a
[ 42%] Built target gmock
[ 50%] Building CXX object bin/CMakeFiles/sut.dir/calc.cpp.o
[ 57%] Building CXX object bin/CMakeFiles/sut.dir/run.cpp.o
[ 64%] Building CXX object bin/CMakeFiles/sut.dir/rng_mt19937.cpp.o
[ 71%] Linking CXX static library libsut.a
[ 71%] Built target sut
[ 78%] Building CXX object test/CMakeFiles/unit_tests.dir/calc_test.cpp.o
[ 85%] Building CXX object test/CMakeFiles/unit_tests.dir/run_test.cpp.o
[ 92%] Linking CXX executable unit_tests
[ 92%] Built target unit_tests
[100%] Running coverage for unit_tests...
Deleting all .da files in . and subdirectories
Done.
Running main() from /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/_deps/googletest-src/googletest/src/gtest_main.cc
[==========] Running 4 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 3 tests from CalcTestSuite
[ RUN      ] CalcTestSuite.SumAddsTwoInts
[       OK ] CalcTestSuite.SumAddsTwoInts (0 ms)
[ RUN      ] CalcTestSuite.MultiplyMultipliesTwoInts
[       OK ] CalcTestSuite.MultiplyMultipliesTwoInts (0 ms)
[ RUN      ] CalcTestSuite.AddRandomNumberAddsThree
[       OK ] CalcTestSuite.AddRandomNumberAddsThree (0 ms)
[----------] 3 tests from CalcTestSuite (0 ms total)

[----------] 1 test from RunTest
[ RUN      ] RunTest.RunOutputsCorrectEquations
[       OK ] RunTest.RunOutputsCorrectEquations (0 ms)
[----------] 1 test from RunTest (0 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 2 test suites ran. (0 ms total)
[  PASSED  ] 4 tests.
Capturing coverage data from .
Found gcov version: 7.5.0
Scanning . for .gcda files ...
Found 3 data files in .
Processing sut.dir/calc.cpp.gcda
geninfo: WARNING: /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/calc.cpp.gcno: Overlong record at end of file!
/home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/calc.cpp.gcno:version 'A84*', prefer 'A75*'
/home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/calc.cpp.gcno:no functions found
geninfo: WARNING: gcov did not create any files for /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/calc.cpp.gcda!
Processing sut.dir/run.cpp.gcda
geninfo: WARNING: /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/run.cpp.gcno: Overlong record at end of file!
/home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/run.cpp.gcno:version 'A84*', prefer 'A75*'
/home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/run.cpp.gcno:no functions found
geninfo: WARNING: gcov did not create any files for /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/run.cpp.gcda!
Processing sut.dir/rng_mt19937.cpp.gcda
geninfo: WARNING: /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/rng_mt19937.cpp.gcno: Overlong record at end of file!
/home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/rng_mt19937.cpp.gcno:version 'A84*', prefer 'A75*'
/home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/rng_mt19937.cpp.gcno:no functions found
geninfo: WARNING: gcov did not create any files for /home/lili/codes/Modern-CMake-for-Cpp/examples/chapter08/06-coverage/build/bin/CMakeFiles/sut.dir/rng_mt19937.cpp.gcda!
Finished .info-file creation
Reading tracefile coverage.info
lcov: ERROR: no valid records found in tracefile coverage.info
test/CMakeFiles/coverage.dir/build.make:70: recipe for target 'test/CMakeFiles/coverage' failed
make[3]: *** [test/CMakeFiles/coverage] Error 255
CMakeFiles/Makefile2:252: recipe for target 'test/CMakeFiles/coverage.dir/all' failed
make[2]: *** [test/CMakeFiles/coverage.dir/all] Error 2
CMakeFiles/Makefile2:259: recipe for target 'test/CMakeFiles/coverage.dir/rule' failed
make[1]: *** [test/CMakeFiles/coverage.dir/rule] Error 2
Makefile:218: recipe for target 'coverage' failed
make: *** [coverage] Error 2

I am using cmake cmake-3.27.7 in ubuntu 18.04

Chapter 07 Example 02 doesn't work.

Inside the folder Modern-CMake-for-Cpp/examples/chapter07/01-find-package-variables I run the following commands:

cmake -S . -B bld

This yields:

-- The CXX compiler identification is GNU 9.4.0
-- 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
-- Found Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread (found version "3.6.1") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Desktop/Modern-CMake-for-Cpp/examples/chapter07/01-find-package-variables/bld

then, I run

cmake --build bld

and I get

cmake --build bld
CMakeFiles/main.dir/build.make:73: *** target pattern contains no '%'.  Stop.
make[1]: *** [CMakeFiles/Makefile2:82: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

All other examples worked fine until now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.