Skip to content

Commit ec44c02

Browse files
committed
add ReadMe and cmake samples
1 parent 572903a commit ec44c02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2180
-2
lines changed

README.md

+790-2
Large diffs are not rendered by default.

Tutorial_part1/CMakeLists.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
#set project name and version
4+
project(Tutorial VERSION 1.0)
5+
6+
######################################### test config ###################################
7+
#set config file
8+
configure_file(TutorialConfig.h.in TutorialConfig.h)
9+
add_executable(Tutorial Tutorial.cpp)
10+
#this line must after add_executable, or will be error!
11+
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
12+
########################################################################################
13+
14+
######################################### test standard ################################
15+
#set c++ standard
16+
#if comment this line, the default compiler will be use
17+
set(CMAKE_CXX_STANDARD 11)
18+
set(CMAKE_CXX_STANDARD_REQUIRED True)
19+
add_executable(TestStd TestStd.cpp)
20+
########################################################################################
21+
22+

Tutorial_part1/TestStd.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
3+
int main(int argc, char *argv[])
4+
{
5+
/** show the C++ version */
6+
if (__cplusplus == 201703L)
7+
std::cout << "C++17\n";
8+
else if (__cplusplus == 201402L)
9+
std::cout << "C++14\n";
10+
else if (__cplusplus == 201103L)
11+
std::cout << "C++11\n";
12+
else if (__cplusplus == 199711L)
13+
std::cout << "C++98\n";
14+
else
15+
std::cout << "pre-standard C++\n";
16+
17+
return 0;
18+
}
19+
20+

Tutorial_part1/Tutorial.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "TutorialConfig.h"
2+
#include <iostream>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
7+
if (argc < 2) {
8+
// report version
9+
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
10+
<< Tutorial_VERSION_MINOR << std::endl;
11+
std::cout << "Usage: " << argv[0] << " number" << std::endl;
12+
return 1;
13+
}
14+
15+
return 0;
16+
}
17+
18+

Tutorial_part1/TutorialConfig.h.in

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _TUTORIALCONFIG_H_
2+
#define _TUTORIALCONFIG_H_
3+
// the configured options and settings for Tutorial
4+
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
5+
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
6+
7+
#endif // _TUTORIALCONFIG_H_
8+

Tutorial_part2/CMakeLists.txt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
#set project name and version
4+
project(Tutorial VERSION 1.0)
5+
6+
# add option
7+
option(USE_MYMATH "Use tutorial provided math implementation" ON)
8+
#########################################################################################
9+
10+
######################################### test config ###################################
11+
#set config file
12+
configure_file(TutorialConfig.h.in TutorialConfig.h)
13+
add_executable(Tutorial Tutorial.cpp)
14+
#this line must after add_executable, or will be error!
15+
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
16+
########################################################################################
17+
18+
######################################### test standard ################################
19+
#set c++ standard
20+
#if comment this line, the default compiler will be use
21+
set(CMAKE_CXX_STANDARD 11)
22+
set(CMAKE_CXX_STANDARD_REQUIRED True)
23+
add_executable(TestStd TestStd.cpp)
24+
########################################################################################
25+
26+
# if USE_MYMATH is true
27+
if(USE_MYMATH)
28+
add_subdirectory(MathFunctions)
29+
list(APPEND EXTRA_LIBS MathFunctions)
30+
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
31+
message("USE_MYMATH is True")
32+
endif()
33+
########################################################################################
34+
35+
# tesr sqrt
36+
add_executable(TestSqrt TestSqrt.cpp)
37+
38+
target_link_libraries(TestSqrt PUBLIC ${EXTRA_LIBS})
39+
40+
target_include_directories(TestSqrt PUBLIC
41+
"${PROJECT_BINARY_DIR}"
42+
${EXTRA_INCLUDES}
43+
)
44+
########################################################################################
45+
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_library(MathFunctions SHARED mysqrt.cpp)
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _MATHFUNCTION_H_
2+
#define _MATHFUNCTION_H_
3+
4+
double mysqrt(double input);
5+
6+
#endif /* _MATHFUNCTION_H_*/
7+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "MathFunctions.h"
2+
#include <iostream>
3+
4+
double mysqrt(double input)
5+
{
6+
if (input <= 0) {
7+
return 0;
8+
}
9+
10+
double result = input;
11+
12+
// do ten iterations
13+
for (int i = 0; i < 10; ++i) {
14+
if (result <= 0) {
15+
result = 0.1;
16+
}
17+
double delta = input - (result * result);
18+
result = result + 0.5 * delta / result;
19+
std::cout << "Computing sqrt of " << input << " to be " << result << std::endl;
20+
}
21+
22+
return result;
23+
}
24+
25+

Tutorial_part2/TestSqrt.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//TestSqrt.cpp
2+
#include <iostream>
3+
#include "TutorialConfig.h"
4+
5+
#ifdef USE_MYMATH
6+
# include "MathFunctions.h"
7+
#else
8+
# include <cmath>
9+
#endif //USE_MYMATH
10+
11+
int main(int argc, char** argv)
12+
{
13+
double inputValue = std::stod(argv[1]);
14+
#ifdef USE_MYMATH
15+
double sqrtValue = mysqrt(inputValue);
16+
#else
17+
double sqrtValue = sqrt(inputValue);
18+
#endif //USE_MYMATH
19+
20+
std::cout << "Input value is :" << inputValue << std::endl;
21+
std::cout << "Sqrt value is :" << sqrtValue << std::endl;
22+
23+
return 0;
24+
}
25+

