cmake demo

hello world with cmake

编写源文件

// hello/main.cpp
#include <iostream>

int main(int argc, char** argv){
	std::cout << "Hello World" << std::endl;
	return 0;
}

编写cmake文件

创建文件CMakeList.txt,文件内容

project(HelloWorld)
cmake_minimum_required(VERSION 3.0)

add_executable(hello_world main.cpp)

里面的结构:

  • project名字
  • cmake最小版本号
  • 编译源文件main.cpp, 创建可执行文件hello_world

通过cmake命令进行编译

创建build目录

cd hello
mkdir build
cd build
cmake ..

显示如下信息

-- The C compiler identification is Clang 8.0.0
-- The CXX compiler identification is Clang 8.0.0
-- Check for working C compiler: /usr/bin/clang
-- Check for working C compiler: /usr/bin/clang -- 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/clang++
-- Check for working CXX compiler: /usr/bin/clang++ -- 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/xxx/hello/build

build目录生成文件如下

CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile

生成可执行文件

在build目录执行make命令,生成可执行文件hello_world,显示信息如下

Scanning dependencies of target hello_world
[ 50%] Building CXX object CMakeFiles/hello_world.dir/main.cpp.o
[100%] Linking CXX executable hello_world
[100%] Built target hello_world
./hello_world
Hello World

参考

[1] https://lappweb.in2p3.fr/~paubert/ASTERICS_HPC/2-2-100.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值