ubuntu@ubuntu:$ vim TutorialConfig.h.in
ubuntu@ubuntu:$ cat TutorialConfig.h.in
// the configured options and settings for Tutorial
// TODO : Define Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine USE_MYMATH
ubuntu@ubuntu:$
Cxx file
ubuntu@ubuntu:$ vim tutorial.cxx
ubuntu@ubuntu:$ cat tutorial.cxx
// A simple program that computes the square root of a number#include<cmath>#include<iostream>#include<string>#include"TutorialConfig.h"#ifdefUSE_MYMATH#include"MathFunctions.h"#endifintmain(int argc,char* argv[]){if(argc <2){//report version
std::cout << argv[0]<<" Version "<< Tutorial_VERSION_MAJOR <<"."<< Tutorial_VERSION_MINOR << std::endl;
std::cout <<"Usage: "<< argv[0]<<" number"<< std::endl;return1;}// convert input to double// std::stod是C++ 11标准函数constdouble inputValue = std::stod(argv[1]);// calculate square rootconstdouble outputValue =mysqrt(inputValue);
std::cout <<"The square root of "<< inputValue <<" is "<< outputValue
<< std::endl;return0;}
ubuntu@ubuntu:$
Top CMake file
ubuntu@ubuntu:$ vim CMakeLists.txt
ubuntu@ubuntu:$ cat CMakeLists.txt
# TODO : Set the minimum required version of CMake to be 3.10
cmake_minimum_required(VERSION 3.10)
# TODO : Create a project named Tutorial and project version number 1.0
project(Tutorial VERSION 1.0)
# TODO : Replace the following code by:
# * Creating an interface library called tutorial_compiler_flags
# Hint: use add_library() with the INTERFACE signature
# * Add compiler feature cxx_std_11 to tutorial_compiler_flags
# Hint: Use target_compile_features()
# specify the C++ standard
# set(CMAKE_CXX_STANDARD 11)
# set(CMAKE_CXX_STANDARD_REQUIRED True)
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
# TODO : Create a variable MY_MATH using option and set default to ON
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# TODO : Use configure_file to configure and copy TutorialConfig.h.in to
# TutorialConfig.h
configure_file(TutorialConfig.h.in TutorialConfig.h)
# TODO : Use list() and APPEND to create a list of optional libraries
# called EXTRA_LIBS and a list of optional include directories called
# EXTRA_INCLUDES. Add the MathFunctions library and source directory to
# the appropriate lists.
#
# Only call add_subdirectory and only add MathFunctions specific values
# to EXTRA_LIBS and EXTRA_INCLUDES if USE_MYMATH is true.
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
# TODO : 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 : Use target_link_libraries to link the library to our executable
# Link to tutorial_compiler_flags
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS} tutorial_compiler_flags)
# TODO : Use target_include_directories to include ${PROJECT_BINARY_DIR}
target_include_directories(Tutorial PUBLIC
${PROJECT_BINARY_DIR}
)
ubuntu@ubuntu:$
MathFunctions directory
ubuntu@ubuntu:$ ls -tlr
total 16
-rw-r--r-- 1 ubuntu ubuntu 904 Oct 1222:26 tutorial.cxx
-rw-r--r-- 1 ubuntu ubuntu 188 Oct 1222:26 TutorialConfig.h.in
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 1222:26 MathFunctions
-rw-r--r-- 1 ubuntu ubuntu 2115 Oct 1817:30 CMakeLists.txt
ubuntu@ubuntu:$
MathFunctions Cxx file
ubuntu@ubuntu:$ vim mysqrt.cxx
ubuntu@ubuntu:$ cat mysqrt.cxx
#include<iostream>// a hack square root calculation using simple operationsdoublemysqrt(double x){if(x <=0){return0;}double result = x;// do ten iterationsfor(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;}
ubuntu@ubuntu:$
MathFunctions header file
ubuntu@ubuntu:$ vim MathFunctions.h
ubuntu@ubuntu:$ cat MathFunctions.h
#ifndef__MATHFUNCTION___H__H#define__MATHFUNCTION___H__Hdoublemysqrt(double x);#endif
ubuntu@ubuntu:$
MathFunctions CMake file
ubuntu@ubuntu:$ vim CMakeLists.txt
ubuntu@ubuntu:$ cat CMakeLists.txt
# TODO : Add a library called MathFunctions
# Hint: You will need the add_library command
add_library(MathFunctions mysqrt.cxx)
# TODO : State that anybody linking to MathFunctions needs to include the
# current source directory, while MathFunctions itself doesn't.
# Hint: Use target_include_directories with the INTERFACE keyword
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
# TODO 3: Link to tutorial_compiler_flags
target_link_libraries(MathFunctions tutorial_compiler_flags)
ubuntu@ubuntu:$
构建cmake工程
ubuntu@ubuntu:$ mkdir exercise7_build
ubuntu@ubuntu:$ cd exercise7_build/
ubuntu@ubuntu:$ ls -trl
total 0
ubuntu@ubuntu:$ cmake -DUSE_MYMATH=OFF ..
-- Build files have been written to: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step4/exercise7_build
ubuntu@ubuntu:$ cmake --build .
ubuntu@ubuntu:$ ls -tlr
total 56
-rw-r--r-- 1 ubuntu ubuntu 137 Oct 18 17:35 TutorialConfig.h
-rw-rw-r-- 1 ubuntu ubuntu 14733 Oct 18 17:35 CMakeCache.txt
-rw-rw-r-- 1 ubuntu ubuntu 6061 Oct 18 17:35 Makefile
-rw-rw-r-- 1 ubuntu ubuntu 1963 Oct 18 17:35 cmake_install.cmake
drwxrwxr-x 3 ubuntu ubuntu 4096 Oct 18 17:35 MathFunctions
-rwxrwxr-x 1 ubuntu ubuntu 15008 Oct 18 17:35 Tutorial
drwxrwxr-x 6 ubuntu ubuntu 4096 Oct 18 17:35 CMakeFiles
ubuntu@ubuntu:$
运行可执行文件
ubuntu@ubuntu:$ ./Tutorial 4294967296
The square root of 4.29497e+09 is 65536
ubuntu@ubuntu:$ ./Tutorial 10
The square root of 10 is 3.16228
ubuntu@ubuntu:$ ./Tutorial
./Tutorial Version 1.0
Usage: ./Tutorial number
ubuntu@ubuntu:$