简体   繁体   English

是否可以根据构建配置更改 Eclipse CDT 中的代码?

[英]Is it possible to alter code in Eclipse CDT based on build configuration?

I have a C++ project that I build for both CentOS 7 and Raspberry Pi.我有一个为 CentOS 7 和 Raspberry Pi 构建的 C++ 项目。 I have an Eclipse build configuration for compiling for CentOS and another build configuration for cross-compiling for Raspberry Pi.我有一个用于 CentOS 编译的 Eclipse 构建配置和另一个用于 Raspberry Pi 交叉编译的构建配置。 These both work great.这些都很好用。

What I want is to automatically alter a line or two of code based on which build configuration I'm using.我想要的是根据我使用的构建配置自动更改一两行代码。 I noticed that Eclipse has a build variable called ConfigName, but I don't think that it is accessible in code.我注意到 Eclipse 有一个名为 ConfigName 的构建变量,但我认为它不能在代码中访问。 I also tried manually creating a macro for each build configuration in the project settings.我还尝试为项目设置中的每个构建配置手动创建一个宏。 However, when I try to read the macro in code I get the error that it was not declared in this scope.但是,当我尝试在代码中读取宏时,我收到错误,指出它未在此范围内声明。 I really don't have much experience with macros other than the basics such as determining what OS you're compiling on.除了基础知识(例如确定您正在编译的操作系统)之外,我对宏没有太多经验。

This seems like it should be a common task that isn't difficult to figure out.这似乎应该是一个不难弄清楚的常见任务。 Maybe I'm missing something obvious but I can't seem to find any documentation on what I'm trying to do.也许我遗漏了一些明显的东西,但我似乎找不到任何关于我正在尝试做的事情的文档。 Is this even possible?这甚至可能吗? Am I looking in the wrong direction?我是不是看错了方向?

After a lot of researching documentation and trial and error, I was able to find a solution.经过大量研究文档和反复试验,我找到了解决方案。

In the project properties, go to C/C++ General --> Paths and Symbols --> Symbols --> GNU C++.在项目属性中,转至 C/C++ General --> Paths and Symbols --> Symbols --> GNU C++。 Add a symbol for each build configuration.为每个构建配置添加一个符号。 I added the following:我添加了以下内容:

CONFIG_DEBUG
CONFIG_PI_DEBUG
CONFIG_RELEASE
CONFIG_PI_RELEASE

NOTE: Just to clarify, don't enter the above symbols together in the same place.注意:只是为了澄清,不要在同一个地方一起输入上述符号。 Enter each one as a single symbol in it's respective build configuration.在其各自的构建配置中将每个作为单个符号输入。

Then, the code can be altered by using the preprocessor directives.然后,可以使用预处理器指令更改代码。 I basically used the following:我基本上使用了以下内容:

#if defined(CONFIG_PI_RELEASE)
  // Raspberry Pi release code
#elif defined(CONFIG_RELEASE)
  // CentOS release code
#elif defined(CONFIG_PI_DEBUG)
  // Raspberry Pi debug code
#else
  // CentOS debug code
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM