Skip to content

Latest commit

 

History

History

01-configure-files

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

01-configure-files

Introduction

During the call to cmake it is possible to create files that use variables from the CMakeLists.txt and cmake cache. During CMake generation the file is copied to a new location and any cmake variables are replaced.

The files in this tutorial are below:

01-configure-files$ tree
.
├── CMakeLists.txt
├── main.cpp
├── path.hpp.in
├── version.hpp.in

Concepts

Configure Files

To do variable substitution in a file you can use the configure_file() function in CMake. This core arguments for this function are source file and destination file.

configure_file(version.hpp.in ${PROJECT_BINARY_DIR}/version.hpp)

configure_file(path.hpp.in ${PROJECT_BINARY_DIR}/path.hpp @ONLY)

The first example above, allows the variable to be defined like a CMake variables using the ${} syntax or an @@ in the version.hpp.in file. After generation a new file version.hpp will be available in the PROJECT_BINARY_DIR.

namespace my
{
	// version variable that will be substituted by cmake
	// This shows an example using the $ variable type
	const char* project_version = "${${PROJECT_NAME}_VERSION}";
}

The second example, only allows variables to be defined using the @@ syntax in the path.hpp.in file. After generation a new file path.hpp will be available in the PROJECT_BINARY_DIR.

namespace my
{
	// version variable that will be substituted by cmake
	// This shows an example using the @ variable type
	const char* project_path = "@CMAKE_SOURCE_DIR@";
}

Building the Example

$ mkdir build

$ cd build/

$ cmake ..
-- The C compiler identification is ...
-- The CXX compiler identification is ...
-- Check for working C compiler: ...
-- Check for working C compiler: ... -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: ...
-- Check for working CXX compiler: ... -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: project-absolute-path/01-configure-files/build

$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  path.hpp  ver.hpp

$ cat path.hpp
#pragma once

namespace my
{
	// version variable that will be substituted by cmake
	// This shows an example using the @ variable type
	const char* project_path = "project-absolute-path/01-configure-files";
}

$ cat version.hpp
#pragma once

namespace my
{
	// version variable that will be substituted by cmake
	// This shows an example using the $ variable type
	const char* project_version = "1.2.3";
}