CMake Tutorial(5)——Adding a Generated File and Generator

这一节,我将向你展示如何将一个生成的源文件添加至应用程序的构建过程中。在本次示例,我们会创建一个预计算数值平方根的表(文本文件),将其作为构建过程的一部分,然后将这个表编译到你的应用程序中。为了实现该功能,我需要一个用于生成该表的程序,我们在MathFunctions目录中创建一个MakeTable.cxx文件内容如下:

这里写代码片// A simple program that builds a sqrt table
#include <cstdio>
#include <cstdlib>
#include <cmath>

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

    if (argc < 2) {
        return 1;
    }

    FILE *fout = fopen(argv[1], "w");
    if (!fout) {
        return 1;
    }

    // create a source file with a table of square roots
    fprintf(fout, "double sqrtTable[] = {\n");
    for (int i=0; i<10; i++) {
        result = sqrt(static_cast<double>(i));
        fprintf(fout, "%g, \n", result);
    }

    // close the table with a zero
    fprintf(fout, "0}; \n");
    fclose(fout);

    return 0;
}

程序通过接受argv[1]作为文件名并将数据写入。接下来我们需要在MathFunctions文件下的CMakeLists.txt文件中加入合适的命令去构建MakeTable的可执行文件,并且让它作为构建过程的一部分运行。需要添加的命令如下:

# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)

# add the command to generate the source code
add_custom_command (
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  DEPENDS MakeTable
  )

# add the binary tree directory to the search path for 
# include files
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )

# add the main library
add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h  )

首先我们像添加其他可执行文件一样添加MakeTable,接着我们添加一个自定义命令用于指定如何通过运行MakeTable来产生文件Table.h(CMAKE_CURRENT_BINARY_DIR表示当前正在构建的目录)。接下来,我们需要让CMake知道,mysqrt.cxx依赖生成文件Table.h,可以把Table.h添加至MathFunctions库的源文件列表中来达到这一目的。当这个工程构建时,会先构建MakeTable可执行文件。接着会运行它动态产生Table.h。最后让mysqrt.cxx包含Table.h,并编译它来生成MathFunctions库。完成上面以及前面几节的内容后,顶层CMakeLists.txt文件内容如下:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
include(CTest)

# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)

# does this system provide the log and exp functions?
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)

check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

# should we use our own math functions
option(USE_MYMATH 
  "Use tutorial provided math implementation" ON)

# 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 MathFunctions library?
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial  ${EXTRA_LIBS})

# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"        
         DESTINATION include)

# does the application run
add_test (TutorialRuns Tutorial 25)

# does the usage message work?
add_test (TutorialUsage Tutorial)
set_tests_properties (TutorialUsage
  PROPERTIES 
  PASS_REGULAR_EXPRESSION "Usage:.*number"
  )


#define a macro to simplify adding tests
macro (do_test arg result)
  add_test (TutorialComp${arg} Tutorial ${arg})
  set_tests_properties (TutorialComp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result}
    )
endmacro (do_test)

# do a bunch of result based tests
do_test (4 "4 is 2")
do_test (9 "9 is 3")
do_test (5 "5 is 2.236")
do_test (7 "7 is 2.645")
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")
do_test (0.0001 "0.0001 is 0.01")

TutorialConfig.h

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

// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

MathFunctions目录下的CMakeLists.txt

# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
# add the command to generate the source code
add_custom_command (
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  DEPENDS MakeTable
  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  )
# add the binary tree directory to the search path 
# for include files
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )

# add the main library
add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h)

install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CMake是一个用于管理项目构建过程的开源工具。它通过配置文件的方式来定义源代码、编译器选项、库依赖和构建规则等信息,并根据这些信息生成针对不同平台和编译器的构建脚本。 CMake的教程可以帮助我们更好地理解和使用这个工具。在教程中,我们可以学习到CMake的基本概念、语法和用法,以及如何编写CMakeLists.txt文件来定义项目的构建过程。 教程一般会从简单的示例开始,逐步介绍CMake的各个方面。首先,我们可以学习如何定义基本的项目信息,如项目名称、版本号和语言要求。然后,我们可以了解如何添加源代码文件、头文件目录和编译选项。接着,我们可以学习如何定义库依赖和链接选项,以及如何生成可执行文件或库文件。 教程还可以教我们如何使用CMake的命令来进行条件编译、循环遍历和文件查找等操作。此外,教程还会介绍如何使用CMake的变量、宏和函数来简化构建过程和提高代码的可读性。 通过学习CMake的教程,我们可以更加高效地管理和构建项目,提高代码的可维护性和可移植性。无论是小型项目还是大型项目,CMake都可以帮助我们更好地组织代码、管理依赖和发布软件。 总之,CMake的教程是学习和使用CMake的重要资源,它可以帮助我们掌握CMake的基本概念和用法,提高项目的构建效率和质量。希望通过学习CMake的教程,我们能够更加熟练地使用这个工具,为我们的项目开发和维护带来便利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值