ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cmake入门

2019-10-30 21:01:03  阅读:259  来源: 互联网

标签:cmake 入门 CXX helloc test Detecting compiler


在这里将运用cmake的内部构建和外部构建

首先创建一个文件夹

mkdir cmake_test

cd make_test

首先创建一个helloc.c文件

1 #include<stdio.h>                                                                 
2 
3 int main(int argc, char **agrv) {
4 
5     printf("Hello world from cMake pro1\n");
6 
7 
8     return 0; 
9 }

然后创建一个CMakeLists.txt文件

1 cmake_minimum_required(VERSION 3.10)                                              
2 PROJECT(Pro1)
3 SET(SRC_LIST helloc.c)
4 MESSAGE(STATUS"This is BINARY_dir" ${PROJECT_BINARY_DIR})
5 MESSAGE(STATUS"This is SOURCE_dir"${PROJECT_SOURCE_DIR} )
6 ADD_EXECUTABLE(helloc ${SRC_LIST} )

第一行:为了不让cmake出现错误,设置cmake的版本

第二行:设置项目的名称为Pro1,并可以指定文件类型(默认支持所有语言)该指令隐式的定义了两个cmake变量:<projectname>_BINARY_DIR以及<projectname>_SOURCE_DIR,内部编译的情况下,两个变量相同

前一个是可执行文件的目录,后一个是源代码的文件目录

第三行:SET指令相当于将SRC_LIST设置为helloc.c,如果有更多的源文件可用空格或分号隔开

第四行:MESSAGE  为打印指令,STATUS表明正常消息, ${PROJECT_BINARY_DIR} 可执行文件的文件目录

第五行:源文件的文家目录

第六行:生成可执行文件和$()中的文件依赖

 

开始构建

执行cmake CMakeLitsts生成makefile文件

miao@openlib:~/cmake_test$ cmake CMakeLists.txt 
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.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
STATUS"This is BINARY_dir"/home/miao/cmake_test
STATUS"This is SOURCE_dir"/home/miao/cmake_test
-- Configuring done
-- Generating done
-- Build files have been written to: /home/miao/cmake_test

cmake 生成的中间文件可以使用make clean来清除

这里make helloc可生成可执行文件

./helloc则可以执行

miao@openlib:~/cmake_test$ make helloc
Scanning dependencies of target helloc
[ 50%] Building C object CMakeFiles/helloc.dir/helloc.c.o
[100%] Linking C executable helloc
[100%] Built target helloc
miao@openlib:~/cmake_test$ ./helloc 
Hello world from cMake pro1

 

外部构建

先在该文件下创建一个build的文件夹

mkdir build

cd build

然后惊醒编译 cmake ..     ..是指上一层目录

直接make就生成了可执行文件

这样生成的中间文件就全部在build文件中

 

标签:cmake,入门,CXX,helloc,test,Detecting,compiler
来源: https://www.cnblogs.com/miaorn/p/11767848.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有