https://www.cnblogs.com/coderfenghc/tag/cmake/ html
https://cmake.org/cmake/help/v3.16/guide/tutorial/index.html#cmake-tutoriallinux
https://www.ncnynl.com/category/ros-junior-tutorial/c++
https://blog.csdn.net/weixin_43455581/article/details/96306977#1_CPython_1shell
https://blog.csdn.net/wsc820508/article/details/81349675api
最先的:https://www.ibm.com/developerworks/cn/linux/l-cn-cmake/ide
前言不少徹底省略工具
一、編輯helloc.c文件:ui
1 #include <stdio.h> 2 3 int main(int argc, char **argv) 4 { 5 printf("Hello world from cMake pro1\n"); 6 7 return 0; 8 }
代碼很是簡單,很少羅嗦。this
二、編輯CMakeList.txt文件,這個是cmake的指導性文件:spa
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
三、第一次構建,生成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
四、二次構建並編譯:
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
五、執行程序:
./helloc
執行結果:
Hello world from cMake pro1
到此爲止,算是成功了。可是因爲cmake過程當中會產生不少中間文件,使用make clean只能清除可執行文件。清除臨時文件不方便,所以,儘可能使用cmake的外部構建。