'getting error in running cmake std Tutorial step 2 example

I am totally new to cmake and its syntax .But fortunately I am able to run the cmake tutorial step 1 as per the introductions mention on below links :

https://cmake.org/cmake/help/latest/guide/tutorial/index.html

But I am totally stucked at step 2 project to run using cmake.

I have created the step 2 project and understand the syntax to link the library for doing square root of a number, But I did not understand how to run this as I am getting below error :

user@server:~/TER_CMAKE/Tutorial/step2_build$ cmake ../step2
CMake Error at CMakeLists.txt:19 (add_subdirectory):
  The binary directory

    /home/user/TER_CMAKE/Tutorial/step2/MathFunctions

  is already used to build a source directory.  It cannot be used to build
  source directory

    /home/user/TER_CMAKE/Tutorial/step2/MathFunctions

  Specify a unique binary directory name.


-- Configuring incomplete, errors occurred!

The example is available at below location for step 2 under heading Adding a Library (Step 2)..

https://moodle.rrze.uni-erlangen.de/pluginfile.php/14829/mod_resource/content/5/CMakeTutorial.pdf

My intention is to run my example this way

 step2_build$ cmake ../step2
 step2_build$ cmake --build .
 step2_build$ ./Tutorial 121 

As I am not sure that is it good to ask this way on this platform ,But as I do not have any other guidance .I am doing this by my own .

Note: I do not wants to use any tool to run my step 2 example.I wants to run everything using command prompt and cmake command only .where I can understand the cmake .

Edit:

Adding my CMakeLists.txt =

cmake_minimum_required(VERSION 3.5)

#set the project name
project(Tutorial VERSION 1.0)

#specify the c++ std
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

option(USE_MYMATH "Use tutorial provided math implementation" ON)

#Configure a header file to pass the version number to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

#add the MathFunctions Library
add_subdirectory(MathFunctions)

if(USE_MYMATH)
    add_subdirectory(MathFunctions)
    list(APPEND EXTRA_LIBS MathFunctions)
    list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()


#add the executable
add_executable(Tutorial tutorial.cpp)

target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC 
                           "${PROJECT_BINARY_DIR}"  
                           ${EXTRA_LIBS}
                          )

My Source tutorial.cpp file:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>

#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif
#include "TutorialConfig.h"

using namespace std;

int main(int argc, char* argv[])
{
  if (argc < 2) {
    cout << "Usage: " << argv[0] << " number" << endl;
    return 1;
  }

  // convert input to double
  const double inputValue = atof(argv[1]);

  // calculate square root
#ifdef USE_MYMATH
  const double outputValue = mysqrt(inputValue);
#else
  const double outputValue = sqrt(inputValue);
#endif
  cout << "The square root of " << inputValue << " is " << outputValue << endl;
  return 0;
}

ToturialConfig.h.in file :

#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

#cmakedefine USE_MYMATH

EDIT: Step2 has a folder MathFuctions,Which has Cmake file mysqrt.cpp file

/TER_CMAKE/Tutorial/step2/MathFunctions/CMakeLists.txt

add_library(MathFunctions mysqrt.cpp)

/TER_CMAKE/Tutorial/step2/MathFunctions/mysqrt.cpp

#include <iostream>

// a hack square root calculation using simple operations
double mysqrt(double x)
{
  if (x <= 0) {
    return 0;
  }

  double result = x;

  // do ten iterations
  for (int i = 0; i < 10; ++i) {
    if (result <= 0) {
      result = 0.1;
    }
    double delta = x - (result * result);
    result = result + 0.5 * delta / result;
    std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
  }
  return result;
}


Solution 1:[1]

In case USE_MYMATH variable is set add_subdirectory(MathFunctions) is invoked twice. You need to decide and remove one of the occurrences on lines 16 and 19 in you CMakeLists.txt.

Solution 2:[2]

Two issues I can see:

  1. You're adding the subdirectory "MathFunctions" twice when you configure the build with -DUSE_MYMATH=ON. This is why you are getting "CMake Error at CMakeLists.txt:19 (add_subdirectory):"

To fix, remove

#add the MathFunctions Library
add_subdirectory(MathFunctions)

and rely on

if(USE_MYMATH)
    add_subdirectory(MathFunctions)
    list(APPEND EXTRA_LIBS MathFunctions)
    list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
  1. In your CMakeLists.txt file, you are doing
target_include_directories(Tutorial PUBLIC 
                           "${PROJECT_BINARY_DIR}"  
                           ${EXTRA_LIBS}
                          )

Instead of

${EXTRA_LIBS}

It should be

${EXTRA_INCLUDES}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vladimir Berlev
Solution 2 marshallb