# Tutorial Step2 Exercise 2 - Adding an Option Solutions

**URL:** https://discourse.cmake.org/t/tutorial-step2-exercise-2-adding-an-option-solutions/9312
**Category:** Usage
**Created:** [October 30, 2023, 4:55pm UTC](https://discourse.cmake.org/t/tutorial-step2-exercise-2-adding-an-option-solutions/9312 "2023-10-30T16:55:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![LadyGreyTeaService](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/f07891/32.png) [@LadyGreyTeaService](https://discourse.cmake.org/u/LadyGreyTeaService)
#### Post date: [October 30, 2023, 4:55pm UTC](https://discourse.cmake.org/t/tutorial-step2-exercise-2-adding-an-option-solutions/9312/1 "2023-10-30T16:55:38Z")

</div>

Tutorial Exercise 2 - Adding an Option Solutions  
[https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Library.html#id6](https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Library.html#id6)

Should the solution for TODO 11 be

```auto
#ifndef USE_MYMATH
# include <cmath>
#endif

```

instead of just

```auto
#include <cmath>

```

as `#include <cmath>` is unnecessary when `USE_MYMATH=OFF`?

* * *

Should text just above TODO 12 read

> First, from within MathFunctions/CMakeLists.txt create a library called SqrtLibrary that has sources mysqrt.cxx.

instead of

> First, from within USE\_MYMATH create a library called SqrtLibrary that has sources mysqrt.cxx.

?

* * *

The stated objective of TODO 12 and TODO 13 is to not compile mysqrt.cxx when USE\_MYMATH=OFF.  
The solutions displayed for TODO 12 and TODO 13 are not wrapped in if(USE\_MYMATH) i.e. bare

```auto
  add_library(SqrtLibrary STATIC
              mysqrt.cxx
              )

```

and bare

```auto
  target_link_libraries(MathFunctions PRIVATE SqrtLibrary)

```

(the eagle-eyed can see from the indentation that these fragments were copied from a CMakeLists.txt where there was probably an enclosing if). But if we use these the given fragments as solutions without an enclosing ‘if’ then SqrtLibrary is always built and linked, even when USE\_MYMATH=OFF.

I would presume the correct solution is:

```auto
if(USE_MYMATH)
  add_library(SqrtLibrary STATIC
              mysqrt.cxx
              )
  target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()

```

where I see the build does not make SqrtLibrary when USE\_MYMATH=OFF.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 30, 2023, 7:08pm UTC](https://discourse.cmake.org/t/tutorial-step2-exercise-2-adding-an-option-solutions/9312/2 "2023-10-30T19:08:28Z")

</div>

Cc: @betsy.mcphail
