我可以设置编译时标志来包含标头吗?

发布于 2024-11-30 01:26:59 字数 424 浏览 0 评论 0原文

我有一个配置类,我想将其用于各种构建。类本身在构建之间发生变化,但类名保持不变,头文件名也保持不变。

此类的单独版本保存在单独的子文件夹中。

前任。

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

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

发布评论

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

评论(7

为人所爱 2024-12-07 01:26:59

根据您使用的构建系统,您将创建一个指向主路径或辅助路径的变量。然后使用此变量附加到 INCLUDE 路径,以便所有源在需要访问配置时只需 #include "config.h" 即可。在您的 Makefile(或同等文件)中,您需要将 $CONFIGPATH/config.cpp 添加到要构建的源代码中。

MSBuild

更新源文件路径:

<ItemGroup>
    <ClCompile Include="main.cpp" />
    <ClCompile Include="$(ConfigToUse)/config.cpp" />
</ItemGroup>
<ItemGroup>
     <ClInclude Include="$(ConfigToUse)/config.h" />
</ItemGroup>

和包含路径:

<PropertyGroup>
    <ConfigurationType>Application</ConfigurationType>
    <ShowAllFiles>false</ShowAllFiles>
    <IncludePath>...;$(ConfigToUse);</IncludePath>
</PropertyGroup>

然后调用 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:

<ItemGroup>
    <ClCompile Include="main.cpp" />
    <ClCompile Include="$(ConfigToUse)/config.cpp" />
</ItemGroup>
<ItemGroup>
     <ClInclude Include="$(ConfigToUse)/config.h" />
</ItemGroup>

And the include path:

<PropertyGroup>
    <ConfigurationType>Application</ConfigurationType>
    <ShowAllFiles>false</ShowAllFiles>
    <IncludePath>...;$(ConfigToUse);</IncludePath>
</PropertyGroup>

Then call msbuild build.xml /p:ConfigToUse=PathToConfig

在你怀里撒娇 2024-12-07 01:26:59

#include 指令并不真正关心内容。

您可以轻松地拥有一个在项目中使用的存根类:

stub.cpp

#ifdef BUILD1
    #include "main/realimpl1.cpp"
#else
    #include "secondary/realimpl2.cpp"
#endif

当然,如果需要,您可以对标头执行相同的操作。

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

#ifdef BUILD1
    #include "main/realimpl1.cpp"
#else
    #include "secondary/realimpl2.cpp"
#endif

And, of course, you can do the same thing with headers if necessary.

四叶草在未来唯美盛开 2024-12-07 01:26:59

您可以像这样放入 header/cpp 防护中:

#ifdef OPTION_A

...

#endif

You can put into your header/cpp guards like this:

#ifdef OPTION_A

...

#endif
北笙凉宸 2024-12-07 01:26:59

您可以使用编译时#define,例如

#define USE_BUILD_X 1
//#define USE_BUILD_Y 1    
...
...
#ifdef (USE_BUILD_X)
#include "mainheader.h"
#elif (USE_BUILD_Y)
#include "secondheader.h"
#endif

You can use compile-time #define such as

#define USE_BUILD_X 1
//#define USE_BUILD_Y 1    
...
...
#ifdef (USE_BUILD_X)
#include "mainheader.h"
#elif (USE_BUILD_Y)
#include "secondheader.h"
#endif
听,心雨的声音 2024-12-07 01:26:59

我认为最好的选择是使用多个 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.

尐偏执 2024-12-07 01:26:59

在 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.

三五鸿雁 2024-12-07 01:26:59

您只需将相关目录添加到编译器包含路径的前面即可。

您可以通过某些编译器选项更改编译器的包含路径(这取决于编译器)。

对于 .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.,

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