从 .h 和 .cpp 进行条件编译

发布于 2024-11-03 14:54:00 字数 603 浏览 2 评论 0原文

我正在尝试制作一个库文件。 .cpp 文件有一些条件编译行。代码可在以下位置找到:

HMC58X3.h http://sprunge.us/hEYW

HMC58X3.cpp http://sprunge.us/faRN

HMC58X3_raw.pde http://sprunge.us/BFVj

基本上,在 Arduino 草图文件中 HMC58X3_raw.pde 我定义 ISHMC5843 并在 HMC58X3.cppHMC58X3.h 中,我确实有不同的代码需要编译,具体取决于该标志是否已被设置已启用。

条件编译似乎适用于 HMC58X3.h,但不适用于 HMC58X3.cpp。它总是看起来好像 ISHMC5843 尚未定义。如何让它发挥作用?

I'm trying to make a library file. The .cpp file has some conditional compiled lines. The code can be found at:

HMC58X3.h http://sprunge.us/hEYW

HMC58X3.cpp http://sprunge.us/faRN

HMC58X3_raw.pde http://sprunge.us/BFVj

Basically, in the Arduino sketch file HMC58X3_raw.pde I define ISHMC5843 and in both HMC58X3.cpp and HMC58X3.h I do have different code to be compiled depending if that flag has been enabled.

The conditional compilation seems to work for HMC58X3.h while it doesn't for HMC58X3.cpp. It always looks as if ISHMC5843 hasn't been defined. How can it be made to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

大海や 2024-11-10 14:54:00

#define 与全局变量不同。它是一个预处理器宏,仅适用于该编译单元的剩余文本。有几种方法可以完成您想要的操作:

  1. 使用 #define ISHMC5843 创建一个 config.h 并确保在任何地方都包含它(并且在引用它的任何其他包含之前) )。
  2. 在您的 Makefile 中(在 Arduino 中您可能无法访问),确保 -DISHMC5843 出现在每个编译行中,通常是将其包含在 CFLAGS 中。 (如何确保 CFLAGS 是编译规则的一部分的详细信息远远超出了这个问题的范围)。

A #define is not like a global variable. It's a pre-processor macro that only applies to the remaining text of that compile unit. There are a couple ways to do what you want:

  1. Create a config.h with #define ISHMC5843 and be sure to include it everywhere (and before any other includes that reference it).
  2. In your Makefile (probably inaccessible to you in Arduino) ensure -DISHMC5843 appears on every compile line, typically by including it in CFLAGS. (The details of how to ensure CFLAGS is part of your compile rule is way beyond the scope of this question).
最近可好 2024-11-10 14:54:00

当您编译 HMC58X3.cpp 时,编译器尚未看到 HMC58X3_raw.pde 中的宏定义。 IMO,您最好使用全局布尔常量变量来实现您在这里尝试做的事情。

When you compile HMC58X3.cpp the compiler hasn't seen the macro definitions in HMC58X3_raw.pde. IMO, you're better of using a global boolean constant variable to achieve what you're trying to do here.

情话难免假 2024-11-10 14:54:00

我看不到 ISHMC58431 在 HMC58X3.h 或 HMC58X3.cpp 中是如何定义的。

预处理文件时,定义必须对预处理器可见。这通常是通过#include'ing一个公共文件来完成的,该文件在需要宏可见性的所有文件中包含#define,或者通过在编译器命令上定义宏来完成行,例如 -DISHMC58431 (取决于编译器)。

这当然要求 .pde 文件也由预处理器处理,因为它有 #include 语句,所以我假设它是这样的。

I cannot see how ISHMC58431 is defined in either HMC58X3.h or HMC58X3.cpp.

The definition has to be visible to the preprocessor when the file is pre-processed. This is normally done by #include'ing a common file that includes the #define in all files that need visibility of the macro, or by defining the macro on the compiler command line, such as -DISHMC58431 for example (compiler dependent).

This would of course require that the .pde file is also processed by the pre-processor, which since it has #include statements, I assume that it is.

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