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...
If you are using CMake 3. X your first choice for adding a preprocessor macro should be target_compile_definitions. The reason you should prefer this approach over any other approach is because it granularity is target based. IE the macro will only be added to your exe/library.
You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake .
CMake can generate a native build environment that will compile source code, create libraries, generate wrappers and build executables in arbitrary combinations. CMake supports in-place and out-of-place builds, and can therefore support multiple builds from a single source tree.
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 syntaxVAR
orVAR=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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With