Ubuntu cmake 编译环境搭建及编译方法 (lesson 01)

news/2025/2/5 22:08:32/

Linux cmake 使用方法

  • 下载cmake 并安装
  • 第一个程序

下载cmake 并安装

下面展示一些操作 内联代码片

sudo apt-get update
sudo apt-get install cmakelove@ubuntu:~$ cmake
Usagecmake [options] <path-to-source>cmake [options] <path-to-existing-build>Specify a source directory to (re-)generate a build system for it in the
current working directory.  Specify an existing build directory to
re-generate its build system.Run 'cmake --help' for more information.

下面查看一下cmake help

love@ubuntu:~$ cmake --help
Usagecmake [options] <path-to-source>cmake [options] <path-to-existing-build>Specify a source directory to (re-)generate a build system for it in the
current working directory.  Specify an existing build directory to
re-generate its build system.Options-C <initial-cache>           = Pre-load a script to populate the cache.-D <var>[:<type>]=<value>    = Create a cmake cache entry.-U <globbing_expr>           = Remove matching entries from CMake cache.-G <generator-name>          = Specify a build system generator.-T <toolset-name>            = Specify toolset name if supported bygenerator.-A <platform-name>           = Specify platform name if supported bygenerator.-Wdev                        = Enable developer warnings.-Wno-dev                     = Suppress developer warnings.-Werror=dev                  = Make developer warnings errors.-Wno-error=dev               = Make developer warnings not errors.-Wdeprecated                 = Enable deprecation warnings.-Wno-deprecated              = Suppress deprecation warnings.-Werror=deprecated           = Make deprecated macro and function warningserrors.-Wno-error=deprecated        = Make deprecated macro and function warningsnot errors.-E                           = CMake command mode.-L[A][H]                     = List non-advanced cached variables.--build <dir>                = Build a CMake-generated project binary tree.-N                           = View mode only.-P <file>                    = Process script mode.--find-package               = Run in pkg-config like mode.--graphviz=[file]            = Generate graphviz of dependencies, seeCMakeGraphVizOptions.cmake for more.--system-information [file]  = Dump information about this system.--debug-trycompile           = Do not delete the try_compile build tree.Only useful on one try_compile at a time.--debug-output               = Put cmake in a debug mode.--trace                      = Put cmake in trace mode.--trace-expand               = Put cmake in trace mode with variableexpansion.--trace-source=<file>        = Trace only this CMake file/module.  Multipleoptions allowed.--warn-uninitialized         = Warn about uninitialized values.--warn-unused-vars           = Warn about unused variables.--no-warn-unused-cli         = Don't warn about command line options.--check-system-vars          = Find problems with variable usage in systemfiles.--help,-help,-usage,-h,-H,/? = Print usage information and exit.--version,-version,/V [<f>]  = Print version number and exit.--help-full [<f>]            = Print all help manuals and exit.--help-manual <man> [<f>]    = Print one help manual and exit.--help-manual-list [<f>]     = List help manuals available and exit.--help-command <cmd> [<f>]   = Print help for one command and exit.--help-command-list [<f>]    = List commands with help available and exit.--help-commands [<f>]        = Print cmake-commands manual and exit.--help-module <mod> [<f>]    = Print help for one module and exit.--help-module-list [<f>]     = List modules with help available and exit.--help-modules [<f>]         = Print cmake-modules manual and exit.--help-policy <cmp> [<f>]    = Print help for one policy and exit.--help-policy-list [<f>]     = List policies with help available and exit.--help-policies [<f>]        = Print cmake-policies manual and exit.--help-property <prop> [<f>] = Print help for one property and exit.--help-property-list [<f>]   = List properties with help available andexit.--help-properties [<f>]      = Print cmake-properties manual and exit.--help-variable var [<f>]    = Print help for one variable and exit.--help-variable-list [<f>]   = List variables with help available and exit.--help-variables [<f>]       = Print cmake-variables manual and exit.GeneratorsThe following generators are available on this platform:Unix Makefiles               = Generates standard UNIX makefiles.Ninja                        = Generates build.ninja files.Watcom WMake                 = Generates Watcom WMake makefiles.CodeBlocks - Ninja           = Generates CodeBlocks project files.CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.CodeLite - Ninja             = Generates CodeLite project files.CodeLite - Unix Makefiles    = Generates CodeLite project files.Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.Sublime Text 2 - Unix Makefiles= Generates Sublime Text 2 project files.Kate - Ninja                 = Generates Kate project files.Kate - Unix Makefiles        = Generates Kate project files.Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.KDevelop3                    = Generates KDevelop 3 project files.KDevelop3 - Unix Makefiles   = Generates KDevelop 3 project files.

