CMake初试

作者: 87d6dc4b11a7 | 来源:发表于2022-12-20 14:33 被阅读0次

最基本的 CMake 项目是从单个源代码文件构建的可执行文件。首先,创建一个 HelloWorld.cpp 文件。

#include<iostream>

int main(int argc, char *argv[]){
   std::cout << "Hello World!" << std::endl;
   return 0;
}

对于这样的简单项目,只需要一个包含三个命令的 CMakeLists.txt 文件。

cmake_minimum_required(VERSION 2.8.9)
project (hello)
add_executable(hello HelloWorld.cpp)

注意:虽然CMake支持大写、小写和混合大小写命令,但小写命令是首选。本文中即使用小写。

任何项目最顶层 CMakeLists.txt 必须从使用 cmake_minimum_required() 命令来指定 CMake 的最小版本开始,以此来确保 CMake 函数使用兼容版本的 CMake 来运行。
要启动一个项目,需要使用 project() 命令来设置项目名称。每个项目都需要,应该在cmake_minimum_required() 之后立即调用。
最后,add_executable() 命令告诉 CMake 使用指定的源代码文件创建一个可执行文件。
创建好项目后,接下来就可以构建和运行项目。通过运行 cmake 来配置项目

[root@localhost test]# ll
总用量 8
-rw-r--r-- 1 root root  91 12月 21 13:27 CMakeLists.txt
-rw-r--r-- 1 root root 114 12月 21 13:27 HelloWorld.cpp
[root@localhost test]# mkdir build
[root@localhost test]# mv CMakeLists.txt build/
[root@localhost test]# ll
总用量 4
drwxr-xr-x 2 root root  28 12月 21 14:10 build
-rw-r--r-- 1 root root 114 12月 21 13:27 HelloWorld.cpp
[root@localhost test]# cmake build/
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
  No CMAKE_C_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:2 (project):
  No CMAKE_CXX_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
See also "/root/test/CMakeFiles/CMakeOutput.log".
See also "/root/test/CMakeFiles/CMakeError.log".

根据提示安装C\C++编译器

yum install gcc gcc-c++

再次运行 cmake 来配置项目

[root@localhost test]# cmake build/
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /root/test
[root@localhost test]# ll
总用量 36
drwxr-xr-x 2 root root    28 12月 21 14:15 build
-rw-r--r-- 1 root root 14098 12月 21 14:17 CMakeCache.txt
drwxr-xr-x 5 root root  4096 12月 21 14:18 CMakeFiles
-rw-r--r-- 1 root root  1596 12月 21 14:17 cmake_install.cmake
-rw-r--r-- 1 root root   114 12月 21 13:27 HelloWorld.cpp
-rw-r--r-- 1 root root  5217 12月 21 14:18 Makefile

cmake命令后边跟的就是 CMakelist.txt 所在的目录,这个目录不必是当前目录,也可以新建一个 build 目录或者其他名字的目录来生成 build 文件,实际项目中也都是这么做的,这样代码会很干净也便于 git 管理。

接下来,执行 make 来实际编译链接项目,生成可执行程序。

[root@localhost test]# make
[ 50%] Building CXX object CMakeFiles/hello.dir/HelloWorld.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
[root@localhost test]# ll
总用量 48
drwxr-xr-x 2 root root    28 12月 21 14:15 build
-rw-r--r-- 1 root root 14098 12月 21 14:17 CMakeCache.txt
drwxr-xr-x 5 root root  4096 12月 21 14:24 CMakeFiles
-rw-r--r-- 1 root root  1596 12月 21 14:17 cmake_install.cmake
-rwxr-xr-x 1 root root  8976 12月 21 14:24 hello
-rw-r--r-- 1 root root   114 12月 21 13:27 HelloWorld.cpp
-rw-r--r-- 1 root root  5217 12月 21 14:18 Makefile
[root@localhost test]# ./hello 
Hello World!

也可以通过 cmake --build . 来实际编译链接项目。

[root@localhost test]# rm -rf hello 
[root@localhost test]# cmake --build .
[ 50%] Linking CXX executable hello
[100%] Built target hello
[root@localhost test]# ll
总用量 48
drwxr-xr-x 2 root root    28 12月 21 14:15 build
-rw-r--r-- 1 root root 14098 12月 21 14:17 CMakeCache.txt
drwxr-xr-x 5 root root  4096 12月 21 14:30 CMakeFiles
-rw-r--r-- 1 root root  1596 12月 21 14:17 cmake_install.cmake
-rwxr-xr-x 1 root root  8976 12月 21 14:30 hello
-rw-r--r-- 1 root root   114 12月 21 13:27 HelloWorld.cpp
-rw-r--r-- 1 root root  5217 12月 21 14:18 Makefile

相关文章

  • CMake初试

    最基本的 CMake 项目是从单个源代码文件构建的可执行文件。首先,创建一个 HelloWorld.cpp 文件。...

  • CMakeList.txt语法

    一. 初试 CMake 的 HelloWorld hello.cpp 和该CmakeList.txt同一个目录 二...

  • 9.Ubuntu安装cmake

    一、安装cmake 在cmake官网下载cmake:https://cmake.org/download/ 我没有...

  • CMake学习

    目录 什么是CMake? linux平台安装CMake CMake的helloworld 1. 什么是CMake?...

  • LNMP安装教程:安装mysql

    安装Mysql前需要安装cmake 1.安装cmake 下载cmake wget http://www.cmake...

  • CMake - 让人头痛的止痛药

    CMake 编译 CMake Manual CMake Tutorial User Interaction Gui...

  • 编译:CMAKE

    CMAKE 0、CMake CMake中,我们先输入cmake 命令对工程进行分析,生成makefile文件; 然...

  • CMake & make & Library

    CMake[https://cmake.org/documentation/] CMAKE_DL_LIBS[htt...

  • linux opencv4.2

    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX...

  • mac下opencv编译使用

    1、安装cmake brew install cmake brew link cmake 2、下载 https:/...

网友评论

    本文标题:CMake初试

    本文链接:https://www.haomeiwen.com/subject/lrauqdtx.html