CMake入门-02-HelloWorld扩展

Posted binglingziyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CMake入门-02-HelloWorld扩展相关的知识,希望对你有一定的参考价值。

工作环境

  • 系统:macOS Mojave 10.14.6
  • CMake: Version 3.15.0-rc4

Hello,World! 扩展-同一目录,多个源文件

(1) 新建 hello 目录,创建文件 CMakeLists.txt、main.cpp、MathFunctions.h、MathFunctions.cpp

$ mkdir hello
$ cd hello
$ touch CMakeLists.txt main.cpp MathFunctions.h MathFunctions.cpp
$ ll
-rw-r--r--  1 staff  staff   124B  8 14 17:19 CMakeLists.txt
-rw-r--r--  1 staff  staff     0B  8 15 16:22 MathFunctions.cpp
-rw-r--r--  1 staff  staff     0B  8 15 16:22 MathFunctions.h
-rw-r--r--@ 1 staff  staff   145B  8 14 21:33 main.cpp

(2) 编写 MathFunctions.h

int power(int base, int exponent);

(3) 编写 MathFunctions.cpp

#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"

int power(int base, int exponent) {
    int result = base;
    int i;

    if (exponent == 0) {
        return 1;
    }

    for(i = 1; i < exponent; ++i){
        result = result * base;
    }
    return result;
}

(4) 编写 main.cpp

#include <iostream>
#include "MathFunctions.h"

using namespace std;

int main(int argc, char const *argv[]) {
  /* code */
  // cout << "Hello,World!" << power(2, 3) << endl;
  printf("%s power(2,3)=%d 
", "Hello,World!", power(2, 3));
  return 0;
}

(5) 编写 CMakeLists.txt

# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0)

# 项目名
project(hello)

# 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
# set(SRC_LIST main.cpp MathFunctions.h MathFunctions.cpp)
aux_source_directory(. SRC_LIST)

# 指定生成目标
add_executable(hello ${SRC_LIST})

(6) 编译运行

$ mkdir build
$ cd build
$ pwd
/Users/staff/Desktop/hello/build

$ cmake ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/staff/Desktop/hello/build

$ make
Scanning dependencies of target hello
[ 33%] Building CXX object CMakeFiles/hello.dir/MathFunctions.cpp.o
[ 66%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

$ ./hello
Hello,World! power(2,3)=8

Hello,World! 扩展-多个目录,多个源文件

(1) 新建 hello 目录,创建文件 CMakeLists.txt、main.cpp、math/MathFunctions.h、math/MathFunctions.cpp

$ mkdir hello
$ cd hello
$ mkdir math
$ touch CMakeLists.txt main.cpp math/MathFunctions.h math/MathFunctions.cpp
$ tree
.
├── CMakeLists.txt
├── main.cpp
└── math
    ├── MathFunctions.cpp
    └── MathFunctions.h

(2) 编写代码

  • math/MathFunctions.h
int power(int base, int exponent);
  • math/MathFunctions.cpp
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"

int power(int base, int exponent) {
    int result = base;
    int i;

    if (exponent == 0) {
        return 1;
    }

    for(i = 1; i < exponent; ++i){
        result = result * base;
    }
    return result;
}
  • main.cpp
#include <iostream>
// 这里要加上 math 目录
#include "math/MathFunctions.h"

using namespace std;

int main(int argc, char const *argv[]) {
  /* code */
  // cout << "Hello,World!" << power(2, 3) << endl;
  printf("%s power(2,3)=%d 
", "Hello,World!", power(2, 3));
  return 0;
}
  • CMakeLists.txt
# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0)

# 项目名
project(hello)

# 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
aux_source_directory(. SRC_LIST)
# 查找 math 目录下的所有源文件,并将名称保存到 MATH_SRC_LIST 变量
aux_source_directory(${PROJECT_SOURCE_DIR}/math MATH_SRC_LIST)

# 指定生成目标
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

(3) 再多学一点

由于多了 math 目录,我们看到 main.cpp 中,#include "math/MathFunctions.h" 也要加上目录。

如果我们不想加 math 目录,直接 #include "MathFunctions.h",操作如下:

  • main.cpp
#include <iostream>
// 这里去掉 math 目录
#include "MathFunctions.h"

using namespace std;

int main(int argc, char const *argv[]) {
  /* code */
  // cout << "Hello,World!" << power(2, 3) << endl;
  printf("%s power(2,3)=%d 
", "Hello,World!", power(2, 3));
  return 0;
}
  • CMakeLists.txt 添加 include_directories
# CMake 最低版本号要求
cmake_minimum_required(VERSION 3.15.0)

# 项目名
project(hello)

# 查找当前目录下的所有源文件,并将名称保存到 SRC_LIST 变量
aux_source_directory(. SRC_LIST)
# 查找 math 目录下的所有源文件,并将名称保存到 MATH_SRC_LIST 变量
aux_source_directory(${PROJECT_SOURCE_DIR}/math MATH_SRC_LIST)

# 添加头文件路径
include_directories(${PROJECT_SOURCE_DIR}/math)

# 指定生成目标
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

相关参考:
CMake 官方网站
CMake 多个源文件-多个目录

联系作者:
技术图片


以上是关于CMake入门-02-HelloWorld扩展的主要内容,如果未能解决你的问题,请参考以下文章

CMake入门——简单CMakeLists的编写

CMake入门

CMake入门

CMake使用入门

CMake入门实战

CMake 入门与进阶