以下参考一下其他论坛的文档并实现测试
cmake 中文教程

第一个程序

源文件代码

#include <stdio.h>
#include <stdlib.h>/*** power - Calculate the power of number.* @param base: Base value.* @param exponent: Exponent value.** @return base raised to the power exponent.*/
double power(double base, int exponent)
{int result = base;int i;if (exponent == 0) {return 1;}for(i = 1; i < exponent; ++i){result = result * base;}return result;
}int main(int argc, char *argv[])
{if (argc < 3){printf("Usage: %s base exponent \n", argv[0]);return 1;}double base = atof(argv[1]);int exponent = atoi(argv[2]);double result = power(base, exponent);printf("%g ^ %d is %g\n", base, exponent, result);return 0;
}

CMakeLists.txt文件源码

# cmake lowest version required
cmake_minimum_required(VERSION 2.8)# project name
project(Demo_01)# assigned target object
add_executable(Demo_01 main.c)

在当前目录下执行cmake .,即可编译代码,可以查看整个编译过程

love@ubuntu:~/workspace/CMakeProject$ cmake .
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/love/workspace/CMakeProject

生成的makefile文件以及临时的文件如下

love@ubuntu:~/workspace/CMakeProject$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  main.c  Makefile

这里我们一一打开每个文件进行查看,用于了解其生成的原理及实质内容;
首先看makefile文件,其源代码如下:

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.10# 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/love/workspace/CMakeProject# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/love/workspace/CMakeProject#=============================================================================
# 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 -H$(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 "No interactive CMake dialog available..."/usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available.
.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/love/workspace/CMakeProject/CMakeFiles /home/love/workspace/CMakeProject/CMakeFiles/progress.marks$(MAKE) -f CMakeFiles/Makefile2 all$(CMAKE_COMMAND) -E cmake_progress_start /home/love/workspace/CMakeProject/CMakeFiles 0
.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) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend#=============================================================================
# Target rules for targets named Demo_01# Build rule for target.
Demo_01: cmake_check_build_system$(MAKE) -f CMakeFiles/Makefile2 Demo_01
.PHONY : Demo_01# fast build rule for target.
Demo_01/fast:$(MAKE) -f CMakeFiles/Demo_01.dir/build.make CMakeFiles/Demo_01.dir/build
.PHONY : Demo_01/fastmain.o: main.c.o.PHONY : main.o# target to build an object file
main.c.o:$(MAKE) -f CMakeFiles/Demo_01.dir/build.make CMakeFiles/Demo_01.dir/main.c.o
.PHONY : main.c.omain.i: main.c.i.PHONY : main.i# target to preprocess a source file
main.c.i:$(MAKE) -f CMakeFiles/Demo_01.dir/build.make CMakeFiles/Demo_01.dir/main.c.i
.PHONY : main.c.imain.s: main.c.s.PHONY : main.s# target to generate assembly for a file
main.c.s:$(MAKE) -f CMakeFiles/Demo_01.dir/build.make CMakeFiles/Demo_01.dir/main.c.s
.PHONY : main.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 "... Demo_01"@echo "... edit_cache"@echo "... main.o"@echo "... main.i"@echo "... main.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) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

最后执行make编译生成的可执行文件

