| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- # 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
|