How to declare a function like macro using CMake

Viewed 717

I can't find how to declare function like macros in CMake.

I need a macro function like:

#define MYFUNC(foo) QString( foo + "_suffix" )

to be defined by my CMakeLists.txt file. I tried:

add_definitions("-DMYFUNC(foo)=QString\(foo+\"_suffix\"\)")

and

add_definitions("-DMYFUNC\(foo\)=QString\(foo+\"_suffix\"\)")

but none works, compiler (VS2015) always reports MYFUNC is undefined...

2 Answers

From the Visual Studio documentation on /D:

The /D option doesn't support function-like macro definitions. To insert definitions that can't be defined on the command line, consider the /FI (Name forced include file) compiler option.

For completeness, GCC does support defining function macros from the commandline:

If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you should quote the option. With sh and csh, -D'name(args…)=definition' works.

CMake documentation for COMPILE_DEFINITIONS states explicitly that function-like macros are not supported:

The COMPILE_DEFINITIONS property may be set to a semicolon-separated list of preprocessor definitions using the syntax VAR or VAR=value. Function-style definitions are not supported. CMake will automatically escape the value correctly for the native build system (note that CMake language syntax may require escapes to specify some values).

Consider using -include instead to force inclusion of a header file where your macro is defined.

Related