Skip to content

Instantly share code, notes, and snippets.

@cfriedt
Last active May 30, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfriedt/c7afa37e4c054bead3d12166cdade5e2 to your computer and use it in GitHub Desktop.
Save cfriedt/c7afa37e4c054bead3d12166cdade5e2 to your computer and use it in GitHub Desktop.
Minimal C/C++ Build Environment for Windows Server 2016 on Google Cloud

Minimal C/C++ Build Environment for Windows Server 2016 on Google Cloud

These instrctions are an abridged version of the instructions to build WireShark for Windows found here.

In this case, the regular traditional Windows Command Prompt is used for executing commands, not the Windows PowerShell.

Rather than using a mingw environment (for gcc) or using LLVM/clang, we opt to simply use Microsoft Visual Studio. However, the Chocolatey package manager provides builds for a number of additional tools that might make Windows development seem a bit more familiar for non-Windows developers.

Create a new VM Instance

First, we have to set up a virtual machine (VM) from the Google Cloud Console.

  1. Navigate to the Menu (☰), Compute Engine, and VM Instances
  2. Hit Create Instance
  3. Under Boot disk, select Change, then Operating System: Windows Server, and Version: Windows Server 2016 Datacenter Core, and hit Select
  4. Press Create
  5. When the VM is ready, click on the instance name "e.g. instance-1", and then Set Windows Password, Set, copy the password, and choose Close
  6. Hit the RDP button (requires Chrome RDP for Google Cloud Platform)

Install the Chocolatey Package Manager

Install Chocolatey as described here.

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command " [System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Install Chocolatey Packages

Note: This part could take an extraordinarily long amount of time.

choco install -y cmake visualstudio2019community visualstudio2019-workload-nativedesktop

Add New Tools to the current PATH

set "PATH=%PATH%;C:\Program Files\CMake\bin"
set "PATH=%PATH%;C:\Program Files\Git\bin"
set "PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build"

Set Visual Studio Environment Variables

Note, there are several sets of environment variables that may be used and they are listed here. Below, we are setting-up to build a 64-bit executable.

vcvars64

Create a Simple CMake Project

Here we create a subdirectory and files for a traditional Hello, World project using C++ and CMake.

mkdir hello
cd hello
notepad hello.cpp

The contents of hello.cpp are blow.

#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  cout << "Hello, world!" << endl;
  return 0;
}

Edit a CMake file using notepad CMakeLists.txt and paste the following into it.

cmake_minimum_required(VERSION 3.10)
project(hello)
add_executable(hello hello.cpp)

Build The Project

On Linux and other POSIX OS's, usually Makefiles are created. However, on Windows, the default approach is to use MSBuild. This requires calling some variant of the vcvars batch file (listed above) to set correct environment variables.

cmake .
msbuild hello.sln

You should observe output that appears something like the listing below.

C:\Users\username\hello>vcvars64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.6.0
** Copyright (c) 2020 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
C:\Users\username\hello>cmake .
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.14393.
-- The C compiler identification is MSVC 19.26.28805.0
-- The CXX compiler identification is MSVC 19.26.28805.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/bin/Hostx64/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/username/hello

C:\Users\username\hello>msbuild hello.sln
Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.
Build started 5/28/2020 3:18:47 PM.
Project "C:\Users\username\hello\hello.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|x64".
ValidateProjects:
  The project "ALL_BUILD" is not selected for building in solution configuration "Debug|x64".
Project "C:\Users\username\hello\hello.sln" (1) is building "C:\Users\username\hello\ZERO_CHECK.vcxproj" (2) on
node 1 (default targets).
InitializeBuildStatus:
  Creating "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
CustomBuild:
  All outputs are up-to-date.
FinalizeBuildStatus:
  Deleting file "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
  Touching "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate".
Done Building Project "C:\Users\username\hello\ZERO_CHECK.vcxproj" (default targets).

Project "C:\Users\username\hello\hello.sln" (1) is building "C:\Users\username\hello\hello.vcxproj.metaproj" (3)
 on node 1 (default targets).
Project "C:\Users\username\hello\hello.vcxproj.metaproj" (3) is building "C:\Users\username\hello\hello.vcxproj"
 (4) on node 1 (default targets).
InitializeBuildStatus:
  Touching "hello.dir\Debug\hello.tlog\unsuccessfulbuild".
CustomBuild:
  All outputs are up-to-date.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\CL.exe /c /Z
  i /nologo /W3 /WX- /diagnostics:column /Od /Ob0 /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /EHsc
   /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"hello.dir\Debug\\" /Fd"hello.dir\Debug\vc142
  .pdb" /Gd /TP /errorReport:queue C:\Users\username\hello\hello.cpp
  hello.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\link.exe /ER
  RORREPORT:QUEUE /OUT:"C:\Users\username\hello\Debug\hello.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32
  .lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"leve
  l='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/Users/username/hello/Debug/hello.pdb" /SUBSYSTEM:
  CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/Users/username/hello/Debug/hello.lib" /MACHINE:X64  /machine:
  x64 hello.dir\Debug\hello.obj
  hello.vcxproj -> C:\Users\username\hello\Debug\hello.exe
FinalizeBuildStatus:
  Deleting file "hello.dir\Debug\hello.tlog\unsuccessfulbuild".
  Touching "hello.dir\Debug\hello.tlog\hello.lastbuildstate".
Done Building Project "C:\Users\username\hello\hello.vcxproj" (default targets).

Done Building Project "C:\Users\username\hello\hello.vcxproj.metaproj" (default targets).

Done Building Project "C:\Users\username\hello\hello.sln" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.34

Run hello

Run Debug\hello

Hello, world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment