mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-06 05:40:54 -06:00
Tutorial: Refactor MathFunctions code
Propagate the refactor in Step 10 MathFunctions through all of the steps. Use MathFunctions/MathFunctions.cxx instead of Tutorial.cxx to determine which sqrt library is called. Adds .h files which correspond to their .cxx files by name.
This commit is contained in:
@@ -31,25 +31,19 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
# build-tree
|
||||
# Hint: Use BUILD_INTERFACE
|
||||
|
||||
# should we use our own math functions
|
||||
option(USE_MYMATH "Use tutorial provided math implementation" ON)
|
||||
|
||||
# configure a header file to pass some of the CMake settings
|
||||
# to the source code
|
||||
configure_file(TutorialConfig.h.in TutorialConfig.h)
|
||||
|
||||
# add the MathFunctions library
|
||||
if(USE_MYMATH)
|
||||
add_subdirectory(MathFunctions)
|
||||
list(APPEND EXTRA_LIBS MathFunctions)
|
||||
endif()
|
||||
add_subdirectory(MathFunctions)
|
||||
|
||||
# add the executable
|
||||
add_executable(Tutorial tutorial.cxx)
|
||||
|
||||
# TODO 2: Link to tutorial_compiler_flags
|
||||
|
||||
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
|
||||
target_link_libraries(Tutorial PUBLIC MathFunctions)
|
||||
|
||||
# add the binary tree to the search path for include files
|
||||
# so that we will find TutorialConfig.h
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
add_library(MathFunctions mysqrt.cxx)
|
||||
add_library(MathFunctions MathFunctions.cxx mysqrt.cxx)
|
||||
|
||||
# state that anybody linking to us needs to include the current source dir
|
||||
# to find MathFunctions.h, while we don't.
|
||||
target_include_directories(MathFunctions
|
||||
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
# should we use our own math functions
|
||||
option(USE_MYMATH "Use tutorial provided math implementation" ON)
|
||||
if (USE_MYMATH)
|
||||
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
|
||||
endif()
|
||||
|
||||
# TODO 3: Link to tutorial_compiler_flags
|
||||
|
||||
19
Help/guide/tutorial/Step4/MathFunctions/MathFunctions.cxx
Normal file
19
Help/guide/tutorial/Step4/MathFunctions/MathFunctions.cxx
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "MathFunctions.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#ifdef USE_MYMATH
|
||||
# include "mysqrt.h"
|
||||
#endif
|
||||
|
||||
namespace mathfunctions {
|
||||
double sqrt(double x)
|
||||
{
|
||||
// which square root function should we use?
|
||||
#ifdef USE_MYMATH
|
||||
return detail::mysqrt(x);
|
||||
#else
|
||||
return std::sqrt(x);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
double mysqrt(double x);
|
||||
#pragma once
|
||||
|
||||
namespace mathfunctions {
|
||||
double sqrt(double x);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "mysqrt.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "MathFunctions.h"
|
||||
|
||||
namespace mathfunctions {
|
||||
namespace detail {
|
||||
// a hack square root calculation using simple operations
|
||||
double mysqrt(double x)
|
||||
{
|
||||
@@ -22,3 +24,5 @@ double mysqrt(double x)
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
7
Help/guide/tutorial/Step4/MathFunctions/mysqrt.h
Normal file
7
Help/guide/tutorial/Step4/MathFunctions/mysqrt.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace mathfunctions {
|
||||
namespace detail {
|
||||
double mysqrt(double x);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
// the configured options and settings for Tutorial
|
||||
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
|
||||
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
|
||||
#cmakedefine USE_MYMATH
|
||||
|
||||
@@ -3,13 +3,9 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "MathFunctions.h"
|
||||
#include "TutorialConfig.h"
|
||||
|
||||
// should we include the MathFunctions header?
|
||||
#ifdef USE_MYMATH
|
||||
# include "MathFunctions.h"
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc < 2) {
|
||||
@@ -23,12 +19,7 @@ int main(int argc, char* argv[])
|
||||
// convert input to double
|
||||
const double inputValue = std::stod(argv[1]);
|
||||
|
||||
// which square root function should we use?
|
||||
#ifdef USE_MYMATH
|
||||
const double outputValue = mysqrt(inputValue);
|
||||
#else
|
||||
const double outputValue = sqrt(inputValue);
|
||||
#endif
|
||||
const double outputValue = mathfunctions::sqrt(inputValue);
|
||||
|
||||
std::cout << "The square root of " << inputValue << " is " << outputValue
|
||||
<< std::endl;
|
||||
|
||||
Reference in New Issue
Block a user