CMake | Ubuntu下编译、安装及使用cmake

目录

1.环境

2.编译、安装

3.使用

4.参考


1.环境

fly@fly-vm:~/workSpace/cmake$ uname -a
Linux fly-vm 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
fly@fly-vm:~/workSpace/cmake$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

如果机器上已经安装旧版本的cmake,先卸载:sudo apt-get --purge remove cmake

2.编译、安装

官网:https://cmake.org/

下载页面:https://cmake.org/download/

CMake 3.20 发行说明:https://cmake.org/cmake/help/v3.20/release/3.20.html

[fly@fly-vm cmake]$ wget https://github.com/Kitware/CMake/releases/download/v3.20.3/cmake-3.20.3.tar.gz
[fly@fly-vm cmake]$ tar -zxvf cmake-3.20.3.tar.gz
[fly@fly-vm cmake]$ cd cmake-3.20.3/
[fly@fly-vm cmake-3.20.3]$ ./bootstrap
[fly@fly-vm cmake-3.20.3]$ make
[fly@fly-vm cmake-3.20.3]$ sudo make install
[fly@fly-vm cmake-3.20.3]$ cmake --version
cmake version 3.20.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

3.使用

[fly@fly-vm HelloWorld]$ ls
CMakeLists.txt  main.c

一个简单的cmake文件:

#http://www.cppblog.com/Roger/archive/2011/11/17/160368.html
PROJECT(HELLO)
SET(SRC_LIST main.c)
MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir" ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

源代码:

/*******************************************************************
 *   > File Name: hello.c
 *   > Author: fly
 *   > Mail: [email protected]
 *   > Create Time: Wed Nov 22 15:50:25 2017
 ******************************************************************/

#include 

int main(int argc, char* argv[])
{
    printf("Hello world from main\n");
    return 0;
}

执行过程:

[fly@fly-vm HelloWorld]$ mkdir build
[fly@fly-vm HelloWorld]$ cd build/
[fly@fly-vm build]$ cmake .. .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- This is BINARY dir/home/fly/pub_work/12-other/CMake/HelloWorld/build
-- This is SOURCE dir/home/fly/pub_work/12-other/CMake/HelloWorld
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fly/pub_work/12-other/CMake/HelloWorld/build
[fly@fly-vm build]$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile
[fly@fly-vm build]$ make
[ 50%] Building C object CMakeFiles/hello.dir/main.c.o
[100%] Linking C executable hello
[100%] Built target hello
[fly@fly-vm build]$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  hello  Makefile
[fly@fly-vm build]$ ./hello
Hello world from main

cmake的使用说明:

ly@fly-vm:~/workSpace$ cmake --help
Usage

  cmake [options] 
  cmake [options] 
  cmake [options] -S  -B 

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
  -S           = Explicitly specify a source directory.
  -B            = Explicitly specify a build directory.
  -C            = Pre-load a script to populate the cache.
  -D [:]=    = Create or update a cmake cache entry.
  -U            = Remove matching entries from CMake cache.
  -G           = Specify a build system generator.
  -T             = Specify toolset name if supported by
                                 generator.
  -A            = Specify platform name if supported by
                                 generator.
  -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 warnings
                                 errors.
  -Wno-error=deprecated        = Make deprecated macro and function warnings
                                 not errors.
  --preset ,--preset=
                               = Specify a configure preset.
  --list-presets               = List available presets.
  -E                           = CMake command mode.
  -L[A][H]                     = List non-advanced cached variables.
  --build                 = Build a CMake-generated project binary tree.
  --install               = Install a CMake-generated project binary
                                 tree.
  --open                  = Open generated project in the associated
                                 application.
  -N                           = View mode only.
  -P                     = Process script mode.
  --find-package               = Legacy pkg-config like mode.  Do not use.
  --graphviz=[file]            = Generate graphviz of dependencies, see
                                 CMakeGraphVizOptions.cmake for more.
  --system-information [file]  = Dump information about this system.
  --log-level=
                               = Set the verbosity of messages from CMake
                                 files.  --loglevel is also accepted for
                                 backward compatibility reasons.
  --log-context                = Prepend log messages with context, if given
  --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.
  --debug-find                 = Put cmake find in a debug mode.
  --trace                      = Put cmake in trace mode.
  --trace-expand               = Put cmake in trace mode with variable
                                 expansion.
  --trace-format=
                               = Set the output format of the trace.
  --trace-source=        = Trace only this CMake file/module.  Multiple
                                 options allowed.
  --trace-redirect=      = Redirect trace output to a file instead of
                                 stderr.
  --warn-uninitialized         = Warn about uninitialized values.
  --no-warn-unused-cli         = Don't warn about command line options.
  --check-system-vars          = Find problems with variable usage in system
                                 files.
  --profiling-format=     = Output data for profiling CMake scripts.
                                 Supported formats: google-trace
  --profiling-output=    = Select an output path for the profiling data
                                 enabled through --profiling-format.
  --help,-help,-usage,-h,-H,/? = Print usage information and exit.
  --version,-version,/V []  = Print version number and exit.
  --help-full []            = Print all help manuals and exit.
  --help-manual  []    = Print one help manual and exit.
  --help-manual-list []     = List help manuals available and exit.
  --help-command  []   = Print help for one command and exit.
  --help-command-list []    = List commands with help available and exit.
  --help-commands []        = Print cmake-commands manual and exit.
  --help-module  []    = Print help for one module and exit.
  --help-module-list []     = List modules with help available and exit.
  --help-modules []         = Print cmake-modules manual and exit.
  --help-policy  []    = Print help for one policy and exit.
  --help-policy-list []     = List policies with help available and exit.
  --help-policies []        = Print cmake-policies manual and exit.
  --help-property  [] = Print help for one property and exit.
  --help-property-list []   = List properties with help available and
                                 exit.
  --help-properties []      = Print cmake-properties manual and exit.
  --help-variable var []    = Print help for one variable and exit.
  --help-variable-list []   = List variables with help available and exit.
  --help-variables []       = Print cmake-variables manual and exit.

Generators

The following generators are available on this platform (* marks default):
  Green Hills MULTI            = Generates Green Hills MULTI files
                                 (experimental, work-in-progress).
* Unix Makefiles               = Generates standard UNIX makefiles.
  Ninja                        = Generates build.ninja files.
  Ninja Multi-Config           = 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.
  Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
  Kate - Ninja                 = Generates Kate project files.
  Kate - Unix Makefiles        = Generates Kate project files.
  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                               = Generates Sublime Text 2 project files.

4.参考

1.Cmake的介绍和使用 Cmake实践

你可能感兴趣的:(1.C/C++编程,3.Linux系统使用,4.Linux,C编程)