Skip to content

Commit

Permalink
Complete Step1 and Step2.
Browse files Browse the repository at this point in the history
  • Loading branch information
KmrTakumi committed May 29, 2024
1 parent e44f0ea commit d7664f4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
/Step1_build/
/Step2_build/
14 changes: 8 additions & 6 deletions Step1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# TODO 1: Set the minimum required version of CMake to be 3.10

cmake_minimum_required(VERSION 3.12)
# TODO 2: Create a project named Tutorial

project(Tutorial)
# TODO 7: Set the project version number as 1.0 in the above project command

project(Tutorial VERSION 1.5)
# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11
# and the variable CMAKE_CXX_STANDARD_REQUIRED to True

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to
# TutorialConfig.h

configure_file(TutorialConfig.h.in TutorialConfig.h)
# TODO 3: Add an executable called Tutorial to the project
# Hint: Be sure to specify the source file as tutorial.cxx

add_executable(Tutorial tutorial.cxx)
# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
4 changes: 4 additions & 0 deletions Step1/TutorialConfig.h.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// the configured options and settings for Tutorial
// TODO 10: Define Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR
// clang-format off
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
// clang-format on
9 changes: 5 additions & 4 deletions Step1/tutorial.cxx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// A simple program that computes the square root of a number
#include <cmath>
#include <cstdlib> // TODO 5: Remove this line
#include <iostream>
#include <string>

// TODO 11: Include TutorialConfig.h
#include "TutorialConfig.h"

int main(int argc, char* argv[])
{
int main(int argc, char *argv[]) {
if (argc < 2) {
// TODO 12: Create a print statement using Tutorial_VERSION_MAJOR
// and Tutorial_VERSION_MINOR
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
<< Tutorial_VERSION_MINOR << std::endl;
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}

// convert input to double
// TODO 4: Replace atof(argv[1]) with std::stod(argv[1])
const double inputValue = atof(argv[1]);
const double inputValue = std::stod(argv[1]);

// calculate square root
const double outputValue = sqrt(inputValue);
Expand Down
5 changes: 3 additions & 2 deletions Step2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
configure_file(TutorialConfig.h.in TutorialConfig.h)

# TODO 2: Use add_subdirectory() to add MathFunctions to this project

add_subdirectory(MathFunctions)
# add the executable
add_executable(Tutorial tutorial.cxx)

# TODO 3: Use target_link_libraries to link the library to our executable

target_link_libraries(Tutorial PUBLIC MathFunctions)
# TODO 4: Add MathFunctions to Tutorial's target_include_directories()
# Hint: ${PROJECT_SOURCE_DIR} is a path to the project source. AKA This folder!
target_include_directories(Tutorial PUBLIC "${PROJECT_SOURCE_DIR}/MathFunctions")

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
Expand Down
10 changes: 7 additions & 3 deletions Step2/MathFunctions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
# TODO 1: Add a library called MathFunctions with sources MathFunctions.cxx
# and mysqrt.cxx
# Hint: You will need the add_library command
add_library(MathFunctions MathFunctions.cxx)

# TODO 7: Create a variable USE_MYMATH using option and set default to ON

option(USE_MYMATH ON)
# TODO 8: If USE_MYMATH is ON, use target_compile_definitions to pass
# USE_MYMATH as a precompiled definition to our source files

if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# TODO 12: When USE_MYMATH is ON, add a library for SqrtLibrary with
# source mysqrt.cxx

add_library(SqrtLibrary STATIC mysqrt)
# TODO 13: When USE_MYMATH is ON, link SqrtLibrary to the MathFunctions Library
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
17 changes: 11 additions & 6 deletions Step2/MathFunctions/MathFunctions.cxx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include "MathFunctions.h"

// TODO 11: include cmath

#include <cmath>
// TODO 10: Wrap the mysqrt include in a precompiled ifdef based on USE_MYMATH
#ifdef USE_MYMATH
#include "mysqrt.h"
#endif

namespace mathfunctions {
double sqrt(double x)
{
// TODO 9: If USE_MYMATH is defined, use detail::mysqrt.
// Otherwise, use std::sqrt.
double sqrt(double x) {
// TODO 9: If USE_MYMATH is defined, use detail::mysqrt.
#ifdef USE_MYMATH
return detail::mysqrt(x);
#else
return std::sqrt(x);
#endif
// Otherwise, use std::sqrt.
}
}
} // namespace mathfunctions
1 change: 1 addition & 0 deletions Step2/TutorialConfig.h.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// the configured options and settings for Tutorial
// clang-format off
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
7 changes: 4 additions & 3 deletions Step2/tutorial.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <string>

// TODO 5: Include MathFunctions.h
#include "MathFunctions.h"
#include "TutorialConfig.h"

int main(int argc, char* argv[])
{
int main(int argc, char* argv[]) {
if (argc < 2) {
// report version
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
Expand All @@ -22,7 +22,8 @@ int main(int argc, char* argv[])
// TODO 6: Replace sqrt with mathfunctions::sqrt

// calculate square root
const double outputValue = sqrt(inputValue);
// const double outputValue = sqrt(inputValue);
const double outputValue = mathfunctions::sqrt(inputValue);
std::cout << "The square root of " << inputValue << " is " << outputValue
<< std::endl;
return 0;
Expand Down

0 comments on commit d7664f4

Please sign in to comment.