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. OpenCvSharp 图像旋转

    /// <summary> /// 图像旋转 /// </summary> private Mat MatRotate(Mat src, float angle) { Mat ...

  2. 解读生命密码的基本手段 ——DNA测序技术的前世今生

    解读生命密码的基本手段 ——DNA测序技术的前世今生 任鲁风  于军 (中国科学院基因组科学及信息重点实验室,北京基因组研究所) DNA(脱氧核糖核酸)和RNA(核糖核酸)是生命体的两种最基本组成物质 ...

  3. windows下批处理保留指定日期下的文件

    @echo offchcp 65001setlocal enabledelayedexpansion ::设置操作路径set "pic_dir=D:\465"echo 开始清理.. ...

  4. 一、hystrix如何集成在openfeign中使用

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 HystrixInvocationHandler hystrix是开源的一个熔断组件,s ...

  5. VBA Exit For语句

    当想要根据特定标准退出For循环时,就可以使用Exit For语句.当执行Exit For时,控件会立即跳转到For循环之后的下一个语句. 语法 以下是在VBA中Exit For语句的语法. Exit ...

  6. js控制台不同的打印方式

    在控制台单个输出: console.log(...):值 console.info(...):信息 console.debug(...):调试信息 console.warn(...):警告信息 con ...

  7. 配置java环境遇到的问题及解决方案

    第一个问题: 安装SDK过程中有过报错,就要注意文件是否齐全,我看了下sdk文件夹下的platforms里面是空的.打开sdk里面的SDK Manager.exe,勾选一个安卓版本进行下载即可. 第二 ...

  8. windows 下 redis 的安装及使用

    1.下载及安装redis 下载地址:https://github.com/dmajkic/redis/downloads 找到对应的版本下载安装 打开cmd窗口,用cd命令进入到安装redis的根目录 ...

  9. Ignatius and the Princess IV (简单DP,排序)

    方法一:    直接进行排序,输出第(n+1)/2位置上的数即可. (容易超时,关闭同步后勉强卡过) #include<iostream> #include<cstdio> # ...

  10. vue-cli 安装使用

    全局安装vue-cli,使用命令npm install -g vue-cli. 下载模板代码,使用命令vue init webpack my-project,之后会有一些询问,按需填写即可. 最后会看 ...