Skip to content

Chapter 2 Lab

David Zemon edited this page Jul 6, 2018 · 3 revisions

Introduction

It's time to get a bit more in-depth with CMake's scripting syntax. It isn't complex, but there are some intricacies as well as many special functions. We'll explore many of them in this lab.

Instructions

Part 1

  1. Replace the four separate invocations of add_subdirectory(...) with a for-loop that iterates over a list of subdirectories. (Note that I do not necessarily recommend doing this in practice - it simply works as a good way to practice CMake's for-loops).
  2. Using a generator expression, add a preprocessor definition that will enable a new log statement if and only if the CMake build type is Debug:
    • Add a log statement such as std::cout << "[Upper] Finished for-loop" << std::endl; after the for-loop in the definition of upper::to_upper(...).
    • CMake build type == Debug
    • Preprocessor macro == TRACE=1
    • Only the source code for the upper library should use this macro (it should not be inherited by any other targets)
  3. Confirm with VERBOSE=1 that your new preprocessor macro is only utilized in the debug build and only in upper.cpp, not in release or any other files.

Part 2

In this section you're going to build a special Make target to generate a complete package for your customer using your own custom CMake function and a few built-in functions.

  1. Shared libraries are good, right? Switch all of the libraries in this project to be dynamically rather than statically linked.
  2. Using any combination(s) of add_custom_target or add_custom_command that you like, "install" the necessary files into a staging directory within the project's binary directory. Those files should include:
    • The HelloWorld executable
    • All three shared libraries
    • Headers from the project's include directory
  3. Create a new target named my_install (you'll see that install is a reserved target name, and we'll learn about it later) via the add_custom_target function.

Note that this is a very open-ended lab. You'll want to familiarize yourself with CMake's -E argument and its various options (hint: see this help page). You'll also need to become familiar with various CMake functions that may or may not have been explicitly taught in the slides. Use of the install() function is FORBIDDEN for this lab. The install() function will be taught next and this lab will help familiarize yourself with CMake syntax, docs, and miscellaneous functions. Remember that you want your app to be cross-platform, so do not use system utilities like cp or install and be sure to make use of generator expressions rather than relying on hard-coded filenames for the executable and libraries.

Clone this wiki locally