Tutorial_part2/TestStd.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
3+
int main(int argc, char *argv[])
4+
{
5+
/** show the C++ version */
6+
if (__cplusplus == 201703L)
7+
std::cout << "C++17\n";
8+
else if (__cplusplus == 201402L)
9+
std::cout << "C++14\n";
10+
else if (__cplusplus == 201103L)
11+
std::cout << "C++11\n";
12+
else if (__cplusplus == 199711L)
13+
std::cout << "C++98\n";
14+
else
15+
std::cout << "pre-standard C++\n";
16+
17+
return 0;
18+
}
19+
20+

Tutorial_part2/Tutorial.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "TutorialConfig.h"
2+
#include <iostream>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
7+
if (argc < 2) {
8+
// report version
9+
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
10+
<< Tutorial_VERSION_MINOR << std::endl;
11+
std::cout << "Usage: " << argv[0] << " number" << std::endl;
12+
return 1;
13+
}
14+
15+
return 0;
16+
}
17+
18+

Tutorial_part2/TutorialConfig.h.in

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _TUTORIALCONFIG_H_
2+
#define _TUTORIALCONFIG_H_
3+
// the configured options and settings for Tutorial
4+
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
5+
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
6+
#cmakedefine USE_MYMATH
7+
8+
#endif // _TUTORIALCONFIG_H_
9+

Tutorial_part3/CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
#set project name and version
4+
project(Tutorial VERSION 1.0)
5+
6+
# add option
7+
option(USE_MYMATH "Use tutorial provided math implementation" ON)
8+
#########################################################################################
9+
10+
######################################### test config ###################################
11+
#set config file
12+
configure_file(TutorialConfig.h.in TutorialConfig.h)
13+
add_executable(Tutorial Tutorial.cpp)
14+
#this line must after add_executable, or will be error!
15+
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
16+
########################################################################################
17+
18+
######################################### test standard ################################
19+
#set c++ standard
20+
#if comment this line, the default compiler will be use
21+
set(CMAKE_CXX_STANDARD 11)
22+
set(CMAKE_CXX_STANDARD_REQUIRED True)
23+
add_executable(TestStd TestStd.cpp)
24+
########################################################################################
25+
26+
# if USE_MYMATH is true
27+
if(USE_MYMATH)
28+
add_subdirectory(MathFunctions)
29+
list(APPEND EXTRA_LIBS MathFunctions)
30+
message("USE_MYMATH is True")
31+
endif()
32+
########################################################################################
33+
34+
# tesr sqrt
35+
add_executable(TestSqrt TestSqrt.cpp)
36+
37+
target_link_libraries(TestSqrt PUBLIC ${EXTRA_LIBS})
38+
39+
target_include_directories(TestSqrt PUBLIC
40+
"${PROJECT_BINARY_DIR}"
41+
)
42+
########################################################################################
43+
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
add_library(MathFunctions SHARED mysqrt.cpp)
3+
4+
target_include_directories(MathFunctions
5+
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
6+
)
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _MATHFUNCTION_H_
2+
#define _MATHFUNCTION_H_
3+
4+
double mysqrt(double input);
5+
6+
#endif /* _MATHFUNCTION_H_*/
7+
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "MathFunctions.h"
2+
#include <iostream>
3+
4+
double mysqrt(double input)
5+
{
6+
if (input <= 0) {
7+
return 0;
8+
}
9+
10+
double result = input;
11+
12+
// do ten iterations
13+
for (int i = 0; i < 10; ++i) {
14+
if (result <= 0) {
15+
result = 0.1;
16+
}
17+
double delta = input - (result * result);
18+
result = result + 0.5 * delta / result;
19+
std::cout << "Computing sqrt of " << input << " to be " << result << std::endl;
20+
}
21+
22+
return result;
23+
}
24+
25+

Tutorial_part3/TestSqrt.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//TestSqrt.cpp
2+
#include <iostream>
3+
#include "TutorialConfig.h"
4+
5+
#ifdef USE_MYMATH
6+
# include "MathFunctions.h"
7+
#else
8+
# include <cmath>
9+
#endif //USE_MYMATH
10+
11+
int main(int argc, char** argv)
12+
{
13+
double inputValue = std::stod(argv[1]);
14+
#ifdef USE_MYMATH
15+
double sqrtValue = mysqrt(inputValue);
16+
#else
17+
double sqrtValue = sqrt(inputValue);
18+
#endif //USE_MYMATH
19+
20+
std::cout << "Input value is :" << inputValue << std::endl;
21+
std::cout << "Sqrt value is :" << sqrtValue << std::endl;
22+
23+
return 0;
24+
}
25+

Tutorial_part3/TestStd.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
3+
int main(int argc, char *argv[])
4+
{
5+
/** show the C++ version */
6+
if (__cplusplus == 201703L)
7+
std::cout << "C++17\n";
8+
else if (__cplusplus == 201402L)
9+
std::cout << "C++14\n";
10+
else if (__cplusplus == 201103L)
11+
std::cout << "C++11\n";
12+
else if (__cplusplus == 199711L)
13+
std::cout << "C++98\n";
14+
else
15+
std::cout << "pre-standard C++\n";
16+
17+
return 0;
18+
}
19+
20+

Tutorial_part3/Tutorial.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "TutorialConfig.h"
2+
#include <iostream>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
7+
if (argc < 2) {
8+
// report version
9+
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
10+
<< Tutorial_VERSION_MINOR << std::endl;
11+
std::cout << "Usage: " << argv[0] << " number" << std::endl;
12+
return 1;
13+
}
14+
15+
return 0;
16+
}
17+
18+

Tutorial_part3/TutorialConfig.h.in

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _TUTORIALCONFIG_H_
2+
#define _TUTORIALCONFIG_H_
3+
// the configured options and settings for Tutorial
4+
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
5+
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
6+
#cmakedefine USE_MYMATH
7+
8+
#endif // _TUTORIALCONFIG_H_
9+

0 commit comments

Comments
 (0)