cmake从入门到精通(一)

BAT架构师资料下载:https://github.com/0voice/from_coder_to_expert
[toc]

概述

本项目的目的是逐步掌握cmake的使用,从最基本的单文件开始,到复杂工程的搭建能力。

实践

案例1-单文件构建

参考:https://cmake.org/cmake-tutorial/

对应代码:01-Tutorial

The most basic project is an executable built from source code files. For simple projects a two line CMakeLists.txt file is all that is required. This will be the starting point for our tutorial. The CMakeLists.txt file looks like:

cmake_minimum_required (VERSION 2.8)
project (01-Tutorial)
add_executable(tutorial 01-Tutorial.cpp)

Note that this example uses lower case commands in the CMakeLists.txt file. Upper, lower, and mixed case commands are supported by CMake. The source code for tutorial.cxx will compute the square root of a number and the first version of it is very simple, as follows:

// A simple program that computes the square root of a number
#include 
#include 
#include 

int main (int argc, char *argv[])
{
    double inputValue;
    if(argc == 2)
    {
        inputValue = atof(argv[1]);
    }
    else
    {
        inputValue = 4;
    }
    double outputValue = sqrt(inputValue);
    fprintf(stdout,"The square root of %g is %g\n",
            inputValue, outputValue);
    return 0;
}

解析

cmake_minimum_required

Set the minimum required version of cmake for a project.

cmake_minimum_required(VERSION major[.minor[.patch[.tweak]]]
                       [FATAL_ERROR])

比如

cmake_minimum_required (VERSION 2.8)

project

参考地址:https://cmake.org/cmake/help/git-stage/command/project.html
Set the name of the project.

project( [LANGUAGES] [...])
project(
        [VERSION [.[.[.]]]]
        [LANGUAGES ...])

指定项目的名称。项目最终编译生成的可执行文件并不一定是这个项目名称,而是由另一条命令(add_executable)确定的,稍候我们再介绍。

add_executable

Add an executable to the project using the specified source files.

add_executable( [WIN32] [MACOSX_BUNDLE]
               [EXCLUDE_FROM_ALL]
               [source1] [source2 ...])

定义了这个工程会生成一个文件名为 name的可执行文件

案例2-单文件+版本号构建

参考:https://cmake.org/cmake-tutorial/

对应代码:02-Tutorial
The first feature we will add is to provide our executable and project with a version number. While you can do this exclusively in the source code, doing it in the CMakeLists.txt file provides more flexibility. To add a version number we modify the CMakeLists.txt file as follows:

cmake_minimum_required (VERSION 2.8)
project (02-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"
  )
 
# 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 02-Tutorial.cpp)

Since the configured file will be written into the binary tree we must add that directory to the list of paths to search for include files. We then create a TutorialConfig.h.in file in the source tree with the following contents:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

When CMake configures this header file the values for @Tutorial_VERSION_MAJOR@ and @Tutorial_VERSION_MINOR@ will be replaced by the values from the CMakeLists.txt file. Next we modify tutorial.cxx to include the configured header file and to make use of the version numbers. The resulting source code is listed below.

// A simple program that computes the square root of a number
#include 
#include 
#include 
#include "TutorialConfig.h"

int main (int argc, char *argv[])
{

    fprintf(stdout,"%s Version %d.%d\n",
                argv[0],
                Tutorial_VERSION_MAJOR,
                Tutorial_VERSION_MINOR);

    double inputValue;
    if(argc == 2)
    {
        inputValue = atof(argv[1]);
    }
    else
    {
        inputValue = 4;
    }
    double outputValue = sqrt(inputValue);
    fprintf(stdout,"The square root of %g is %g\n",
            inputValue, outputValue);
    return 0;
}

解析

set

Set a normal, cache, or environment variable to a given value.
设置变量
参考:https://cmake.org/cmake/help/git-stage/command/set.html?highlight=set

  • Set Normal Variable
    set( ... [PARENT_SCOPE])
  • Set Cache Entry
    set( ... CACHE [FORCE])
  • Set Environment Variable
    set(ENV{} ...)

PROJECT_SOURCE_DIR

Top level source directory for the current project.
即是工程的顶级目录

PROJECT_BINARY_DIR

Full path to build directory for project.
即是编译目录,比如如果你创建了build目录(cd build和cmake ..),则路径为:

PROJECT_SOURCE_DIR/build

如果在工程顶级目录直接进行编译(cmake .)则和PROJECT_SOURCE_DIR一致

案例3-单文件+库文件调用

顶层目录内的文件内容

先编译库文件

库文件放在MathFunctions目录。
Now we will add a library to our project. This library will contain our own implementation for computing the square root of a number. The executable can then use this library instead of the standard square root function provided by the compiler. For this tutorial we will put the library into a subdirectory called MathFunctions. It will have the following one line CMakeLists.txt file:

  1. 添加CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
add_library(MathFunctions mysqrt.cpp)
  1. 添加头文件MathFunctions.h
#ifndef __MATH_FUNCTION_H__
#define __MATH_FUNCTION_H__
double mysqrt(double input);
#endif
  1. 添加实现文件
#include 
#include 
double mysqrt(double input)
{
    printf("call mysqrt\n");
    return sqrt(input);
}

此时目录文件为:
  1. 创建build目录并进行编译
mkdir build
cd build
cmake ..
make

此时build目录下生成libMathFunctions.a文件,将其拷贝到上一级目录(即是MathFunctions)

cp libMathFunctions.a ../
cd ..
ls
#可以看到当前libMathFunctions目录的内容
CMakeLists.txt  MathFunctions.cpp  MathFunctions.h  build  libMathFunctions.a

编译main函数所在文件

  1. 顶层目录CMakeLists.txt文件
cmake_minimum_required (VERSION 2.8)
project (03-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"
  )
 
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 

 
# add the executable
add_executable(tutorial 03-Tutorial.cpp)
target_link_libraries (tutorial MathFunctions)

  1. main函数所在文件
    03-Tutorial.cpp
// A simple program that computes the square root of a number
#include 
#include 
#include 
#include "TutorialConfig.h"
#include "MathFunctions.h"

int main (int argc, char *argv[])
{

    fprintf(stdout,"%s Version %d.%d\n",
                argv[0],
                Tutorial_VERSION_MAJOR,
                Tutorial_VERSION_MINOR);

    double inputValue;
    if(argc == 2)
    {
        inputValue = atof(argv[1]);
    }
    else
    {
        inputValue = 4;
    }
    double outputValue = mysqrt(inputValue);
    fprintf(stdout,"The square root of %g is %g\n",
            inputValue, outputValue);
    return 0;
}
  1. 编译和执行
mkdir build
cd build
cmake ..
make

执行文件./tutorial

lqf@ubuntu:/mnt/hgfs/linux/multimedia/src/project/cmake_learn/03-Tutorial/build$ ./tutorial 
./tutorial Version 1.0
call mysqrt
The square root of 4 is 2

参考文档

[1] cmake-tutorial

你可能感兴趣的:(cmake从入门到精通(一))