2

I want to conditionally define a variable that I then can use within C/C++ sources to conditionally compile some code, like this:

#ifdef MY_MESON_VARIABLE
    // some code
#endif

How do I go about actually setting MY_MESON_VARIABLE from Meson build system code such that it becomes accessible to the C preprocessor? I already know how do flow control in Meson, so I just need to know how to set a preprocessor variable.

4
  • Passing -DMU_MESON_VARIABLE (or /DMU_MESON_VARIABLE for visual) to the command line, your build system might have dedicated api to add those definitions. Commented Nov 6, 2024 at 11:05
  • 1
    mesonbuild.com/Adding-arguments.html Commented Nov 6, 2024 at 11:08
  • @Jarod42 thanks, I guess it was hiding from me in plain sight. Commented Nov 6, 2024 at 11:13
  • 1
    How to add preprocessor defines to a target? Commented Nov 6, 2024 at 11:14

1 Answer 1

1

Meson allows you to define preprocessor variables via compilation arguments. To do this, use add_project_arguments or add_global_arguments, add to Meson.build:

add_project_arguments('-DMY_MESON_VARIABLE', language: 'c')

The -D before the variable name tells the compiler to define the specified variable.

The add_project_arguments function adds compilation arguments that will be applied to the entire project.

This will make MY_MESON_VARIABLE available for #ifdef in C/C++ sources.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.