Started a cmake tutorial
This commit is contained in:
		
							
								
								
									
										27
									
								
								Step4/MathFunctions/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								Step4/MathFunctions/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| # create the MathFunctions library | ||||
| 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) | ||||
|  | ||||
|   target_link_libraries(MathFunctions PRIVATE SqrtLibrary) | ||||
| endif() | ||||
|  | ||||
| # link MathFunctions to tutorial_compiler_flags | ||||
| target_link_libraries(MathFunctions PUBLIC tutorial_compiler_flags) | ||||
							
								
								
									
										19
									
								
								Step4/MathFunctions/MathFunctions.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								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 | ||||
| } | ||||
| } | ||||
							
								
								
									
										5
									
								
								Step4/MathFunctions/MathFunctions.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Step4/MathFunctions/MathFunctions.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| #pragma once | ||||
|  | ||||
| namespace mathfunctions { | ||||
| double sqrt(double x); | ||||
| } | ||||
							
								
								
									
										28
									
								
								Step4/MathFunctions/mysqrt.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								Step4/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
									
								
								Step4/MathFunctions/mysqrt.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								Step4/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