Started a cmake tutorial
This commit is contained in:
55
Step7/MathFunctions/CMakeLists.txt
Normal file
55
Step7/MathFunctions/CMakeLists.txt
Normal file
@ -0,0 +1,55 @@
|
||||
add_library(MathFunctions MathFunctions.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}
|
||||
)
|
||||
|
||||
# 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
|
||||
)
|
||||
|
||||
# link SqrtLibrary to tutorial_compiler_flags
|
||||
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
|
||||
|
||||
# TODO 1: Include CheckCXXSourceCompiles
|
||||
|
||||
# TODO 2: Use check_cxx_source_compiles with simple C++ code to verify
|
||||
# availability of:
|
||||
# * std::log
|
||||
# * std::exp
|
||||
# Store the results in HAVE_LOG and HAVE_EXP respectively.
|
||||
|
||||
# Hint: Sample C++ code which uses log:
|
||||
# #include <cmath>
|
||||
# int main() {
|
||||
# std::log(1.0);
|
||||
# return 0;
|
||||
# }
|
||||
|
||||
# TODO 3: Conditionally on HAVE_LOG and HAVE_EXP, add private compile
|
||||
# definitions "HAVE_LOG" and "HAVE_EXP" to the SqrtLibrary target.
|
||||
|
||||
# Hint: Use target_compile_definitions()
|
||||
|
||||
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
|
||||
endif()
|
||||
|
||||
# link MathFunctions to tutorial_compiler_flags
|
||||
target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags)
|
||||
|
||||
# install libs
|
||||
set(installable_libs MathFunctions tutorial_compiler_flags)
|
||||
if(TARGET SqrtLibrary)
|
||||
list(APPEND installable_libs SqrtLibrary)
|
||||
endif()
|
||||
install(TARGETS ${installable_libs} DESTINATION lib)
|
||||
# install include headers
|
||||
install(FILES MathFunctions.h DESTINATION include)
|
19
Step7/MathFunctions/MathFunctions.cxx
Normal file
19
Step7/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
Step7/MathFunctions/MathFunctions.h
Normal file
5
Step7/MathFunctions/MathFunctions.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace mathfunctions {
|
||||
double sqrt(double x);
|
||||
}
|
37
Step7/MathFunctions/mysqrt.cxx
Normal file
37
Step7/MathFunctions/mysqrt.cxx
Normal file
@ -0,0 +1,37 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
// TODO 5: If both HAVE_LOG and HAVE_EXP are defined, use the following:
|
||||
//// double result = std::exp(std::log(x) * 0.5);
|
||||
//// std::cout << "Computing sqrt of " << x << " to be " << result
|
||||
//// << " using log and exp" << std::endl;
|
||||
// else, use the existing logic.
|
||||
|
||||
// Hint: Don't forget the #endif before returning the result!
|
||||
|
||||
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
Step7/MathFunctions/mysqrt.h
Normal file
7
Step7/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