问题
Can you use macros defined in static libraries?
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.
回答1:
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.
Update:
So to implement a debug option, I see two options:
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.
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.
来源:https://stackoverflow.com/questions/6758187/enabling-ifdef-macro-used-in-the-static-library