https://www.cnblogs.com/coderfenghc/tag/cmake/  

https://cmake.org/cmake/help/v3.16/guide/tutorial/index.html#cmake-tutorial

https://www.ncnynl.com/category/ros-junior-tutorial/

https://blog.csdn.net/weixin_43455581/article/details/96306977#1_CPython_1

https://blog.csdn.net/wsc820508/article/details/81349675

最早的:https://www.ibm.com/developerworks/cn/linux/l-cn-cmake/

前言很多完全省略

1、编辑helloc.c文件:

 #include <stdio.h>

 int main(int argc, char **argv)
{
printf("Hello world from cMake pro1\n"); return ;
}

  代码非常简单,不多罗嗦。

2、编辑CMakeList.txt文件,这个是cmake的指导性文件:

 cmake_minimum_required(VERSION 3.15)
PROJECT(Pro1)
SET(SRC_LIST helloc.c)
MESSAGE(STATUS "This is BINARY_dir " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE_dir " ${PROJECT_SOURCE_DIR})
ADD_EXECUTABLE(helloc ${SRC_LIST})

  文件说明:对新手有用,老手请忽略

  第1行:主要是不让camke时,工具出现警告,可以不加,建议加上

  第2行:PROJECT指令指定项目名称为Rpo1,并暗含两个变量PROJECT_BINARY_DIR和PROJECT_SOURCE_DIR,前者是项目中可执行文件的目录名称,后者是项目的源代码目录名称;

  第3行:SET指令相当与定义并设置变量SRC_LIST的值为helloc.c,如果有更多的源文件,以空格或分号分割即可

  第4行:MESSAGE指令打印消息,STATUS表明正常消息,用${PROJECT_BINARY_DIR}获得PROJECT_BINARY_DIR的值,打印项目中可执行文件夹名称

  第5行:打印项目中源代码文件夹名称

  第6行:ADD_EXECUTABLE指令主要制定生成的可执行文件helloc和依赖列表,请务必使用 ${SRC_LIST},以获取依赖列表中的所有文件,而不是依赖文件SRC_LIST

3、第一次构建,生成Makefile文件,返回信息如下:

 -- The C compiler identification is GNU 9.2.
-- The CXX compiler identification is GNU 9.2.
-- 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
-- This is BINARY_dir /home/nication/WORKM/studyCode/toolCode/cMake/pro1
-- This is SOURCE_dir /home/nication/WORKM/studyCode/toolCode/cMake/pro1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nication/WORKM/studyCode/toolCode/cMake/pro1

  Makefile文件:

 # CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.15 # Default target executed when no arguments are given to make.
default_target: all .PHONY : default_target # Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL: #=============================================================================
# Special targets provided by cmake. # Disable implicit rules so canonical targets will work.
.SUFFIXES: # Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES = .SUFFIXES: .hpux_make_needs_suffix_list # Suppress display of executed commands.
$(VERBOSE).SILENT: # A target that is always out of date.
cmake_force: .PHONY : cmake_force #=============================================================================
# Set environment variables for the build. # The shell in which to execute make rules.
SHELL = /bin/sh # The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake # The command to remove a file.
RM = /usr/bin/cmake -E remove -f # Escaping for special characters.
EQUALS = = # The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/nication/WORKM/studyCode/toolCode/cMake/pro1 # The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/nication/WORKM/studyCode/toolCode/cMake/pro1 #=============================================================================
# Targets provided globally by CMake. # Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache # Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache .PHONY : rebuild_cache/fast # Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
/usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache # Special rule for the target edit_cache
edit_cache/fast: edit_cache .PHONY : edit_cache/fast # The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/nication/WORKM/studyCode/toolCode/cMake/pro1/CMakeFiles /home/nication/WORKM/studyCode/toolCode/cMake/pro1/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/nication/WORKM/studyCode/toolCode/cMake/pro1/CMakeFiles
.PHONY : all # The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean # The main clean target
clean/fast: clean .PHONY : clean/fast # Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall # Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast # clear depends
depend:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake
.PHONY : depend #=============================================================================
# Target rules for targets named helloc # Build rule for target.
helloc: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 helloc
.PHONY : helloc # fast build rule for target.
helloc/fast:
$(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/build
.PHONY : helloc/fast helloc.o: helloc.c.o .PHONY : helloc.o # target to build an object file
helloc.c.o:
$(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/helloc.c.o
.PHONY : helloc.c.o helloc.i: helloc.c.i .PHONY : helloc.i # target to preprocess a source file
helloc.c.i:
$(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/helloc.c.i
.PHONY : helloc.c.i helloc.s: helloc.c.s .PHONY : helloc.s # target to generate assembly for a file
helloc.c.s:
$(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/helloc.c.s
.PHONY : helloc.c.s # Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... rebuild_cache"
@echo "... helloc"
@echo "... edit_cache"
@echo "... helloc.o"
@echo "... helloc.i"
@echo "... helloc.s"
.PHONY : help #=============================================================================
# Special targets to cleanup operation of make. # Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake
.PHONY : cmake_check_build_system

4、二次构建并编译:

 make

  返回信息:

 Scanning dependencies of target helloc
[ %] Building C object CMakeFiles/helloc.dir/helloc.c.o
[%] Linking C executable helloc
[%] Built target helloc

  自动生成可执行文件,helloc,文件列表:

 -rw-r--r--  nication nication  10月  : CMakeCache.txt
drwxr-xr-x nication nication 10月 : CMakeFiles
-rw-r--r-- nication nication 10月 : cmake_install.cmake
-rw-r--r-- nication nication 10月 : CMakeLists.txt
-rwxr-xr-x nication nication 10月 : helloc
-rw-r--r-- nication nication 10月 : helloc.c
-rw-r--r-- nication nication 10月 : Makefile

5、执行程序:

  ./helloc

  执行结果:

  Hello world from cMake pro1

到此为止,算是成功了。但是由于cmake过程中会产生很多中间文件,使用make clean只能清除可执行文件。清除临时文件不方便,因此,尽量使用cmake的外部构建。

cmake入门之内部构建的更多相关文章

  1. cmake入门:01 构建一个简单的可执行程序

    一.目录结构 CMakeLists.txt:cmake 工程入口文件,包含当前目录下的工程组织信息.cmake 指令根据此文件生成相应的 MakeFile 文件. Hello.c: 源代码文件 bui ...

  2. CMake入门教程(转帖)

    本文转自:https://www.cnblogs.com/never--more/p/6921837.html CMake入门教程 参考文献:http://www.ibm.com/developerw ...

  3. C++ CMake 入门实战[转载]

    C++ CMake 入门实战 2016-11-05 CMake用于跨平台的编译系统,对于通常的c/c++工程,都是通过make来进行编译的,CMake可以通过指令生成Makefile文件来指导整个项目 ...

  4. pytorch入门2.2构建回归模型初体验(开始训练)

    pytorch入门2.x构建回归模型系列: pytorch入门2.0构建回归模型初体验(数据生成) pytorch入门2.1构建回归模型初体验(模型构建) pytorch入门2.2构建回归模型初体验( ...

  5. VScode 使用 CMake 入门

    参考 CMake 入门实战 在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下: 编写 CMake 配置文件 CMakeLists.txt . 执行命令 cmake PA ...

  6. CMake 入门实战 | HaHack

    CMake 入门实战 | HaHack undefined

  7. CMake入门(二)

    CMake入门(二) 最后更新日期:2014-04-25 by kagula 阅读前提:<CMake入门(一)>.Linux的基本操作 环境: Windows 8.1 64bit英文版.V ...

  8. CMake入门

    CMake入门 CMake是一个跨平台的安装编译工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似 ...

  9. C#游戏开发高速入门 2.1 构建游戏场景

    C#游戏开发高速入门 2.1  构建游戏场景 假设已经计划好了要编写什么样的游戏,在打开Unity以后.要做的第一件事情就是构建游戏场景(Scene).游戏场景就是玩家游戏时,在游戏视图中看到的一切. ...

随机推荐

  1. ReLU函数的缺陷

    ReLU激活功能并不完美. 它有一个被称为 “ReLU 死区” 的问题:在训练过程中,一些神经元会“死亡”,即它们停止输出 0 以外的任何东西.在某些情况下,你可能会发现你网络的一半神经元已经死亡,特 ...

  2. SHELL字符串处理技巧(${}、##、%%)

      在SHELL编程中,经常要处理一些字符串变量.比如,计算长度啊.截取子串啊.字符替换啊等等,常常要用到awk.expr.sed.tr等命令.下面给大家介绍个简单的字符串处理方法,用不着嵌套复杂的子 ...

  3. vue中计算属性和侦听属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Linux开机自动启动服务

    当我们的Linux,关机后,或者重启后,有些服务需要人工启动才能有.为了解决这个问题,我找了一个办法,用一个脚本,开机启动想启动的服务. 方法一:(强烈推荐) 1.写一个脚本auto.sh vim  ...

  5. 五、DML操作汇总

    前言: DML(Data Manipulation Language)数据操作语言,以INSERT.UPDATE.DELETE三种指令为核心,分别代表插入.更新与删除,是必须要掌握的指令,DML和SQ ...

  6. 虚拟机VMware安装

    1.进入VMware官网,下载 (点击进入官网) 2.下载镜像文件 (点击进入官网下载) 3.下载好后,打开VMware,点击创建新的虚拟机 4.点击下一步,并找到刚才下载好的镜像文件 5.跟随系统点 ...

  7. Linux 之 用户、用户组以及权限

    拥有者(user),拥有组(group),其他人(other) 由于Linux是一个多人多任务的系统,因此经常会出现同一台机器同时有多个人进行操作,为了考虑每个人的隐私权以及每个人喜好的工作环境,所以 ...

  8. vue项目 npm install 安装依赖 特别慢 解决办法

    使用NPM(Node.js包管理工具)安装依赖时速度特别慢,为了安装Express,执行命令后两个多小时都没安装成功,最后只能取消安装,笔者20M带宽,应该不是我网络的原因,后来在网上找了好久才找到一 ...

  9. 刷题小tips

    自从认真做题以后,结合自己的做题体验以及网上前人的总结经验贴,罗列下列的小建议: 1.为了避免每一次都要重复输入测试数据的麻烦,利用输入重定向文件 我是只把标准输入stdin重定位到in.txt,然后 ...

  10. linux网络编程之socket编程(九)

    转眼又快到十一月份了,北京已经是完全进入冬天的节奏,外面冷风嗖嗖的,不过在夜深人静之时,学习永远成了我最快乐的时光,只有此时会觉得自己是如此踏实,虽说白天工作也是编一天程,但是此时的编程,是一种业余爱 ...