love@ubuntu:~/workspace/CMakeProject$ make
Scanning dependencies of target Demo_01
[ 50%] Building C object CMakeFiles/Demo_01.dir/main.c.o
[100%] Linking C executable Demo_01
[100%] Built target Demo_01
love@ubuntu:~/workspace/CMakeProject$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  Demo_01  main.c  Makefile
love@ubuntu:~/workspace/CMakeProject$ 

运行可执行文件测试

love@ubuntu:~/workspace/CMakeProject$ ./Demo_01 10 3
10 ^ 3 is 1000
love@ubuntu:~/workspace/CMakeProject$ 

http://www.ppmy.cn/news/291974.html

相关文章

apple 西单大悦城维修_如何检查Apple是否已召回MacBook(免费维修)

apple 西单大悦城维修 Apple 苹果 Apple has recalled a lot of MacBooks recently. Your MacBook may be eligible for free replacement of its battery, keyboard, logic board, display backlight, or another component. Here’s how to check whether you can get some f…

vue使用高德地图--附带移动获取当前城市信息

高德地图 1.使用准备申请密钥vue使用 2.移动地图获取城市案例(注意事项)3.总结 1.使用准备 申请密钥 登录注册高德开放平台进入控制台 创建应用 申请key–生成key和安全密钥(2021之后key需要配合安全密钥使用) 注意&#xff1a;安全密钥需要在key之前 vue使用 首先在pubil…

CISP-PTE2022最新考试经验分享

CISP_PTE2022年10月份考试心得体会 2022年9月份由于公司需要&#xff0c;参加了中启航的CISPPTE培训&#xff0c;总培训时间八天&#xff0c;8师傅讲的很好&#xff0c;浅显易懂&#xff0c;经过4天的理论学习和4天的实操练习&#xff0c;经过十一假期的熟练&#xff0c;我在10…

【深入浅出 Spring Security(七)】RememberMe的实现原理详讲

RememberMe 的实现原理 一、RememberMe 的基本使用二、RememberMeAuthenticationFilter 源码分析RememberMeServicesTokenBasedRememberMeServicesTokenBasedRememberMeServices 中对 processAutoLoginCookie 方法的实现总结原理图式 三、提高安全性PersistentTokenBasedRememb…

基于Tensorflow+SDD+Python人脸口罩识别系统(深度学习)含全部工程源码及模型+视频演示+图片数据集

目录 前言总体设计系统整体结构图系统流程图 运行环境Python 环境Anaconda 环境搭建 模块实现1. 数据预处理2. 模型构建及算法实现3. 模型生成 系统测试1. 训练准确率2. 运行结果 工程源代码下载其它资料下载 前言 在当今全球范围内&#xff0c;新冠疫情对我们的生活方式带来了…

linux命令输出结果但不显示在屏幕上的通用办法

linux命令输出结果但不显示在屏幕上的通用办法 这个针对于我这种小白马大哈很简单的一个命令&#xff0c;记给自己备用 举个例子&#xff1a;unzip命令不输出结果 unzip xx.zip > /dev/null 2>&1 unzip xx.zip > /dev/null 前半部分是将标准输出重定向到空设备…

计算机能安装几个硬盘,一台电脑最多能接多少个硬盘?

一台电脑最多能接多少个硬盘&#xff0c;主要还是要看电脑主板硬盘接口的数量&#xff0c;由于m.2或msata接口会占用sata接口&#xff0c;一般电脑最多可以接多少块硬盘取决于电脑主板可独立使用的硬盘接口数。需要注意的是&#xff0c;如果电脑连接多个硬盘&#xff0c;需要机…

WPS免费账号分享(WPS会员+稻壳会员)

办公用到wps会员&#xff0c;随手开了个&#xff0c;用的不多&#xff0c;分享给大家一起用。 超级会员&#xff1d;wps会员稻壳会员&#xff0c;一般的功能应该都有了。加水印、转换pdf、简历模板、ppt模板、excel模板等 另外给大家分享15天稻壳的领取方法和pdf转word、exce…