简体   繁体   English

c ++代码中的#if语句

[英]#if statements in c++ code

I'm using a 3rd party library, and I'm not compiling their sources, only including the headers from /usr/include . 我正在使用第三方库,我没有编译他们的来源,只包括来自/usr/include的头文件。

I know that the source code contains a block like the following: 我知道源代码包含如下所示的块:

#if VAL1 && VAL2
    do something
#else
   do something I dont want to do
#endif

I know that VAL2 is set to 1 , but VAL1 is set to 0, in a different file included somewhere inside. 我知道VAL2设置为1 ,但VAL1设置为0,位于内部某处的不同文件中。

What I'm trying to do is define VAL1 myself, by copying the content of the header which sets VAL1 to 0 , and setting the value myself. 我想要做的是自己定义VAL1 ,通过复制将VAL1设置为0的标题的内容,并自己设置值。

Is this going to make it so that when the code of the library runs, it will run into the first block? 这样做是为了当库的代码运行时,它会运行到第一个块吗? or is it totally static in the compilation time? 或者在编译时它是完全静态的?

Preprocessor macros are compilation-time things. 预处理器宏是编译时的东西。 You usually use them to make sure compiler know how to behave in certain situation. 您通常使用它们来确保编译器知道在某些情况下如何表现。 For example when you're using different libraries for different platforms. 例如,当您为不同的平台使用不同的库时。

More info here 更多信息在这里

Is this going to make it so that when the code of the library runs, it will run into the first block? 这样做是为了当库的代码运行时,它会运行到第一个块吗? or is it totally static in the compilation time? 或者在编译时它是完全静态的?

the answer is : it is totally static in the compilation time. 答案是:它在编译时完全是静态的。

If you would like to compile both versions of your code then depending on context where this if/def macro is used you could: 如果您想编译代码的两个版本,那么根据使用此if/def宏的上下文,您可以:

if (VAL1 && VAL2)
{    do something }
else
{   do something I dont want to do }

then assuming VAL1 and VAL2 resolves to 1, the code do something will compile and execute, but the code do something I dont want to do will only compile - but compiler will optimize it out by not including it in resulting executable. 然后假设VAL1VAL2解析为1,代码do something将编译并执行,但代码do something I dont want to do只会编译 - 但编译器会通过不将其包含在生成的可执行文件中来优化它。

short answer: no 简短的回答: 没有

Those are preprocessor directives which are (as the name indicates) preprocessed before compilation. 这些是预处理程序指令 ,在编译之前(如名称所示)预处理。 Once your program is compiled; 一旦你的程序被编译; you cannot change that behavior anymore. 你不能再改变这种行为了。

No, this will only affect the header as it is parsed during your next compilation run. 不,这只会影响在下一次编译运行期间解析的标题。 It'll have no effect on the compiled part of the library that you're already linking against. 它对您已链接的库的已编译部分没有任何影响。

This means it's probably a bad idea. 这意味着它可能是一个坏主意。 In general you want to keep compiler flags consistent, and that includes macros. 通常,您希望保持编译器标志一致,并且包括宏。 We cannot tell whether it'll make a difference here but, unless you're sure, I would stick with the same macro definitions. 我们无法判断它是否会在这里产生影响但是,除非你确定,否则我会坚持使用相同的宏定义。

If that means you have to recompile the library itself then that's what you'll have to do. 如果这意味着您必须重新编译库本身,那么您就必须这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM