Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backward compatible `add_compile_definitions`

Tags:

cmake

I am working on fixing some CMake files that have been made with a version newer than 3.10, but should have been coded to be compatible with 3.10.

There is a particular line:

add_compile_definitions(SOME_BOOL_VARIABLE)

that I can not figure out how to change to make it work with version 3.10. The add_definitions(SOME_BOOL_VARIABLE) command was the obvious thing that I tried, but produces errors with "no such file of directory".

like image 937
Ander Biguri Avatar asked Oct 12 '25 07:10

Ander Biguri


1 Answers

From the CMake documentation, the functionality of add_definitions() and add_compile_definitions() is essentially the identical:

Adds preprocessor definitions to the compiler command line for targets in the current directory and below (whether added before or after this command is invoked).

The difference is in the syntax accepted for the command arguments. The older add_definitions() command requires the -D flag be prepended to each definition.

add_definitions(-DSOME_BOOL_VARIABLE) 

The newer add_compile_definitions() command (available in CMake 3.12 and above) is cleaner, and does not require the -D flag prefix.


If you are refactoring your code, modern CMake encourages a target-centric approach. Whenever possible, you should prefer the target_compile_definitions() command to add preprocessor definitions to only those targets that require them.

target_compile_definitions(MyLibraryTarget PRIVATE SOME_BOOL_VARIABLE)
like image 114
Kevin Avatar answered Oct 14 '25 00:10

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!