7.MariaDB笔记——cmake使用介绍二
在上篇的基础上,继续学习实验。
提供可执行文件和项目的版本号。
在CMakeLists文件中加入
cmake_minimum_required(VERSION 2.6)
project(Tutorial)
# The versionnumber.
set (Tutorial_VERSION_MAJOR1)
set(Tutorial_VERSION_MINOR 0)
# configure aheader file to pass some of the CMake settings
# to thesource code
configure_file(
"${PROJECT_SOURCE_DIR}/TutorialConfig.h"
"${PROJECT_BINARY_DIR}/TutorialConfig.h"
)
# add the binarytree to the search path for include files
# so that wewill find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add theexecutable
add_executable(Tutorialtutorial.cxx)
我们必须增加一个目录路径来搜索include的文件,创建一个TutorialConfig.h文件如下:
// theconfigured options and settings for Tutorial
#defineTutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#defineTutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
当CMAKE配置@Tutorial_VERSION_MAJOR@和@Tutorial_VERSION_MINOR@的时候,会在CMakelists文件中找(现在我们定义的是1和 0)。
接着修改源码文件tutorial.cxx来包含configured头文件来使用版本号。
// A simpleprogram that computes the square root of a number
#include
#include
#include
#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: %snumber\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 .如下:
>cmake .
-- Configuring done
-- Generating done
-- Build files have been written to:F:/VS2010_ZHIZUO/cmake_zhizuo
然后执行cmake –build .
测试如下:
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe 4
The square root of 4 is 2
F:\VS2010_ZHIZUO\cmake_zhizuo\Debug>Tutorial.exe
Tutorial.exe Version 1.0
Usage: Tutorial.exe number
看,输出了版本信息。