ubuntu下cmake学习

Posted 小小的小孩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu下cmake学习相关的知识,希望对你有一定的参考价值。

这个系列的博客是是从cmake 官方学来的,相当于是他的中文版吧,原版请看https://cmake.org/cmake-tutorial/

官方教程有7步,我打算只讲(fanyi)前2步,后面的基本都类似,看官方教程即可。

Step 1:新建一个可执行程序

首先确保你已经安装了cmake 和 g++,如果没有安装,就:

sudo apt-get install cmake g++

然后准备一个工作空间,并准备一些素材:

mkdir learn_cmake_again
cd learn_cmake_again
touch CMakeLists.txt
touch tutoria.cpp

然后给文件加内容:

#CMakeLists.txt文件内容
#版本号
cmake_minimum_required (VERSION 2.8)
#项目名
project (Tutorial)
#生成可执行程序 语法:add_executable(可执行程序名  要编译的cpp)
add_executable(Tutorial tutorial.cpp)
//cpp文件中的内容
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"Usage: %s number\\n",argv[0]);
    return 1;
    }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\\n",
          inputValue, outputValue);
  return 0;
}

现在就可以尝试链接编译成可执行程序了

cmake .
make 
./Tutorial 16

结果很明显是对的,哈哈,接下来一个任务就要往程序里面增加一点变量


 

CMakeLists.txt中的内容为:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
# 将版本号设置为变量,它具体的作用会在第六步讲到 set (Tutorial_VERSION_MAJOR
1) set (Tutorial_VERSION_MINOR 0) # configure a header file to pass some of the CMake settings # to the source code #.h.in文件是可以自动编译的文件,通过下面这个命令可以得到.h文件
# 关于PROJECT_BINARY_DIR和PROJECT_SOURCE_DIR的区别可以查看:http://blog.csdn.net/sukhoi27smk/article/details/46388711,本教程中就不做区分了 configure_file (
"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" "${PROJECT_BINARY_DIR}/TutorialConfig.h" ) # add the binary tree to the search path for include files # so that we will find TutorialConfig.h include_directories("${PROJECT_BINARY_DIR}") # add the executable add_executable(Tutorial tutorial.cpp)
//cpp文件里的内容
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
 
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d\\n",
            argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR);
    fprintf(stdout,"Usage: %s number\\n",argv[0]);
    return 1;
    }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\\n",
          inputValue, outputValue);
  return 0;
}
//TutorialConfig.h.in(需要新建)文件里的内容
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

然后:

cmake .
make

就会看到自动生成了TutorialConfig.h文件,而且此时cpp里还可以打印出CMakeLists.txt里定义的变量,纵观来看,.in文件相当于是一个桥梁,架起了cmakelists中的变量到cpp的传递。

平时看项目时的CMakeLists.txt可没那么简单,经常会包含新建库,然后链接库的步骤。


 

Step 2:添加一个库

此时我们需要改一下文件结构了,如图

子目录

以下是各个文件的内容:

#外部的CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
 
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  )
# should we use our own math functions?
# 相当于是个flag option (USE_MYMATH "Use tutorial provided math implementation" ON) # add the binary tree to the search path for include files # so that we will find TutorialConfig.h # add the MathFunctions library? # if (USE_MYMATH) include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions") add_subdirectory (MathFunctions) #将MathFunctions中的子目录也包含进去 set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) # set语法: set(<变量> <值> <值>),把值赋给变量,多个值的话,就把多个值拼接赋给变量 endif (USE_MYMATH) # add the executable add_executable (Tutorial tutorial.cpp) target_link_libraries (Tutorial ${EXTRA_LIBS})
//tutorial.cpp的内容
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif
 
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d\\n", argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR);
    fprintf(stdout,"Usage: %s number\\n",argv[0]);
    return 1;
    }
 
  double inputValue = atof(argv[1]);
//根据外部的情况,选择用哪一部分程序,在实际项目中经常用到,比如有多个版本的opencv时,就可以由多个选择,提高兼容性 #ifdef USE_MYMATH
double outputValue = mysqrt(inputValue); #else double outputValue = sqrt(inputValue); #endif fprintf(stdout,"The square root of %g is %g\\n", inputValue, outputValue); return 0; }
//TutorialConfig.h.in文件内容,.h文件不用写会自动生成
// the configured options and settings for Tutorial
//它的作用主要是用于生成.h文件,所以具体看.h文件体会其作用即可 #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ #cmakedefine USE_MYMATH
#子文件夹中的CMakeLists.txt
add_library(MathFunctions mysqrt.cpp)
//MathFunctions.h内容
#include<iostream>
#include<math.h>
double mysqrt(double a) ;
//mysqrt.cpp内容
#include<iostream>
#include<math.h>
#include<MathFunctions.h>
using namespace std;

double mysqrt(double a) {
cout<<"its my sqrt"<<endl;
return sqrt(a);
}

之后cmake . 和make,就可以了

  

以上是关于ubuntu下cmake学习的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu 20.04下搭建C++ & OpenCV 4.6.0 & cmake编译

转载:Ubuntu14.04下升级cmake工具

ubuntu下升级CMake

ubuntu源码编译安装cmake-2.8.10.2

Vscode 在ubuntu下 cmake-tools提示有误

ubuntu下如何安装opencv-2.4.3