Exercise4

本文展示了如何使用CMake构建一个C++项目,该项目包含一个主程序和一个名为MathFunctions的库。CMakeLists.txt文件用于设置最小CMake版本、项目信息、源文件、库链接和头文件路径。程序中,如果定义了USE_MYMATH,则使用自定义的平方根函数,否则使用标准库的sqrt函数。
摘要由CSDN通过智能技术生成

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@
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"
#include "MathFunctions.h"
    
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
#ifdef USE_MYMATH
  const double outputValue = mysqrt(inputValue);
#else
  const double outputValue = sqrt(inputValue);
#endif

  std::cout << "The square root of " << inputValue << " is " << outputValue
            << std::endl;
  return 0;
}
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 : Set the variable CMAKE_CXX_STANDARD to 11

#    and the variable CMAKE_CXX_REQUIRED_STANDARD to True

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# TODO : Use configure_file to configure and copy TutorialConfig.h.in to

#         TutorialConfig.h

configure_file(TutorialConfig.h.in TutorialConfig.h)


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


# 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
target_link_libraries(Tutorial PUBLIC MathFunctions)

# TODO : Use target_include_directories to include ${PROJECT_BINARY_DIR}

target_include_directories(Tutorial PUBLIC
                           ${PROJECT_BINARY_DIR}
                           ${PROJECT_SOURCE_DIR}/MathFunctions
                          )


ubuntu@ubuntu:$ 

MathFunctions directory

ubuntu@ubuntu:$ ls -tlr
total 12
-rw-r--r-- 1 ubuntu ubuntu 456 Oct 12 22:26 mysqrt.cxx
-rw-r--r-- 1 ubuntu ubuntu 128 Oct 18 09:59 CMakeLists.txt
-rw-r--r-- 1 ubuntu ubuntu  94 Oct 18 10:01 MathFunctions.h
ubuntu@ubuntu:$ 

MathFunctions Cxx file

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

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

  double result = 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:$

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)
ubuntu@ubuntu:$

构建cmake工程

ubuntu@ubuntu:$ ls -trl
total 16
-rw-r--r-- 1 ubuntu ubuntu  211 Oct 12 22:26 TutorialConfig.h.in
-rw-r--r-- 1 ubuntu ubuntu  934 Oct 18 09:56 tutorial.cxx
drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 18 10:01 MathFunctions
-rw-r--r-- 1 ubuntu ubuntu 1715 Oct 18 10:12 CMakeLists.txt
ubuntu@ubuntu:$ mkdir exercise4_build
ubuntu@ubuntu:$ cd exercise4_build/
ubuntu@ubuntu:$ cmake ..
-- Build files have been written to: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step2/exercise4_build
ubuntu@ubuntu:$ cmake --build .
ubuntu@ubuntu:
ubuntu@ubuntu:$ ls -tlr
total 56
-rw-r--r-- 1 ubuntu ubuntu   165 Oct 18 10:13 TutorialConfig.h
-rw-rw-r-- 1 ubuntu ubuntu 14669 Oct 18 10:13 CMakeCache.txt
-rw-rw-r-- 1 ubuntu ubuntu  6061 Oct 18 10:13 Makefile
-rw-rw-r-- 1 ubuntu ubuntu  1963 Oct 18 10:13 cmake_install.cmake
drwxrwxr-x 3 ubuntu ubuntu  4096 Oct 18 10:13 MathFunctions
-rwxrwxr-x 1 ubuntu ubuntu 15008 Oct 18 10:13 Tutorial
drwxrwxr-x 6 ubuntu ubuntu  4096 Oct 18 10:13 CMakeFiles
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:$
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:$
待办事项列表是一种用来记录和管理个人或团队需要完成的任务的工具。在网络与分布式计算中,我们可以利用网络和分布式计算技术来优化待办事项列表的管理和实时协作。 首先,网络技术可以使待办事项列表实现跨设备和跨平台的同步和共享。用户可以通过网络连接到云端服务器或者使用云服务来存储和同步他们的待办事项列表。这样,无论用户使用哪个设备或者平台,他们都可以随时随地访问和更新他们的待办事项。 其次,分布式计算技术可以通过将任务分配给多个计算节点来实现待办事项列表的并行处理和提高处理速度。分布式计算能够将复杂的任务分解成多个子任务,并将这些子任务分配给多个计算节点同时处理。通过这种方式,待办事项可以更加高效地进行处理和完成。 另外,分布式计算还可以实现待办事项列表的实时协作和共享。不同用户可以通过网络连接到共享的计算节点,共同编辑和更新待办事项列表。这种实时协作的模式可以让团队成员之间实时交流和共享任务进展,提高团队的协作效率。 总而言之,网络和分布式计算技术可以为待办事项列表的管理和实时协作提供强大的支持。利用这些技术,我们可以随时随地访问和更新待办事项,同时可以利用分布式计算来提高任务处理的效率和实现实时协作。这些优化可以有助于提高个人和团队的工作效率,确保待办事项能够及时完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值