How to Read a Cmake Variable in C++ Source Code

How to read a CMake Variable in C++ source code

The easiest way to do this, is to pass the LIBINTERFACE_VERSION as a definition with add_definition:

add_definitions( -DVERSION_LIBINTERFACE=${LIBINTERFACE_VERSION} )

However, you can also create a "header-file template" and use configure_file. This way, CMake will replace your @LIBINTERFACE_VERSION@. This is also a little more extensible because you can easily add extra defines or variables here...

E.g. create a file "version_config.h.in", looking like this:

#ifndef VERSION_CONFIG_H
#define VERSION_CONFIG_H

// define your version_libinterface
#define VERSION_LIBINTERFACE @LIBINTERFACE_VERSION@

// alternatively you could add your global method getLibInterfaceVersion here
unsigned int getLibInterfaceVersion()
{
return @LIBINTERFACE_VERSION@;
}

#endif // VERSION_CONFIG_H

Then add a configure_file line to your cmakelists.txt:

configure_file( version_config.h.in ${CMAKE_BINARY_DIR}/generated/version_config.h )
include_directories( ${CMAKE_BINARY_DIR}/generated/ ) # Make sure it can be included...

And of course, make sure the correct version_config.h is included in your source-files.

Use variable from CMAKE in C++

add_definitions ( -DVARNAME=... )

is the correct way of using add_definitions.

To check for a constant then, use

#ifdef VARNAME
...
#endif

How to check CMake variable in C++

CMake variables cannot be accessed in C++. However, CMake can set compiler options, and specifically, macro definitions. Such macros can be accessed by the pre processor. Use the target_compile_definitions command.

Passing a CMake variable to C++ source code

You can use the configure_file(filename output_file) command. It replaces something like ${VARIABLE} or @VARIABLE@ in file filename with the actual value of cmake variable VARIABLE and writes the result to output_file.

So, you can write something like

// in C++ code
std::string my_var = "@MY_VAR@";

# in CMakeLists.txt
configure_file(filename.h.in filename.h)

Also, I think it is a good idea for the output file to be generated somewhere inside build directory (and its' containing directory added to include paths).

configure_file in cmake documentation

Referring to a CMAKE variable from code

The problem is with your use of add_definitions. You're effectively passing the value of ${CMAKE_CURRENT_SOURCE_DIR}/ptx as an additinal argument on the compiler's command line, which the compiler probably interprets as a source file it should compile. Check the full command line invoked to be sure.

You probably intended this:

add_definitions(-DNV12_2_ARGB_PTX_DIR="${CMAKE_CURRENT_SOURCE_DIR}/ptx")

Note that you may have to play around with escaping the quotation marks to get them all the way down to C++. Alternatively, you could use configure_file().

Use value from C/C++ macro in CMake

I'd go with file(READ ...) to read the header followed by string(REGEX ...) to extract desired define.

Example code:

file(READ "foo.h" header)
string(REGEX MATCH "#define FOO_MAJOR_VERSION [0-9]+" macrodef "${header}")
string(REGEX MATCH "[0-9]+" FooMajorVersion "${macrodef}")

pass variable value to make using cmake

Here is a small demonstration,

// demo.cpp
#include <iostream>
using namespace std;

int main()
{
#ifdef USE_FILES_FOR_INPUT
cout << "Using files.." << endl;
#else
cout << "Not using files.." << endl;
#endif


return 0;
}

The CMakeLists.txt,

cmake_minimum_required(VERSION 3.0)

add_executable(demo demo.cpp)

if (USE_FILES_FOR_INPUT)
add_definitions(-DUSE_FILES_FOR_INPUT)
endif()

And a sample output,

baris$ cmake . && make && ./demo
<...>
Not using files..

baris$ cmake -DUSE_FILES_FOR_INPUT=ON . && make && ./demo
<...>
Using files..


Related Topics



Leave a reply



Submit