Error during the adding library for learning

So i am new to learning CMake language.

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

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).

-- 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

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.