Started a cmake tutorial
This commit is contained in:
22
Step3/MathFunctions/CMakeLists.txt
Normal file
22
Step3/MathFunctions/CMakeLists.txt
Normal file
@ -0,0 +1,22 @@
|
||||
add_library(MathFunctions MathFunctions.cxx)
|
||||
|
||||
# TODO 1: State that anybody linking to MathFunctions needs to include the
|
||||
# current source directory, while MathFunctions itself doesn't.
|
||||
# Hint: Use target_include_directories with the INTERFACE keyword
|
||||
|
||||
# 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")
|
||||
|
||||
# library that just does sqrt
|
||||
add_library(SqrtLibrary STATIC
|
||||
mysqrt.cxx
|
||||
)
|
||||
|
||||
# TODO 7: Link SqrtLibrary to tutorial_compiler_flags
|
||||
|
||||
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
|
||||
endif()
|
||||
|
||||
# TODO 6: Link MathFunctions to tutorial_compiler_flags
|
19
Step3/MathFunctions/MathFunctions.cxx
Normal file
19
Step3/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
|
||||
}
|
||||
}
|
5
Step3/MathFunctions/MathFunctions.h
Normal file
5
Step3/MathFunctions/MathFunctions.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace mathfunctions {
|
||||
double sqrt(double x);
|
||||
}
|
28
Step3/MathFunctions/mysqrt.cxx
Normal file
28
Step3/MathFunctions/mysqrt.cxx
Normal file
@ -0,0 +1,28 @@
|
||||
#include "mysqrt.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace mathfunctions {
|
||||
namespace detail {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
7
Step3/MathFunctions/mysqrt.h
Normal file
7
Step3/MathFunctions/mysqrt.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace mathfunctions {
|
||||
namespace detail {
|
||||
double mysqrt(double x);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user