静态库中的预处理器宏
在 Objective-C 项目中,我使用静态库,该静态库的编译取决于要设置的一些预处理器宏。
当我根据库在项目中设置这些宏时,库看不到它们。但是当我将它们设置在库项目中时它确实有效。
由于我想在其他项目中重用该库,因此我需要根据该库分别为每个项目设置预处理器宏。有解决办法吗?
In a Objective-C project I am using a static library, compilation of this static library depends on some preprocessor macros to be set.
When I set these macros in the project depending on the library the library does not see them. But when I set them in the library project it does work.
Since I want to reuse this library for other projects, I require to set the preprocessor macros for each project depending on the library separately. Is there a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
预处理器 maros 仅在编译时有意义,因此您构建的任何库都将特定于构建库时这些预处理器宏的值。您将需要许多不同版本的库,使用预处理器宏的不同可能值构建,或者您可以切换到使用不同的方法来控制将在运行时工作的库代码的行为,例如设置通过库 API 获取一些适当的参数。
Preprocessor maros only have any meaning at compile-time, so any library you build will be specific to the values of these preprocessor macros at the time you built the library. You will either need lots of different versions of your library, built with the different possible values of your preprocessor macros, or you could switch to using a different method to control the behaviour of your library code which will work at run-time, e.g. setting some appropriate parameters through the library API.
这本身并不是一个答案,而是我在解决同一问题时发现的一些有趣的事情。
我有一个静态库 (
MyLib
),其中包含用于日志记录的标头 (Log.h
)。我有一个使用MyLib
的应用程序项目 (MyApp
)。Log.h
与此有一些相似之处:在
MyApp
构建设置中,我可以使用预处理器宏LOG_LEVEL_DEBUG
成功关闭和打开日志记录。当我在MyApp
中找到的源文件中使用LogDebug()
时,此方法有效。但是,使用LogDebug()
的MyLib
源文件不受MyApp
构建设置的影响。我必须使用MyLib
构建设置来影响MyLib
源文件中的LogDebug()
。我很确定我知道发生了什么,但我愿意接受纠正。以下场景是
MyApp
在构建设置中定义了LOG_LEVEL_DEBUG
(启用调试),而MyLib
未定义它(禁用它)。当
MyApp
构建时,它首先编译MyLib
,其中所有LogDebug()
都在MyLib
源文件中替换作为no-op
(因为LOG_LEVEL_DEBUG
未定义)。编译MyLib
后,将编译MyApp
,并将MyApp
源中的所有LogDebug()
方法替换为NSLog()
语句,因为LOG_LEVEL_DEBUG
是在构建设置中定义的。This is not an answer per se, but something interesting I discovered while struggling with this same issue.
I have a static library (
MyLib
) that contains a header for logging (Log.h
). I have an application project (MyApp
) that usesMyLib
.Log.h
has some resemblance of this:In
MyApp
build settings, I can use the preprocessor macroLOG_LEVEL_DEBUG
to successfully turn off and on logging. This works when I useLogDebug()
in source files found inMyApp
. However, theMyLib
source files that useLogDebug()
are not affected by theMyApp
build settings. I have to use theMyLib
build settings to affectLogDebug()
within theMyLib
source files.I am pretty sure I know what is happening but I'd be open to correction. Below is the scenario where
MyApp
definesLOG_LEVEL_DEBUG
in build settings (enabling debugging) andMyLib
does not define it (disabling it).When
MyApp
builds, it first compilesMyLib
where all of theLogDebug()
are replaced within theMyLib
source files asno-op
(sinceLOG_LEVEL_DEBUG
was not defined). AfterMyLib
is compiled,MyApp
is compiled and all of theLogDebug()
methods withinMyApp
source are replaced withNSLog()
statements becauseLOG_LEVEL_DEBUG
was defined in the build settings.