definitions与configure

definitions与configure

本文介绍了add_definitions与configure_file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有条件地编译代码的几个部分,这取决于系统上是否存在一些库。它们的存在是在CMake配置阶段确定的,我计划使用预处理器定义(如#ifdef(LIB_DEFINED)... #endif)告诉编译器结果。

I need to conditionally compile several parts of code, depending on whether there are some libraries present on 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 (like #ifdef(LIB_DEFINED) ... #endif).

我知道如何在CMake中实现两种可能性:

I know about two possibilities how to achieve that in CMake:


  1. 这些预处理器定义模板文件,通过它在CMakeLists到configure_file(),最后在#包括每一个源文件

  2. 产生的配置文件中CMakeLists直接使用add_definitions(-DLIB_DEFINED)。

第一种方法似乎更复杂,对我来说,那么,有没有服用它,而不是第二个的任何好处(例如避免某些可移植性问题)?

The first approach seems more complicated to me, so are there any advantages of taking it instead of the second one (e.g. avoiding some portability issues)?

推荐答案

根据你使用的库的数量,如果遵循第二种方法,编译器的调用会变得很大。所以,我要说对于较小的项目只有那些可选库2-3遵循的方法2,但如果它更像是10个左右,较好的跟踪方法1,这样编译输出保持可读性。

Depending on the amount of libraries you use, the call of the compiler becomes large if following the second approach. So I would say for smaller projects with only 2-3 of those optional libraries follow approach 2 but if it's more like 10 or so, better follow approach 1 so that the compilation output stays readable.

这篇关于add_definitions与configure_file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:20