启用静态库中使用的ifdef宏 [英] Enabling ifdef macro used in the static library

查看:495
本文介绍了启用静态库中使用的ifdef宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用静态库中定义的宏吗?

Can you use macros defined in static libraries?

我有一个名为TWDEBUG的调试宏,可在为共享创建的静态库中使用. 如果我将静态库导入到我的新项目中并使用它,则编译器似乎无法识别它.我确实将预处理器宏设置为TWDEBUGOther C flags,将Other C++ flags设置为-TWDEBUG,但是当我运行代码时,ifdef宏不会被执行.

I have my own debug macro called TWDEBUG that I use in a static library I create for sharing. If I import the static library to my new project and use it, the compiler does not seem to recognize it. I did set up preprocessor macros to TWDEBUG and Other C flags and Other C++ flags to -TWDEBUG, but when I ran the code the ifdef macro doesn't get executed.

推荐答案

在编译时评估宏.因此,当您构建静态库时,它们的值将被冻结.对于调试语句,这通常意味着将它们省略,而不是已构建库的一部分.

Macros are evaluated at compile-time. So their values are frozen when you build the static library. For debugging statement, this usually means that they are omitted and not part of the built library.

如果以后将静态库添加到项目中,则可以更改宏的值.但是由于它不再编译,因此对静态库没有任何影响.调试语句丢失.

If you later add the static library to a project, you can change the value of the macros. But it won't have any effect on the static library since it is not compiled anymore. The debugging statements are missing.

更新:

因此,要实现调试选项,我看到两个选项:

So to implement a debug option, I see two options:

  1. 您可以使用全局变量和适当的ifs代替宏和ifdefs来打开和关闭调试.其他开发人员可以使用API​​(全局函数)来设置调试级别,以便隐藏全局变量.

  1. Instead of macros and ifdefs, you use a global variable and proper ifs to turn debugging on and off. Other developers can use an API (global function) to set the debugging level so you can hide the global variable.

从同一源代码创建两个静态库,一个静态库已启用调试用于开发目的,另一个静态库已禁用调试以用于生产用途.仅当XCode可以在两个库之间自动切换时,此选项才可行.目前,我不知道您将如何配置它.

Create two static library from the same source code, one with debugging enabled for development purposes and the other with debugging disabled for production use. This option is probably only viable if XCode can automatically switch between the two libraries. And at the moment, I don't know how you would configure that.

这篇关于启用静态库中使用的ifdef宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