summaryrefslogtreecommitdiff
path: root/Help/guide/tutorial/Step8
diff options
context:
space:
mode:
Diffstat (limited to 'Help/guide/tutorial/Step8')
-rw-r--r--Help/guide/tutorial/Step8/CMakeLists.txt12
-rw-r--r--Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt73
-rw-r--r--Help/guide/tutorial/Step8/MathFunctions/MathFunctions.cxx19
-rw-r--r--Help/guide/tutorial/Step8/MathFunctions/MathFunctions.h6
-rw-r--r--Help/guide/tutorial/Step8/MathFunctions/mysqrt.cxx8
-rw-r--r--Help/guide/tutorial/Step8/MathFunctions/mysqrt.h7
-rw-r--r--Help/guide/tutorial/Step8/TutorialConfig.h.in1
-rw-r--r--Help/guide/tutorial/Step8/tutorial.cxx13
8 files changed, 88 insertions, 51 deletions
diff --git a/Help/guide/tutorial/Step8/CMakeLists.txt b/Help/guide/tutorial/Step8/CMakeLists.txt
index cb87281b9e..97ec6aaa38 100644
--- a/Help/guide/tutorial/Step8/CMakeLists.txt
+++ b/Help/guide/tutorial/Step8/CMakeLists.txt
@@ -16,23 +16,17 @@ target_compile_options(tutorial_compiler_flags INTERFACE
"$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
)
-
-# 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)
-target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)
+
+target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
diff --git a/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt b/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt
index f81b563409..872a24a80e 100644
--- a/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt
+++ b/Help/guide/tutorial/Step8/MathFunctions/CMakeLists.txt
@@ -1,39 +1,58 @@
-add_library(MathFunctions mysqrt.cxx)
+add_library(MathFunctions MathFunctions.cxx)
+
+# 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
+ )
+
+ target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
+
+ # does this system provide the log and exp functions?
+ include(CheckCXXSourceCompiles)
+ check_cxx_source_compiles("
+ #include <cmath>
+ int main() {
+ std::log(1.0);
+ return 0;
+ }
+ " HAVE_LOG)
+ check_cxx_source_compiles("
+ #include <cmath>
+ int main() {
+ std::exp(1.0);
+ return 0;
+ }
+ " HAVE_EXP)
+
+ # add compile definitions
+ if(HAVE_LOG AND HAVE_EXP)
+ target_compile_definitions(SqrtLibrary
+ PRIVATE "HAVE_LOG" "HAVE_EXP"
+ )
+ endif()
+
+ target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
+endif()
# 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}
+ )
# link our compiler flags interface library
-target_link_libraries(MathFunctions tutorial_compiler_flags)
-
-# does this system provide the log and exp functions?
-include(CheckCXXSourceCompiles)
-check_cxx_source_compiles("
- #include <cmath>
- int main() {
- std::log(1.0);
- return 0;
- }
-" HAVE_LOG)
-check_cxx_source_compiles("
- #include <cmath>
- int main() {
- std::exp(1.0);
- return 0;
- }
-" HAVE_EXP)
-
-# add compile definitions
-if(HAVE_LOG AND HAVE_EXP)
- target_compile_definitions(MathFunctions
- PRIVATE "HAVE_LOG" "HAVE_EXP")
-endif()
+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)
diff --git a/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.cxx b/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.cxx
new file mode 100644
index 0000000000..dc28b4b530
--- /dev/null
+++ b/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.cxx
@@ -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
+}
+}
diff --git a/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.h b/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.h
index cd36bccffd..d5c2f224bf 100644
--- a/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.h
+++ b/Help/guide/tutorial/Step8/MathFunctions/MathFunctions.h
@@ -1 +1,5 @@
-double mysqrt(double x);
+#pragma once
+
+namespace mathfunctions {
+double sqrt(double x);
+}
diff --git a/Help/guide/tutorial/Step8/MathFunctions/mysqrt.cxx b/Help/guide/tutorial/Step8/MathFunctions/mysqrt.cxx
index 7eecd26b5d..28ab042de6 100644
--- a/Help/guide/tutorial/Step8/MathFunctions/mysqrt.cxx
+++ b/Help/guide/tutorial/Step8/MathFunctions/mysqrt.cxx
@@ -1,8 +1,10 @@
+#include "mysqrt.h"
+
#include <cmath>
#include <iostream>
-#include "MathFunctions.h"
-
+namespace mathfunctions {
+namespace detail {
// a hack square root calculation using simple operations
double mysqrt(double x)
{
@@ -30,3 +32,5 @@ double mysqrt(double x)
#endif
return result;
}
+}
+}
diff --git a/Help/guide/tutorial/Step8/MathFunctions/mysqrt.h b/Help/guide/tutorial/Step8/MathFunctions/mysqrt.h
new file mode 100644
index 0000000000..593d41e7a6
--- /dev/null
+++ b/Help/guide/tutorial/Step8/MathFunctions/mysqrt.h
@@ -0,0 +1,7 @@
+#pragma once
+
+namespace mathfunctions {
+namespace detail {
+double mysqrt(double x);
+}
+}
diff --git a/Help/guide/tutorial/Step8/TutorialConfig.h.in b/Help/guide/tutorial/Step8/TutorialConfig.h.in
index e23f5213c7..7e4d7fa1fa 100644
--- a/Help/guide/tutorial/Step8/TutorialConfig.h.in
+++ b/Help/guide/tutorial/Step8/TutorialConfig.h.in
@@ -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
diff --git a/Help/guide/tutorial/Step8/tutorial.cxx b/Help/guide/tutorial/Step8/tutorial.cxx
index b3c6a4f43c..a3a2bdce00 100644
--- a/Help/guide/tutorial/Step8/tutorial.cxx
+++ b/Help/guide/tutorial/Step8/tutorial.cxx
@@ -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;