'Build a .sln / .vcxproj project from command line with a free edition of Visual C++

If I don't want to use the features of the Visual Studio IDE, is it possible to install a non-time-limited / non-30-days-limited-demo of a Microsoft C++ compiler (which version?), and build a .sln or .vcxproj project directly from command line?

Here is what I succesfully use for single .cpp file projects:

call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86
cl helloworld.cpp /link user32.lib

Is there a way to extend this to .sln or .vcxproj projects?



Solution 1:[1]

I use devenv for this. Note that, at least with VS 2017 Community, you must open the VS gui and sign in with a Microsoft account, otherwise it will stop allowing compiles after 30 days.

PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE
devenv Solution.sln /build Debug /project project

(This also works even if you don't run vcvarsall.bat, which is nice)

Solution 2:[2]

If we want to not use the IDE, it's possible to install just the VS Build Tools (the installer is named vs_BuildTools.exe for VS2019), and then just use msbuild.

Example: https://docs.microsoft.com/en-us/cpp/build/walkthrough-using-msbuild-to-create-a-visual-cpp-project?view=msvc-160

build.bat:

MSBuild.exe helloworld.vcxproj /p:configuration=release /p:platform=x64

helloworld.vcxproj:

<Project DefaultTargets="Build" ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ProjectConfiguration Include="Release|x64">
      <Configuration>Release</Configuration>
      <Platform>x64</Platform>
    </ProjectConfiguration>    
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />
  <PropertyGroup>
    <ConfigurationType>Application</ConfigurationType>
    <PlatformToolset>v142</PlatformToolset>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
  <ItemGroup>
    <ClCompile Include="main.cpp" />
  </ItemGroup>
  <ItemGroup>
    <ClInclude Include="main.h" />
  </ItemGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Targets" />
</Project>

main.cpp:

#include <iostream>
#include "main.h"
int main()
{
   std::cout << "Hello, from MSBuild!\n";
   return 0;
}

and an empty main.h file.

No .sln file is really needed in this case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0x5453
Solution 2 Basj