# CMake Tutorial Step 2 Exercise 1 build failed

**URL:** https://discourse.cmake.org/t/cmake-tutorial-step-2-exercise-1-build-failed/13209
**Category:** Usage
**Created:** [December 16, 2024, 2:55am UTC](https://discourse.cmake.org/t/cmake-tutorial-step-2-exercise-1-build-failed/13209 "2024-12-16T02:55:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Travert](https://discourse.cmake.org/user_avatar/discourse.cmake.org/travert/32/5197_2.png) [@Travert](https://discourse.cmake.org/u/Travert)
#### Post date: [December 16, 2024, 2:55am UTC](https://discourse.cmake.org/t/cmake-tutorial-step-2-exercise-1-build-failed/13209/1 "2024-12-16T02:55:34Z")

</div>

Hello, just started working with CMake and was going through the tutorial fine until step 2 exercise 1, Creating a library. I completed all the TODOs but when I go to build the solution, I get an error:

`[project path]Step2\tutorial.cpp(8,10): error C1083: Cannot open include file: 'MathFunctions.h': No such file or directory [[project path]\Step2\Step2_build\Tutorial.vcxproj]`.

I am using powershell to run the commands and even tried it with the exact same commands and file, yet I’m still getting an error.

Here is what I have added for top level “CMakeLists.txt”:

```auto
// ...omitted code...
# TODO 2: Use add_subdirectory() to add MathFunctions to this project
add_subdirectory(MathFunctions)

# add the executable
add_executable(Tutorial tutorial.cpp)

# TODO 3: Use target_link_libraries to link the library to our executable
target_link_libraries(Tutorial PUBLIC MathFunctions)

# TODO 4: Add MathFunctions to Tutorial's target_include_directories()
# Hint: ${PROJECT_SOURCE_DIR} is a path to the project source. AKA This folder!

# add the binary tree to the search path for include files to find TutorialConfig.h
target_include_directories(Tutorial PUBLIC 
	"${PROJECT_BINARY_DIR}"
	"${PROJECT_BINARY_DIR}/MathFunctions"
)

```

Here is what I have in “MathFunctions/CMakeLists.txt”:

```auto
add_library(MathFunctions
	MathFunctions.cpp
	mysqrt.cpp
)

```

My tutorial.cpp also has the “MathFunctions.h” include.

I also noticed when opening up the solution in Visual Studio that MathFunctions is setup as another project in the solution. Is that supposed to be correct? What am I missing that causes the error?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [December 16, 2024, 5:52am UTC](https://discourse.cmake.org/t/cmake-tutorial-step-2-exercise-1-build-failed/13209/2 "2024-12-16T05:52:39Z")

</div>

> [@Travert](#):
>
> ```auto
> # set the project name and version
> project(Tutorial VERSION 1.0)
> 
> # ...
> 
> # use of this project name:
> target_include_directories(Tutorial PUBLIC 
> "${PROJECT_BINARY_DIR}"
> "${PROJECT_BINARY_DIR}/MathFunctions"
> )
> 
> ```

If you change the directory structure, you have to change this too:

```cmake
target_include_directories(Tutorial PUBLIC 
	"${CMAKE_CURRENT_SOURCE_DIR}"
	"${CMAKE_CURRENT_SOURCE_DIR}/MathFunctions"
)

```

P.S.: this is not part of Step 2?

---

<div class="post-metadata">

### Author: ![Travert](https://discourse.cmake.org/user_avatar/discourse.cmake.org/travert/32/5197_2.png) [@Travert](https://discourse.cmake.org/u/Travert)
#### Post date: [December 16, 2024, 5:58pm UTC](https://discourse.cmake.org/t/cmake-tutorial-step-2-exercise-1-build-failed/13209/3 "2024-12-16T17:58:27Z")

</div>

Thank you for your suggestion! I tried using your suggestion but it still had the same error, but after looking at the steps again, and some sleep, I noticed where I went wrong and just had to change the value of the second path from `"${PROJECT_BINARY_DIR}/MathFunctions"` to `"${PROJECT_SOURCE_DIR}/MathFunctions"`.

They did have it as a hint and as the solution but I guess I was too tired and tunnel visioned to see it lol.

Here is the change:

```auto
// Old code
# add the binary tree to the search path for include files to find TutorialConfig.h
target_include_directories(Tutorial PUBLIC 
	"${PROJECT_BINARY_DIR}"
	"${PROJECT_BINARY_DIR}/MathFunctions"
)

```

```auto
// New code
# add the binary tree to the search path for include files to find TutorialConfig.h
target_include_directories(Tutorial PUBLIC 
	"${PROJECT_BINARY_DIR}"
	"${PROJECT_SOURCE_DIR}/MathFunctions"
)

```
