• cmake入门之内部构建


    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文件:

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

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

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

    1 cmake_minimum_required(VERSION 3.15)
    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})

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

      第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文件,返回信息如下:

     1 -- The C compiler identification is GNU 9.2.0
     2 -- The CXX compiler identification is GNU 9.2.0
     3 -- Check for working C compiler: /usr/bin/cc
     4 -- Check for working C compiler: /usr/bin/cc -- works
     5 -- Detecting C compiler ABI info
     6 -- Detecting C compiler ABI info - done
     7 -- Detecting C compile features
     8 -- Detecting C compile features - done
     9 -- Check for working CXX compiler: /usr/bin/c++
    10 -- Check for working CXX compiler: /usr/bin/c++ -- works
    11 -- Detecting CXX compiler ABI info
    12 -- Detecting CXX compiler ABI info - done
    13 -- Detecting CXX compile features
    14 -- Detecting CXX compile features - done
    15 -- This is BINARY_dir /home/nication/WORKM/studyCode/toolCode/cMake/pro1
    16 -- This is SOURCE_dir /home/nication/WORKM/studyCode/toolCode/cMake/pro1
    17 -- Configuring done
    18 -- Generating done
    19 -- Build files have been written to: /home/nication/WORKM/studyCode/toolCode/cMake/pro1

      Makefile文件:

      1 # CMAKE generated file: DO NOT EDIT!
      2 # Generated by "Unix Makefiles" Generator, CMake Version 3.15
      3 
      4 # Default target executed when no arguments are given to make.
      5 default_target: all
      6 
      7 .PHONY : default_target
      8 
      9 # Allow only one "make -f Makefile2" at a time, but pass parallelism.
     10 .NOTPARALLEL:
     11 
     12 
     13 #=============================================================================
     14 # Special targets provided by cmake.
     15 
     16 # Disable implicit rules so canonical targets will work.
     17 .SUFFIXES:
     18 
     19 
     20 # Remove some rules from gmake that .SUFFIXES does not remove.
     21 SUFFIXES =
     22 
     23 .SUFFIXES: .hpux_make_needs_suffix_list
     24 
     25 
     26 # Suppress display of executed commands.
     27 $(VERBOSE).SILENT:
     28 
     29 
     30 # A target that is always out of date.
     31 cmake_force:
     32 
     33 .PHONY : cmake_force
     34 
     35 #=============================================================================
     36 # Set environment variables for the build.
     37 
     38 # The shell in which to execute make rules.
     39 SHELL = /bin/sh
     40 
     41 # The CMake executable.
     42 CMAKE_COMMAND = /usr/bin/cmake
     43 
     44 # The command to remove a file.
     45 RM = /usr/bin/cmake -E remove -f
     46 
     47 # Escaping for special characters.
     48 EQUALS = =
     49 
     50 # The top-level source directory on which CMake was run.
     51 CMAKE_SOURCE_DIR = /home/nication/WORKM/studyCode/toolCode/cMake/pro1
     52 
     53 # The top-level build directory on which CMake was run.
     54 CMAKE_BINARY_DIR = /home/nication/WORKM/studyCode/toolCode/cMake/pro1
     55 
     56 #=============================================================================
     57 # Targets provided globally by CMake.
     58 
     59 # Special rule for the target rebuild_cache
     60 rebuild_cache:
     61     @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
     62     /usr/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
     63 .PHONY : rebuild_cache
     64 
     65 # Special rule for the target rebuild_cache
     66 rebuild_cache/fast: rebuild_cache
     67 
     68 .PHONY : rebuild_cache/fast
     69 
     70 # Special rule for the target edit_cache
     71 edit_cache:
     72     @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
     73     /usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
     74 .PHONY : edit_cache
     75 
     76 # Special rule for the target edit_cache
     77 edit_cache/fast: edit_cache
     78 
     79 .PHONY : edit_cache/fast
     80 
     81 # The main all target
     82 all: cmake_check_build_system
     83     $(CMAKE_COMMAND) -E cmake_progress_start /home/nication/WORKM/studyCode/toolCode/cMake/pro1/CMakeFiles /home/nication/WORKM/studyCode/toolCode/cMake/pro1/CMakeFiles/progress.marks
     84     $(MAKE) -f CMakeFiles/Makefile2 all
     85     $(CMAKE_COMMAND) -E cmake_progress_start /home/nication/WORKM/studyCode/toolCode/cMake/pro1/CMakeFiles 0
     86 .PHONY : all
     87 
     88 # The main clean target
     89 clean:
     90     $(MAKE) -f CMakeFiles/Makefile2 clean
     91 .PHONY : clean
     92 
     93 # The main clean target
     94 clean/fast: clean
     95 
     96 .PHONY : clean/fast
     97 
     98 # Prepare targets for installation.
     99 preinstall: all
    100     $(MAKE) -f CMakeFiles/Makefile2 preinstall
    101 .PHONY : preinstall
    102 
    103 # Prepare targets for installation.
    104 preinstall/fast:
    105     $(MAKE) -f CMakeFiles/Makefile2 preinstall
    106 .PHONY : preinstall/fast
    107 
    108 # clear depends
    109 depend:
    110     $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
    111 .PHONY : depend
    112 
    113 #=============================================================================
    114 # Target rules for targets named helloc
    115 
    116 # Build rule for target.
    117 helloc: cmake_check_build_system
    118     $(MAKE) -f CMakeFiles/Makefile2 helloc
    119 .PHONY : helloc
    120 
    121 # fast build rule for target.
    122 helloc/fast:
    123     $(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/build
    124 .PHONY : helloc/fast
    125 
    126 helloc.o: helloc.c.o
    127 
    128 .PHONY : helloc.o
    129 
    130 # target to build an object file
    131 helloc.c.o:
    132     $(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/helloc.c.o
    133 .PHONY : helloc.c.o
    134 
    135 helloc.i: helloc.c.i
    136 
    137 .PHONY : helloc.i
    138 
    139 # target to preprocess a source file
    140 helloc.c.i:
    141     $(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/helloc.c.i
    142 .PHONY : helloc.c.i
    143 
    144 helloc.s: helloc.c.s
    145 
    146 .PHONY : helloc.s
    147 
    148 # target to generate assembly for a file
    149 helloc.c.s:
    150     $(MAKE) -f CMakeFiles/helloc.dir/build.make CMakeFiles/helloc.dir/helloc.c.s
    151 .PHONY : helloc.c.s
    152 
    153 # Help Target
    154 help:
    155     @echo "The following are some of the valid targets for this Makefile:"
    156     @echo "... all (the default if no target is provided)"
    157     @echo "... clean"
    158     @echo "... depend"
    159     @echo "... rebuild_cache"
    160     @echo "... helloc"
    161     @echo "... edit_cache"
    162     @echo "... helloc.o"
    163     @echo "... helloc.i"
    164     @echo "... helloc.s"
    165 .PHONY : help
    166 
    167 
    168 
    169 #=============================================================================
    170 # Special targets to cleanup operation of make.
    171 
    172 # Special rule to run CMake to check the build system integrity.
    173 # No rule that depends on this can have commands that come from listfiles
    174 # because they might be regenerated.
    175 cmake_check_build_system:
    176     $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
    177 .PHONY : cmake_check_build_system

    4、二次构建并编译:

    1 make

      返回信息:

    1 Scanning dependencies of target helloc
    2 [ 50%] Building C object CMakeFiles/helloc.dir/helloc.c.o
    3 [100%] Linking C executable helloc
    4 [100%] Built target helloc

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

    1 -rw-r--r-- 1 nication nication 13433 10月 30 09:16 CMakeCache.txt
    2 drwxr-xr-x 5 nication nication  4096 10月 30 09:53 CMakeFiles
    3 -rw-r--r-- 1 nication nication  1546 10月 30 09:16 cmake_install.cmake
    4 -rw-r--r-- 1 nication nication   229 10月 30 09:15 CMakeLists.txt
    5 -rwxr-xr-x 1 nication nication 16536 10月 30 09:53 helloc
    6 -rw-r--r-- 1 nication nication   110 10月 30 08:56 helloc.c
    7 -rw-r--r-- 1 nication nication  4839 10月 30 09:16 Makefile

    5、执行程序:

      ./helloc

      执行结果:

      Hello world from cMake pro1

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

  • 相关阅读:
    js调试工具
    学习之compare:c#和c++中各种compare方法
    individual program总结2.0
    individual program总结
    C语言编程—自动生成四则运算升级版
    jQuery Validate(一)
    Java_得到GET和POST请求URL和参数列表
    jQuery Validate(二)
    MySQL_使用时遇到的问题汇总
    jQuery Validate(三)
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/11763326.html
Copyright © 2020-2023  润新知