Skip to content

Commit 3e47325

Browse files
committed
finish "step4":exercise 1
1 parent 6b836e2 commit 3e47325

5 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎Step4/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
cmake_minimum_required(VERSION 3.23)
22

33
project(Tutorial)
4-
4+
# set(CMAKE_CXX_COMPILER g++-13)
55
option(TUTORIAL_BUILD_UTILITIES "Build the Tutorial executable" ON)
66

77
# TODO1: Add a default-OFF option named TUTORIAL_USE_STD_SQRT, with a doc
88
# string of "Use std::sqrt"
9+
option(TUTORIAL_USE_STD_SQRT "Use std::sqrt" OFF)
910

1011
if(TUTORIAL_BUILD_UTILITIES)
1112
add_subdirectory(Tutorial)

‎Step4/CMakePresets.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"description": "Preset to use with the tutorial",
88
"binaryDir": "${sourceDir}/build",
99
"cacheVariables": {
10-
"TODO7": "Remove the CMAKE_CXX_STANDARD (and this TODO)",
11-
"CMAKE_CXX_STANDARD": "20",
12-
"TODO8": "Set TUTORIAL_USE_STD_SQRT to ON"
10+
"CMAKE_CXX_COMPILER":"g++-13",
11+
"CMAKE_EXPORT_COMPILE_COMMANDS":"ON",
12+
13+
"TUTORIAL_USE_STD_SQRT": "ON"
1314
}
1415
}
1516
]

‎Step4/MathFunctions/CMakeLists.txt‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@ add_library(MathFunctions)
22

33
target_sources(MathFunctions
44
PRIVATE
5-
MathFunctions.cxx
5+
MathFunctions.cxx
66

77
PUBLIC
8-
FILE_SET HEADERS
9-
FILES
10-
MathFunctions.h
8+
FILE_SET HEADERS
9+
FILES
10+
MathFunctions.h
1111
)
1212

1313
# TODO2: Add a compile feature for C++20 support to MathFunctions
14+
target_compile_features(MathFunctions PRIVATE cxx_std_20)
1415

1516
# TODO3: Add a conditional which checks TUTORIAL_USE_STD_SQRT and if
1617
# ON, set a compile definition on MathFunctions of the same name
18+
if(TUTORIAL_USE_STD_SQRT)
19+
target_compile_definitions(MathFunctions
20+
PRIVATE
21+
TUTORIAL_USE_STD_SQRT
22+
)
23+
endif()

‎Step4/MathFunctions/MathFunctions.cxx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// TODO5: Include <cmath>
2+
#include <cmath>
23

34
#include <format>
45
#include <iostream>
@@ -32,7 +33,10 @@ double sqrt(double x)
3233
{
3334
// TODO6: Check if TUTORIAL_USE_STD_SQRT is defined, if so use std::sqrt
3435
// instead of mysqrt
35-
36+
#ifdef TUTORIAL_USE_STD_SQRT
37+
return std::sqrt(x);
38+
#else
3639
return mysqrt(x);
40+
#endif
3741
}
3842
}

‎Step4/Tutorial/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ target_link_libraries(Tutorial
1313
)
1414

1515
# TODO4: Add a compile feature for C++20 support to Tutorial
16+
target_compile_features(Tutorial PRIVATE cxx_std_20)
1617

1718

1819
if(

0 commit comments

Comments
 (0)