Exercise14

Configure.h.in file

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"
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif


int main(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;
    return 1;
  }

  // convert input to double
  // std::stod是C++ 11标准函数
  const double inputValue = std::stod(argv[1]);

  // calculate square root
  const double outputValue = mysqrt(inputValue);
  std::cout << "The square root of " << inputValue << " is " << outputValue
            << std::endl;
  return 0;
}
ubuntu@ubuntu:$

Licenese.txt file

ubuntu@ubuntu:$ vim License.txt 
ubuntu@ubuntu:$ cat License.txt 
This is the open source License.txt file introduced in
CMake/Tutorial/Step9...
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 helper variables to determine which compiler we are using:

# * Create a new variable gcc_like_cxx that is true if we are using CXX and

#   any of the following compilers: ARMClang, AppleClang, Clang, GNU, LCC

# * Create a new variable msvc_cxx that is true if we are using CXX and MSVC

# Hint: Use set() and COMPILE_LANG_AND_ID

set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU,LCC>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")


# TODO : Add warning flag compile options to the interface library

# tutorial_compiler_flags.

# * For gcc_like_cxx, add flags -Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused

# * For msvc_cxx, add flags -W3

# Hint: Use target_compile_options()

target_compile_options(tutorial_compiler_flags INTERFACE
            "$<${gcc_like_cxx}:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>"
            "$<${msvc_cxx}:-W3>"
            )

# TODO 7: With nested generator expressions, only use the flags for the

# build-tree

# Hint: Use BUILD_INTERFACE

target_compile_options(tutorial_compiler_flags INTERFACE
          "$<${gcc_like_cxx}:$<BUILD_INTERFACE:-Wall;-Wextra;-Wshadow;-Wformat=2;-Wunused>>"
           "$<${msvc_cxx}:$<BUILD_INTERFACE:-W3>>"
            )


# 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}
                          )

# TODO : Install Tutorial in the bin directory

# Hint: Use the TARGETS and DESTINATION parameters

install(TARGETS Tutorial DESTINATION bin)

# TODO : Install Tutorial.h to the include directory

# Hint: Use the FILES and DESTINATION parameters

install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
        DESTINATION include
        )

# TODO : Enable testing

# enable_testing()

include(CTest)

# TODO : Add a test called Runs which runs the following command:

# $ Tutorial 25

add_test(NAME Runs COMMAND Tutorial 4)

# TODO : Add a test called Usage which runs the following command:

# $ Tutorial

# Make sure the expected output is displayed.

# Hint: Use the PASS_REGULAR_EXPRESSION property with "Usage.*number"

add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
                     PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
                     )

# TODO : Add a test which runs the following command:

# $ Tutorial 4

# Make sure the result is correct.

# Hint: Use the PASS_REGULAR_EXPRESSION property with "4 is 2"

add_test(NAME StandardUse COMMAND Tutorial 4)
set_tests_properties(StandardUse
          PROPERTIES PASS_REGULAR_EXPRESSION "4 is 2"
          )

# TODO : Add more tests. Create a function called do_test to avoid copy +

# paste. Test the following values: 4, 9, 5, 7, 25, -25 and 0.00001.

function(do_test target arg result)
    add_test(NAME Comp${arg} COMMAND ${target} ${arg})
    set_tests_properties(Comp${arg}
                        PROPERTIES PASS_REGULAR_EXPRESSION ${result}
                        )
endfunction()

# do a bunch of result based tests

do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")

include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_DIRECTORY ${PROJECT_BINARY_DIR}/pack)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
set(CPACK_SOURCE_GENERATOR "TXZ")

include(CPack)

ubuntu@ubuntu:$

CTestConfig.cmake file

