I am writing a Visual Studio extension in C# and I need to capture programmatically all the options set by the compiler when the user runs a build.
I have realized that I can get some of them from the Project Properties (like all the -I and -D). But some are "invisible" and I realized that they are the macros set by the compiler itself. I have found a page listing all of them at: https://learn.microsoft.com/fr-fr/cpp/preprocessor/predefined-macros?view=vs-2019.
I could find a lot of those I am looking for: __cplusplus, _MSVC_LANG, _MSC_VER, _CPPRTTI, _MSC_FULL_VER, _MSC_EXTENSIONS, etc.
I am looking for the (C#) API allowing to get all those macros values programmatically so my VS extension can get them and inject them in my process.
Thanks for your help!
The normal way I know to get the expanded value of macros is to use MSBuild knowledge.
Choose any C++ project, add this script into the xx.vcxproj
and we can get the details in output log:
<Target Name="GetExpandedValue" AfterTargets="build">
<Message Text="VS_IncludePath = $(VC_IncludePath)" Importance="high" />
<Message Text="WindowsSDK_IncludePath = $(WindowsSDK_IncludePath)" Importance="high" />
</Target>
But it's hard to get the expanded value by any api from VS SDK. Those macros are defined in a msbuild props file: C:\Program Files (x86)\Microsoft Visual Studio\xxx\xxx\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Common.props
.Those macros are defined there and I don't find any api in VS SDK or .net framework that can be used expand them.
I can only imagine a possible way like this, call the msbuild.exe
programmatically to build a xx.csproj file with script above, and get the output logger. Then parse it to get the useful info we need from the log. It would be a big job...
So if you don't have special need for the expanded value, I suggest you get the value in your machine, and insert the value into your extension code. (As I know, if we use same VS version with similar workloads, the value of those unique macros are the same. )