1

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;
}
  • I am totally new to this cmake .So asking in this manner.May be I am not following proper professionalism .But wants someones guidance to run this step2 example . – mahesh narlekar Jan 13 '20 at 12:22
  • The tutorial you refers to contains many **snippets**, some of which won't work together. Show **your code** (`CMakeLists.txt`) which you are trying to build. – Tsyvarev Jan 13 '20 at 12:32
  • @Tsyvarev,I have updated my post with my cmake list information . – mahesh narlekar Jan 13 '20 at 13:35

2 Answers2

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.

Vladimir Berlev
  • 492
  • 4
  • 15
  • I have commented out line 16 .But How can I run my code.I have created step2_build folder outside step2 folder .And trying to run the code using : 1) step2_build$ cmake ../step2 (2) step2_build$ cmake --build . (3) step2_build$ ./Tutorial 121 ...But I am not able to get the ./Tutorial Executable ??I have updated my post with MATHFUNCTION cmake file and cpp file – mahesh narlekar Jan 14 '20 at 10:38
  • Actually if all the steps you mention complete successfully it must be there. Try to search for the file in your build directory: `step2_build$ find . -name Tutorial` – Vladimir Berlev Jan 14 '20 at 12:41
  • When I am trying to run below command,I am getting this error . => /TER_CMAKE/Tutorial/step2_build$ cmake --build . make: *** No targets specified and no makefile found. Stop. – mahesh narlekar Jan 14 '20 at 14:02
  • Only this runs . /TER_CMAKE/Tutorial/step2_build$ cmake ../step2 -- Configuring done -- Generating done -- Build files have been written to: /home/user/TER_CMAKE/Tutorial/step2 – mahesh narlekar Jan 14 '20 at 14:03
  • Should I run it from other folder location to generate the ./Tutorial executable ?? – mahesh narlekar Jan 14 '20 at 14:05
  • According to your comment, you have generated build tree to `/home/user/TER_CMAKE/Tutorial/step2`, so you have to run further commands in this directory. – Vladimir Berlev Jan 14 '20 at 14:46
  • I am able to run the first step : `. /TER_CMAKE/Tutorial/step2_build$ cmake ../step2 ` .But after that If I am trying to run the next command I Am getting error which is : ` /TER_CMAKE/Tutorial/step2_build$ cmake --build .` make: *** No targets specified and no makefile found. Stop. – mahesh narlekar Jan 15 '20 at 09:41
  • I got the issue .Your reply work well .Only I need to delete old cache and generated cmake folder from my old build .By mistake I have created that files at wrong location . – mahesh narlekar Jan 22 '20 at 14:03
  • Yes, sometimes you have to clean caches to make changes effective. – Vladimir Berlev Jan 23 '20 at 15:49
0

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}
marshallb
  • 1
  • 1