ubuntu@ubuntu:$ ls -tlr
total 24
-rw-r--r-- 1 ubuntu ubuntu  904 Oct 12 22:26 tutorial.cxx
-rw-r--r-- 1 ubuntu ubuntu  188 Oct 12 22:26 TutorialConfig.h.in
-rw-r--r-- 1 ubuntu ubuntu  245 Oct 12 22:26 CTestConfig.cmake
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 19 15:19 MathFunctions
-rw-r--r-- 1 ubuntu ubuntu 2329 Oct 19 15:20 CMakeLists.txt
ubuntu@ubuntu:$ vim CTestConfig.cmake 
ubuntu@ubuntu:$ cat CTestConfig.cmake 
set(CTEST_PROJECT_NAME "CMakeTutorial")
set(CTEST_NIGHTLY_START_TIME "00:00:00 EST")

set(CTEST_DROP_METHOD "http")
set(CTEST_DROP_SITE "my.cdash.org")
set(CTEST_DROP_LOCATION "/submit.php?project=CMakeTutorial")
set(CTEST_DROP_SITE_CDASH TRUE)
ubuntu@ubuntu:$ 

MathFunctions directory

ubuntu@ubuntu:$ ls -tlr
total 28
-rw-r--r-- 1 ubuntu ubuntu  904 Oct 12 22:26 tutorial.cxx
-rw-r--r-- 1 ubuntu ubuntu  188 Oct 12 22:26 TutorialConfig.h.in
-rw-r--r-- 1 ubuntu ubuntu   79 Oct 12 22:26 License.txt
-rw-r--r-- 1 ubuntu ubuntu  245 Oct 12 22:26 CTestConfig.cmake
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 12 22:26 MathFunctions
-rw-r--r-- 1 ubuntu ubuntu 2655 Oct 20 11:42 CMakeLists.txt
ubuntu@ubuntu:$ 

MathFunctions Cxx file

ubuntu@ubuntu:$ vim mysqrt.cxx 
ubuntu@ubuntu:$ cat mysqrt.cxx 
#include <iostream>

#include "MathFunctions.h"
#include "Table.h"

