Add_definitions vs. configure_file

I need to conditionally compile several parts of the code, depending on the presence of some libraries in the system or not. Their presence is determined during the CMake configuration phase, and I plan to tell the compiler the results using preprocessor definitions (e.g. #ifdef (LIB_DEFINED) ... #endif).

I know of two ways to achieve this in CMake:

  • Place the template file with these preprocessor definitions, pass it to CMakeLists for configure_file (), and finally # include the generated configuration file in each source file
  • Use add_definitions (-DLIB_DEFINED) directly in CMakeLists directly.

The first approach seems more complicated to me, so are there any advantages to using it instead of the second (for example, avoiding some portability issues)?

+2
source share
2 answers

Depending on the number of libraries used, the compiler call becomes large if you follow the second approach. Therefore, I would say that for small projects only 2-3 of these additional libraries follow approach 2, but if it looks more like 10 or so, it is better to follow approach 1 so that the compilation output remains readable.

+1
source

1 , , , / , . , . , add_definitions - ( , ).

+1

Source: https://habr.com/ru/post/1648068/


All Articles