Exercise1

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 <cstdlib> 
#include <iostream>
#include <string>


int main(int argc, char* argv[])
{
  if (argc < 2) {
    std::cout << "Usage: " << argv[0] << " number" << std::endl;
    return 1;
  }
  // convert input to double
  const double inputValue = atof(argv[1]);

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

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
project(Tutorial)

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

ubuntu@ubuntu:$ 

构建cmake工程

ubuntu@ubuntu:$ ls -tlr
total 12
-rw-r--r-- 1 ubuntu ubuntu 775 Oct 12 22:26 tutorial.cxx
-rw-r--r-- 1 ubuntu ubuntu 679 Oct 17 16:50 CMakeLists.txt
ubuntu@ubuntu:$ mkdir exercise1_build
ubuntu@ubuntu:$ ls -tlr
total 16
-rw-r--r-- 1 ubuntu ubuntu  775 Oct 12 22:26 tutorial.cxx
-rw-r--r-- 1 ubuntu ubuntu  679 Oct 17 16:50 CMakeLists.txt
drwxrwxr-x 2 ubuntu ubuntu 4096 Oct 17 17:04 exercise1_build
ubuntu@ubuntu:$ cd exercise1_build/
ubuntu@ubuntu:$ cmake ../
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/study/cmake-learning/cmake-3.25.0-rc1-tutorial-source/Step1/exercise1_build
ubuntu@ubuntu:$ ls -tlr
total 32
-rw-rw-r-- 1 ubuntu ubuntu 14355 Oct 17 17:05 CMakeCache.txt
-rw-rw-r-- 1 ubuntu ubuntu  5549 Oct 17 17:07 Makefile
-rw-rw-r-- 1 ubuntu ubuntu  1730 Oct 17 17:07 cmake_install.cmake
drwxrwxr-x 6 ubuntu ubuntu  4096 Oct 17 17:07 CMakeFiles
ubuntu@ubuntu:$ cmake --build .
[ 50%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
ubuntu@ubuntu:$

运行可执行文件


```bash
ubuntu@ubuntu:$ ls -tlr
total 48
-rw-rw-r-- 1 ubuntu ubuntu 14355 Oct 17 17:05 CMakeCache.txt
-rw-rw-r-- 1 ubuntu ubuntu  5549 Oct 17 17:07 Makefile
-rw-rw-r-- 1 ubuntu ubuntu  1730 Oct 17 17:07 cmake_install.cmake
-rwxrwxr-x 1 ubuntu ubuntu 13152 Oct 17 17:08 Tutorial
drwxrwxr-x 6 ubuntu ubuntu  4096 Oct 17 17:08 CMakeFiles
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
Usage: ./Tutorial number
ubun@ubuntu:$ pwd
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值