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