我可以设置编译时标志来包含标头吗?
我有一个配置类,我想将其用于各种构建。类本身在构建之间发生变化,但类名保持不变,头文件名也保持不变。
此类的单独版本保存在单独的子文件夹中。
前任。
main/config.h
main/config.cpp
secondary/config.h
secondary/config.cpp
有没有一种好方法可以通过编译时标志或命令行选项让构建确定要使用哪个标头/cpp?我已经有了相当多的配置,并且预计将来会有更多。我想避免一长串 #ifdef/#elif/#elif/etc..
编辑:我想避免单独构建,并且希望避免在整个代码中使用 #defines。如果我之前没有说清楚的话,我很抱歉! >_<
I have a configuration class that I would like to use for a variety of builds. The class itself changes between builds, but the class name remains the same, as does the header file name.
The separate versions of this class are held in separate subfolders.
ex.
main/config.h
main/config.cpp
secondary/config.h
secondary/config.cpp
Is there a good way to, through a compile-time flag or command line option, have the build determine which header/cpp to use? I have quite a few configurations already, and expect to have many more in the future. I would like to avoid a long list of #ifdef/#elif/#elif/etc..
edit: I would like to avoid having separate builds, and would like to avoid using #defines throughout the code. I'm sorry if I didn't make that clear before! >_<
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据您使用的构建系统,您将创建一个指向主路径或辅助路径的变量。然后使用此变量附加到
INCLUDE
路径,以便所有源在需要访问配置时只需#include "config.h"
即可。在您的 Makefile(或同等文件)中,您需要将$CONFIGPATH/config.cpp
添加到要构建的源代码中。MSBuild
更新源文件路径:
和包含路径:
然后调用
msbuild build.xml /p:ConfigToUse=PathToConfig
Depending on what build system you are using you would create a variable that points to the main or secondary path. This variable is then used to append to the
INCLUDE
path so all of your sources can just#include "config.h"
when they need access to config. In your Makefile (or equivalent) you will need to add the$CONFIGPATH/config.cpp
to your sources to build.MSBuild
Update source file paths:
And the include path:
Then call
msbuild build.xml /p:ConfigToUse=PathToConfig
#include 指令并不真正关心内容。
您可以轻松地拥有一个在项目中使用的存根类:
stub.cpp
当然,如果需要,您可以对标头执行相同的操作。
The #include directive doesn't really care about content.
You can just as easily have a stub class that you use in your project:
stub.cpp
And, of course, you can do the same thing with headers if necessary.
您可以像这样放入 header/cpp 防护中:
You can put into your header/cpp guards like this:
您可以使用编译时#define,例如
You can use compile-time #define such as
我认为最好的选择是使用多个 makefile 或任何您使用的文件。每种配置一个。不要使您的源文件不可读。
您可以将常用文件放置在 Common 目录中,将其他文件放置在单独的目录中 - 每个配置对应一个目录。
I suppose the good choise is to use several makefiles or whatever you use. One for each configuration. Do not make your source files unreadable.
You can place common files in the Common directory, and other files in separate directories - one directory for each configuration.
在 Visual Studio(如果这是您的 IDE)中,您可以拥有多个“配置”(默认情况下为“调试”和“发布”),并且每个构建中可能不包含某些文件。您可以进行配置“Debug main”(排除 secondary/config.cpp),并配置“Debug secondary”(排除 main/config.cpp)。
如果您不使用 Visual Studio,我相信有一种方法可以对 make 文件执行类似的操作。
In Visual Studio (if that's your IDE) you can have multiple "configurations" (by default Debug and Release), and it's possible to have certain files not included in each build. You could make configuration "Debug main" which excludes secondary/config.cpp, and configuration "Debug second" which excludes main/config.cpp.
If you're using not using Visual Studio, I believe there's a way to do something similar with make files.
您只需将相关目录添加到编译器包含路径的前面即可。
您可以通过某些编译器选项更改编译器的包含路径(这取决于编译器)。
对于 .cpp 文件来说是一样的。只需在普通源代码树中有一个 .cpp 文件,其中通过
#include
指令包含该 .cpp 文件。干杯&呵呵,
You can just add the relevant directory to the front of the compiler's include path.
You can change the compiler's include path via some compiler option (it depends on the compiler).
For the .cpp file it's the same. Just have a .cpp file in your ordinary source tree, that includes that .cpp file via a
#include
directive.Cheers & hth.,