// a hack square root calculation using simple operations
double mysqrt(double x)
{
  if (x <= 0) {
    return 0;
  }

  // if we have both log and exp then use them
  double result = x;
  if (x >= 1 && x < 10) {
    std::cout << "Use the table to help find an initial value " << std::endl;
    result = sqrtTable[static_cast<int>(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;
}
ubuntu@ubuntu:$ 

MathFunctions header file

ubuntu@ubuntu:$ vim MathFunctions.h 
ubuntu@ubuntu:$ cat MathFunctions.h 
#ifndef __MATHFUNCTION___H__H
#define __MATHFUNCTION___H__H

double mysqrt(double x);

#endif
ubuntu@ubuntu:$

MakeTable Cxx file

ubuntu@ubuntu:$ vim MakeTable.cxx 
ubuntu@ubuntu:$ cat MakeTable.cxx 
// A simple program that builds a sqrt table
#include <cmath>
#include <fstream>
#include <iostream>

int main(int argc, char* argv[])
{
  // make sure we have enough arguments
  if (argc < 2) {
    return 1;
  }

  std::ofstream fout(argv[1], std::ios_base::out);
  const bool fileOpen = fout.is_open();
  if (fileOpen) {
    fout << "double sqrtTable[] = {" << std::endl;
    for (int i = 0; i < 10; ++i) {
      fout << sqrt(static_cast<double>(i)) << "," << std::endl;
    }
    // close the table with a zero
    fout << "0};" << std::endl;
    fout.close();
  }
  return fileOpen ? 0 : 1; // return 0 if wrote the file
}
ubuntu@ubuntu:$ 

MathFunctions CMake file

ubuntu@ubuntu:$ vim CMakeLists.txt 
ubuntu@ubuntu:$ cat CMakeLists.txt 
add_executable(MakeTable MakeTable.cxx)

add_library(MathFunctions mysqrt.cxx
            ${CMAKE_CURRENT_BINARY_DIR}/Table.h
        )

# 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}
          PRIVATE  ${CMAKE_CURRENT_BINARY_DIR}
          )

# link our compiler flags interface library

target_link_libraries(MathFunctions tutorial_compiler_flags)

add_custom_command(
          OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
          COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
          DEPENDS MakeTable
          )

# does this system provide the log and exp functions?

# install libs

set(installable_libs MathFunctions tutorial_compiler_flags)
install(TARGETS ${installable_libs} DESTINATION lib)

# install include headers

install(FILES MathFunctions.h DESTINATION include)
ubuntu@ubuntu:$ ls -tlr
total 16
-rw-r--r-- 1 ubuntu ubuntu  25 Oct 12 22:26 MathFunctions.h
-rw-r--r-- 1 ubuntu ubuntu 627 Oct 12 22:26 MakeTable.cxx
-rw-r--r-- 1 ubuntu ubuntu 948 Oct 19 19:07 CMakeLists.txt
-rw-r--r-- 1 ubuntu ubuntu 703 Oct 19 19:10 mysqrt.cxx
ubuntu@ubuntu:$ 

构建cmake工程

ubuntu@ubuntu:$ mkdir exercise14_build
ubuntu@ubuntu:$ cd exercise14_build
ubuntu@ubuntu:$ cmake -DUSE_MYMATH=ON ..
-- Build files have been written to: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step9/exercise14_build
ubuntu@ubuntu:$ cmake --build .
ubuntu@ubuntu:$ ls -tlr
total 104
-rw-r--r--  1 ubuntu ubuntu   137 Oct 20 11:42 TutorialConfig.h
drwxrwxr-x  3 ubuntu ubuntu  4096 Oct 20 11:42 Testing
-rw-r--r--  1 ubuntu ubuntu  2686 Oct 20 11:42 DartConfiguration.tcl
-rw-r--r--  1 ubuntu ubuntu  4151 Oct 20 11:42 CPackSourceConfig.cmake
-rw-r--r--  1 ubuntu ubuntu  3687 Oct 20 11:42 CPackConfig.cmake
-rw-rw-r--  1 ubuntu ubuntu 18119 Oct 20 11:42 CMakeCache.txt
-rw-rw-r--  1 ubuntu ubuntu 23911 Oct 20 11:42 Makefile
-rw-rw-r--  1 ubuntu ubuntu  4770 Oct 20 11:42 CTestTestfile.cmake
-rw-rw-r--  1 ubuntu ubuntu  3064 Oct 20 11:42 cmake_install.cmake
drwxrwxr-x  3 ubuntu ubuntu  4096 Oct 20 11:42 MathFunctions
-rwxrwxr-x  1 ubuntu ubuntu 15136 Oct 20 11:42 Tutorial
drwxrwxr-x 34 ubuntu ubuntu  4096 Oct 20 11:42 CMakeFiles
ubuntu@ubuntu:$ cpack 
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial []
CPack: Create package
CPack: - package: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step9/exercise14_build/pack/Tutorial-1.0-Linux.sh generated.
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial []
CPack: Create package
CPack: - package: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step9/exercise14_build/pack/Tutorial-1.0-Linux.tar.gz generated.
CPack: Create package using TZ
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial []
CPack: Create package
CPack: - package: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step9/exercise14_build/pack/Tutorial-1.0-Linux.tar.Z generated.
ubuntu@ubuntu:$ ls -tlr
total 112
-rw-r--r--  1 ubuntu ubuntu   137 Oct 20 11:42 TutorialConfig.h
drwxrwxr-x  3 ubuntu ubuntu  4096 Oct 20 11:42 Testing
-rw-r--r--  1 ubuntu ubuntu  2686 Oct 20 11:42 DartConfiguration.tcl
-rw-r--r--  1 ubuntu ubuntu  4151 Oct 20 11:42 CPackSourceConfig.cmake
-rw-r--r--  1 ubuntu ubuntu  3687 Oct 20 11:42 CPackConfig.cmake
-rw-rw-r--  1 ubuntu ubuntu 18119 Oct 20 11:42 CMakeCache.txt
-rw-rw-r--  1 ubuntu ubuntu 23911 Oct 20 11:42 Makefile
-rw-rw-r--  1 ubuntu ubuntu  4770 Oct 20 11:42 CTestTestfile.cmake
-rw-rw-r--  1 ubuntu ubuntu  3064 Oct 20 11:42 cmake_install.cmake
drwxrwxr-x  3 ubuntu ubuntu  4096 Oct 20 11:42 MathFunctions
-rwxrwxr-x  1 ubuntu ubuntu 15136 Oct 20 11:42 Tutorial
drwxrwxr-x 34 ubuntu ubuntu  4096 Oct 20 11:42 CMakeFiles
-rw-rw-r--  1 ubuntu ubuntu   640 Oct 20 11:42 install_manifest.txt
drwxrwxr-x  3 ubuntu ubuntu  4096 Oct 20 11:42 pack
ubuntu@ubuntu:$ 

ubuntu@ubuntu:$ cd pack/
ubuntu@ubuntu:$ ls -trl
total 36
drwxrwxr-x 3 ubuntu ubuntu  4096 Oct 20 11:42 _CPack_Packages
-rwxrwxrwx 1 ubuntu ubuntu 10230 Oct 20 11:42 Tutorial-1.0-Linux.sh
-rw-rw-r-- 1 ubuntu ubuntu  6201 Oct 20 11:42 Tutorial-1.0-Linux.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu  9048 Oct 20 11:42 Tutorial-1.0-Linux.tar.Z
ubuntu@ubuntu:$ vim Tutorial-1.0-Linux.sh 
ubuntu@ubuntu:$ tar -tvf Tutorial-1.0-Linux.tar.gz 
drwxrwxr-x ubuntu/ubuntu     0 2022-10-20 11:42 Tutorial-1.0-Linux/include/
-rw-r--r-- ubuntu/ubuntu    25 2022-10-12 22:26 Tutorial-1.0-Linux/include/MathFunctions.h
-rw-r--r-- ubuntu/ubuntu   137 2022-10-20 11:42 Tutorial-1.0-Linux/include/TutorialConfig.h
drwxrwxr-x ubuntu/ubuntu     0 2022-10-20 11:42 Tutorial-1.0-Linux/bin/
-rwxr-xr-x ubuntu/ubuntu 15136 2022-10-20 11:42 Tutorial-1.0-Linux/bin/Tutorial
drwxrwxr-x ubuntu/ubuntu     0 2022-10-20 11:42 Tutorial-1.0-Linux/lib/
-rw-r--r-- ubuntu/ubuntu  3874 2022-10-20 11:42 Tutorial-1.0-Linux/lib/libMathFunctions.a
ubuntu@ubuntu:$ 

运行可执行文件

ubuntu@ubuntu:$ ./Tutorial 4294967296
Computing sqrt of 4.29497e+09 to be 2.14748e+09
Computing sqrt of 4.29497e+09 to be 1.07374e+09
Computing sqrt of 4.29497e+09 to be 5.36871e+08
Computing sqrt of 4.29497e+09 to be 2.68435e+08
Computing sqrt of 4.29497e+09 to be 1.34218e+08
Computing sqrt of 4.29497e+09 to be 6.71089e+07
Computing sqrt of 4.29497e+09 to be 3.35545e+07
Computing sqrt of 4.29497e+09 to be 1.67773e+07
Computing sqrt of 4.29497e+09 to be 8.38878e+06
Computing sqrt of 4.29497e+09 to be 4.19465e+06
The square root of 4.29497e+09 is 4.19465e+06
ubuntu@ubuntu:$ ./Tutorial 10
Computing sqrt of 10 to be 5.5
Computing sqrt of 10 to be 3.65909
Computing sqrt of 10 to be 3.19601
Computing sqrt of 10 to be 3.16246
Computing sqrt of 10 to be 3.16228
Computing sqrt of 10 to be 3.16228
Computing sqrt of 10 to be 3.16228
Computing sqrt of 10 to be 3.16228
Computing sqrt of 10 to be 3.16228
Computing sqrt of 10 to be 3.16228
The square root of 10 is 3.16228
ubuntu@ubuntu:$ ./Tutorial
./Tutorial Version 1.0
Usage: ./Tutorial number
ubuntu@ubuntu:$ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值