blob: 827d7754d5e8d57725059c3fd760b4d01de38023 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# Adding a Version Number and Configured Header File #
The first feature we will add is to provide our executable and project with a
version number. While we could do this exclusively in the source code, using
CMakeLists provides more flexibility.
To add a version number we modify the CMakeLists file as follows:
cmake_minimum_required(VERSION 3.3)
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"
)
# add the executable
add_executable(Tutorial tutorial.cxx)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
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
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 <cmath>
#include <iostream>
#include <string>
#include <sstream>
#include "TutorialConfig.h"
int main (int argc, char *argv[])
{
if (argc < 2) {
std::cout << argv[0] << " Version "
<< Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR
<< std::endl;
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
std::cout << "The square root of "
<< inputValue << " is " << outputValue << std::endl;
return 0;
}
# Adding C++11 support #
Let's add some C++11 features to our project. We will need to explicitly state
in the CMake code that it should use the correct flags. The easiest way to
enable C++11 support for CMake is by using the CMAKE_CXX_STANDARD
and CMAKE_CXX_STANDARD_REQUIRED variables.
First, replace `atof` with `std::stod` in tutorial.cxx.
Then, add the CMAKE_CXX_STANDARD and CMAKE_CXX_STANDARD_REQUIRED variables to
the CMakeLists file. The STANADARD value should be set to 11, and REQUIRED
should be set to True.
# Build and Test #
Run cmake or cmake-gui to configure the project and then build it with your
chosen build tool
cd to the directory where Tutorial was built (likely the make directory or
a Debug or Release build configuration subdirectory) and run these commands:
Tutorial 4294967296
Tutorial 10
Tutorial
|