I have a configuration class that I would like to use for a variety of builds. The class itself changes between builds, but the c开发者_Go百科lass 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! >_<
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
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.
You can put into your header/cpp guards like this:
#ifdef OPTION_A
...
#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
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.
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.
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.,
精彩评论