Skip to content

Instantly share code, notes, and snippets.

@retorillo
Last active April 1, 2018 17:51
Show Gist options
  • Save retorillo/1159fe5db0714040e7c9df166bc8b26a to your computer and use it in GitHub Desktop.
Save retorillo/1159fe5db0714040e7c9df166bc8b26a to your computer and use it in GitHub Desktop.

How to write your own build script by using benefit from MSBuild environment variables configuration corresponding specified platform (C++)

The following XML enables to resolve this challenge. SetBuildDefaultEnvironmentVariables is key of this.

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="ManualBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PlatformToolset>v140</PlatformToolset> <!-- (Optional) only if want to explicitly specify platform toolset version -->
    <TargetPlatformVersion>8.1</TargetPlatformVersion> <!-- (Optional) only if want to explicitly specify SDK version -->
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <Target Name="ManualBuild">
    <CallTarget Targets="SetBuildDefaultEnvironmentVariables" />
    <Exec Command="echo %LIB%" />
  </Target>
</Project>

Assumes the above XML is named as example.xml, the following msbuild command will print LIB environment variable for linking x86 libraries no matter whether your machine is x64 or not.

msbuild example.xml /p:Platform=x86

On the other hand, the next one will print for ARM.

msbuild example.xml /p:Platform=arm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment