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. http://go.microsoft.com/fwlink/?linkid问题

    今天不知怎的,打开一个网页出现了http://go.microsoft.com/fwlink/?linkid=XXX 烦死了,在百度搜了下方法,什么改注册表,策略模板等不见起效.其实打开IE的设置, ...

  2. display的属性

    在一般的CSS布局制作时候,我们常常会用到display对应值有block.none.inline这三个值.,display这个属性用于定义建立布局时元素生成的显示框类型.对于 HTML 等文档类型, ...

  3. JqueryAjax 常用复制

    $.ajax({ type : "POST", url : "", data : {}, success : function(data) { } }); $. ...

  4. iOS音频学习笔记三:音频会话管理

    ​      使用Audio Session API ,可以指定App需要的音频行为,比如,当播放音频时,使得其他应用App静音或者混和在一起,也可以指定当App的音频被中断(例如被电话)时的行为,还 ...

  5. STM8 定时器

    中断映射表 对应stm8_interrupt.c #pragma vector=1 __interrupt void TRAP_IRQHandler(void) { } #pragma vector= ...

  6. Vue动画操作

    概述 Vue 在插入.更新或者移除 DOM 时,提供多种不同方式的应用过渡效果.包括以下工具: 在 CSS 过渡和动画中自动应用 class 可以配合使用第三方 CSS 动画库,如 Animate.c ...

  7. 【Java】接口和抽象类总结

    一.接口 1.1 成员变量(其实是常量) 1.2 方法 二.抽象类 2.1 成员变量 2.2 方法 一.接口 1.1 成员变量(其实是常量) 成员变量默认用 public static final修饰 ...

  8. git命令——git add

    如何理解git add git add命令本身并不复杂,字面意义上理解是“将一个文件添加到项目中“.但是这种理解有缺陷,有时候可能会出现某个文件同时存在暂存区域 和 非暂存区域(staged and ...

  9. Window Server 2008 概述

    Windows历史版本 Windows工作站发展历史 Windows95——>Windows98——>Windows2000——>WindowsXP——>Vista——> ...

  10. P2P system: FastTrack and BitTorrent

    FastTrack FastTrack来源于Gnutella,是Gnutella 和 Napster的杂交体 有些node承担了更重要的责任,这些nodes称为supernodes,因为这些改进,它比 ...