Skip to content

Chapter 1 Lab

David Zemon edited this page Jul 18, 2018 · 8 revisions

Introduction

We're going to add a third library to the project which will be an alternative to upper. The code for useful will switch between using upper and this new library based on a preprocessor definition (#ifdef). We'll also take the time to refactor our growing project's build system into smaller and more modular pieces.

Instructions

  1. Read about the add_subdirectory(...) command and segregate the relevant CMake scripting code into separate CMakeLists.txt files, one in each of the project's subdirectories.
    • This is also an opportune time to create a new directory for the main executable, containing main.cpp and its own CMakeLists.txt file.
    • Global code, such as the invocation of the project() command, can and should be left in the primary CMakeLists.txt script at the root of the project.
  2. Create a new library (in a new child directory) named lower, containing a single source file and an appropriate header in the common include directory.
  3. Implement a to_lower(...) function, similar to upper's to_upper(...) function.
  4. Using CMake, set a preprocessor definition, LOWER, on the lower library such that any dependent targets of lower also inherit that definition.
  5. Use the same CMake/preprocessor technique to define an UPPER definition on the upper library and its dependents
  6. Add a CMake option that allows the user to easily switch between three different implementations for useless:
    1. Original case: useless is dependent on neither upper nor lower.
    2. Uppercase: useless is dependent on upper.
    3. Lowercase: useless is dependent on lower.
  7. Utilize the preprocessor definition(s) set above in the useless code to switch between the three implementations.
  8. Run your code with all three options to ensure it works as expected. You may also wish to check your dependency diagrams as generated by Graphviz.
Clone this wiki locally