CMake HelloWorld

 (一)CMake使用

CMake使用
1.注释
    # 这是一个CMakeLists.txt文件
    cmake_minimum_required(VERSION 3.10)

2.add_executable 定义工程会生成一个可执行程序
    add_executable(可执行程序名 源文件名称)

    # 样式1:
    add_executable(app add.c div.c main.c mult.c sub.c)

(二)HelloWorld 项目

heheda@linux:~/Linux/HelloWorld$ tree
.
├── build
├── CMakeLists.txt
└── helloworld.cpp

1 directory, 2 files
  • helloworld.cpp
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
    cout<<"Hello World"<<endl;
    return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HELLOWORLD)
add_executable(app helloworld.cpp) # g++ helloworld.cpp -o app
  • 命令执行 
执行命令:
1.mkdir build
2.cd build
3.cmake ..
4.make
5./app


执行结果:
heheda@linux:~/Linux/HelloWorld$ cd build
heheda@linux:~/Linux/HelloWorld/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /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: /usr/bin/c++
-- Check for working CXX compiler: /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: /home/heheda/Linux/HelloWorld/build
heheda@linux:~/Linux/HelloWorld/build$ make
Scanning dependencies of target app
[ 50%] Building CXX object CMakeFiles/app.dir/helloworld.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
heheda@linux:~/Linux/HelloWorld/build$ ./app
Hello World
heheda@linux:~/Linux/HelloWorld/build$

 

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-13 16:28:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-13 16:28:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-13 16:28:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-13 16:28:01       20 阅读

热门阅读

  1. 一系列实用工具、编程工具和学习网站推荐

    2024-01-13 16:28:01       36 阅读
  2. CNN和RNN的区别是什么?

    2024-01-13 16:28:01       36 阅读
  3. vue2使用富文本wangeditor

    2024-01-13 16:28:01       39 阅读
  4. MySQL深入——11

    2024-01-13 16:28:01       27 阅读
  5. HCIP-3

    HCIP-3

    2024-01-13 16:28:01      27 阅读
  6. Dockerfile基本结构及编写详解

    2024-01-13 16:28:01       34 阅读
  7. 统计出现过一次的公共字符串

    2024-01-13 16:28:01       36 阅读