静态库中的预处理器宏

发布于 2024-12-10 07:36:46 字数 162 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-12-17 07:36:46

预处理器 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.

清音悠歌 2024-12-17 07:36:46

这本身并不是一个答案,而是我在解决同一问题时发现的一些有趣的事情。

我有一个静态库 (MyLib),其中包含用于日志记录的标头 (Log.h)。我有一个使用 MyLib 的应用程序项目 (MyApp)。 Log.h 与此有一些相似之处:

#ifdef LOG_LEVEL_DEBUG
#   define LogDebug(...) NSLog(__VA_ARGS__)
#else 
#   define LogDebug(...)
#endif

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 uses MyLib. Log.h has some resemblance of this:

#ifdef LOG_LEVEL_DEBUG
#   define LogDebug(...) NSLog(__VA_ARGS__)
#else 
#   define LogDebug(...)
#endif

In MyApp build settings, I can use the preprocessor macro LOG_LEVEL_DEBUG to successfully turn off and on logging. This works when I use LogDebug() in source files found in MyApp. However, the MyLib source files that use LogDebug() are not affected by the MyApp build settings. I have to use the MyLib build settings to affect LogDebug() within the MyLib source files.

I am pretty sure I know what is happening but I'd be open to correction. Below is the scenario where MyApp defines LOG_LEVEL_DEBUG in build settings (enabling debugging) and MyLib does not define it (disabling it).

When MyApp builds, it first compiles MyLib where all of the LogDebug() are replaced within the MyLib source files as no-op (since LOG_LEVEL_DEBUG was not defined). After MyLib is compiled, MyApp is compiled and all of the LogDebug() methods within MyApp source are replaced with NSLog() statements because LOG_LEVEL_DEBUG was defined in the build settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文