I am working on a C project in Visual Studio 2019, which should have the ability to be compiled as an executable and as a DLL. It is legacy code and contains the preprocessor flags AS_EXE and AS_DLL, which is presumably the flags I need to set. I just don't know how to do this based on different values in Project -> Properties -> Configuration Properties -> General -> Configuration Type. I would like the values Dynamic Library (.dll) and Application (.exe) to map to the two preprocessor definitions, AS_DLL and AS_EXE respectively.
I have looked around on Google the last couple of days, but without luck.
It would be nice if it could be done similarly to when switching between debug and release, but I sense that it'll be a bit more complex. Can this even be achieved? And if so, how would I go about doing it?
CodePudding user response:
You can use Condition attribute of ItemDefinitionGroup node in your .vcxproj.
<ItemDefinitionGroup Condition="'$(Configuration)'=='MyConfigForDLL'">
<ClCompile>
<PreprocessorDefinitions>AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
...
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='MyConfigForEXE'">
<ClCompile>
<PreprocessorDefinitions>AS_EXE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
...
</ClCompile>
</ItemDefinitionGroup>

