cmake简单使用

概述

cmake是一种跨平台编译工具,除了可以编译c,c++代码也可以编译其他语言的代码,其主要就是通过cmake执行CMakeLists.txt从而生成Makefile。下面就自己了解到的简单的一点知识,做以记录。更多可查看官网:https://cmake.org。

cmake简单使用

  1. 创建项目文件夹。
    在终端输入指令:mkdir testCmake-helloworld
  2. 创建项目文件及CMakeLists.txt。
    在终端输入:
    cd testCmake-helloworld
    touch CMakeLists.txt
    vim main.cpp
    打开main.cpp后,在其中写入如下内容:
    main.cppmain.cpp
#include <iostream>

using namespace std;

int main(int argc,char*argv[])
{
	cout<<"hello world!!"<<endl;
	return 0;
}

保存后退出。
终端输入:vim CMakeLists.txt
向CMakeLists.txt写入如下内容:

cmake_minimum_required(VERSION 2.8)
project(hello)
add_executable(hello main.cpp)

保存退出,查看当前的文件列表:
$ ls
CMakeLists.txt build-hello-cmake main.cpp
可以看到有两个文件CMakeLists.txt 和main.cpp,一个文件夹build-hello-cmake,此时可以进入到构建目录build-hello-cmake中,输入下述指令:

cmake ..

终端会显示如下:

CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


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

接着终端输入:

make

终端显示如下:

[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

此时构建完成,在目录build-hello-cmake中生成可执行程序hello。当前路径下执行下述指令:

# admin @ bogon in ~/Program/testCmake-helloworld/build-hello-cmake [17:21:33] 
$ ./hello
hello world!!

会输出程序的运行结果。
上述是cmake执行构建的过程,总体来说就是下列指令:

mkdir 构建目录名
cd 构建目录名
cmake CMakeLists.txt所在的路径
make 

其构建项目最常用的四句指令就是上述所写。首先在项目目录下创建一个空文件夹,作为构建目录,即:mkdir 构建目录名;然后进入构建目录,即:cd 构建目录名;接下来执行cmake,但是在cmake的后面需要指明CMakeLists.txt所在的路径,最后执行make指令,整个项目生成可执行程序。
当不在构建目录里执行cmake时,可以按照下述方法来执行cmake:

admin @ bogon in ~/Program/testCmake-helloworld [17:31:29] 
$ cmake -S ./ -B ./build-hello-cmake 
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


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

其中-S指明了源文件所在的路径,-B指明了构建生成的文件所在的路径。
这样就可以在非构建目录下执行cmake,但生成的文件依旧存入到构建目录中。同样接下来也在项目目录下使用指令make,指令如下:

# admin @ bogon in ~/Program/testCmake-helloworld [17:33:09] 
$ make -C ./build-hello-cmake 
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

make指令中-C指明了生成可执行文件所在的路径。
以上仅供参考。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值