# Error during the adding library for learning

**URL:** <https://discourse.cmake.org/t/error-during-the-adding-library-for-learning/15406>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [December 16, 2025, 5:19am UTC](https://discourse.cmake.org/t/error-during-the-adding-library-for-learning/15406 "2025-12-16T05:19:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhruv-dhiman122](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dhruv-dhiman122/32/5948_2.png) [@dhruv-dhiman122](https://discourse.cmake.org/u/dhruv-dhiman122)\
**Post date:** [December 16, 2025, 5:19am UTC](https://discourse.cmake.org/t/error-during-the-adding-library-for-learning/15406/1 "2025-12-16T05:19:58Z")

</div>

So i am new to learning CMake language.

```cmake
cmake_minimum_required(VERSION 3.31)

project(cmake)

add_executable(target_machine)

add_library(MathFunctions CMake/Help/guide/tutorial/Step1/MathFunctions/Mathfunctions.cxx
                          CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h)

target_sources(target_machine
    PRIVATE
        CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx
    PUBLIC
        FILE_SET HEADERS
        BASE_DIRS
           MathFunctions
        FILES
            CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx
            CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h)

```

So following the tutorial in github repo i was doing todo 5 and 6 from step 1. And i came across this problem in adding the library. The reason i am having absolute path is due to CML file is in root dict of the learning dict on my Fedora OS. I did see the docs for add\_library() which said me to add the source but still i am ending up with some error. If any one can help with this it will be great help

---

<div class="post-metadata">

**Author:** ![Andrej730](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andrej730/32/5752_2.png) [@Andrej730](https://discourse.cmake.org/u/Andrej730)\
**Post date:** [December 16, 2025, 8:38am UTC](https://discourse.cmake.org/t/error-during-the-adding-library-for-learning/15406/2 "2025-12-16T08:38:10Z")

</div>

What error do you get?

From the glance I see that `CMake/Help/guide/tutorial/Step1/MathFunctions/Mathfunctions.cxx` is uncluded 3 times for some reason - once as a source in `add_library`, then as a private source in `target_sources` and then as a public source but with incorrect `FILE_SET` type (`HEADERS`, though it’s not a header).

---

<div class="post-metadata">

**Author:** ![dhruv-dhiman122](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dhruv-dhiman122/32/5948_2.png) [@dhruv-dhiman122](https://discourse.cmake.org/u/dhruv-dhiman122)\
**Post date:** [December 16, 2025, 12:45pm UTC](https://discourse.cmake.org/t/error-during-the-adding-library-for-learning/15406/3 "2025-12-16T12:45:56Z")

</div>

```bash
-- Configuring done (0.0s)
CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

CMake Error at CMakeLists.txt:10 (target_sources):
  File:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h

  must be in one of the file set's base directories:

    /home/dhruvdhiman/Desktop/code/learn/embedded/cmake/MathFunctions

-- Generating done (0.1s)
CMake Generate step failed. Build files cannot be regenerated correctly.
gmake: *** [Makefile:204: cmake_check_build_system] Error 1

```

This is the error message i keep getting back. And if possible could pin point line for me to correct them. Thinking about i shouldnt have set `FILE_SET ` as HEADER, while still having to include source file

---

<div class="post-metadata">

**Author:** ![Andrej730](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andrej730/32/5752_2.png) [@Andrej730](https://discourse.cmake.org/u/Andrej730)\
**Post date:** [December 16, 2025, 2:52pm UTC](https://discourse.cmake.org/t/error-during-the-adding-library-for-learning/15406/4 "2025-12-16T14:52:22Z")

</div>

On a side note, kind of odd that cmake errors the same error 5 times in this case - I’ve experience this before too…

Regarding the errors - paths specified in `target_sources` are considered to be relative to the current cmake’s file source dir - in your case it’s `/.../learn/embedded/cmake`.  
E.g.

- `MathFunctions` → `/.../learn/embedded/cmake/MathFunctions/`
- `CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx` → `/.../learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.cxx`.

Since you’ve specified that base directory for the file set is `MathFunctions`, then all files inside the file set should be inside this directory, which is not the case for `/../learn/embedded/cmake/CMake/Help/guide/tutorial/Step1/MathFunctions/MathFunctions.h`

The solution depends on what you’re trying to achieve, maybe you need two file sets if you want provide `BASE_DIRS` as it currently is or maybe you don’t need `BASE_DIRS` at all or maybe header files should be moved.
