CMake -G Ninja on Windows specify x64 - windows

I am using CMake on Windows with Ninja generator
cmake -G Ninja ..
This uses the default Windows x86 toolchain.
How to specify x64 using the Ninja generator?
PS: I know how to generate x64 with Visual Studio
cmake -G "Visual Studio 12 2013 Win64 ..

You have to set the compiler environment accordingly before calling Ninja generation. If you have Visual Studio 2013 installed at the standard installation path you call:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x64
cmake.exe -G "Ninja" ..
Edit: Thanks for the hint from #Antwane: "Or simply run CMake command from a Microsoft Visual Studio Command Prompt (x64). A shortcut to this prompt is located in Start Menu".
The naming varies over the Visual Studio versions:
When I then look into the generated CMakeCache.txt file I see:
...
//CXX compiler
CMAKE_CXX_COMPILER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64/cl.exe
...
//Flags used by the linker.
CMAKE_EXE_LINKER_FLAGS:STRING= /machine:x64
...
//Path to a program.
CMAKE_LINKER:FILEPATH=C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/amd64/link.exe
...

When I tried to run cmake on command line in Windows, trying to use Ninja and targetting the Visual Studio 14.0 compiler (2015), it kept picking up on other installed compilers (in my case gcc) instead.
The following command line worked:
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER="cl.exe" -DCMAKE_CXX_COMPILER="cl.exe" -DMSVC_TOOLSET_VERSION=140 ..

This answer assumes you have Visual Studio installed and have installed all the proper C++ libraries and what not when you installed visual studio.
Start typing in "x64 Native Tools" in your start menu. Open the file location and you will see a certain amount of shortcuts. On my visual studio installation I only have compilers for the x86/x64 architecture.
Anyway now that you have opened the file location you will be presented with a bunch of developer command prompt shortcuts:
PS C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC> ls
Directory: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/16/2021 8:12 PM 2139 x64 Native Tools Command Prompt for VS 2019 Preview.lnk
-a--- 2/16/2021 8:12 PM 2197 x64_x86 Cross Tools Command Prompt for VS 2019 Preview.lnk
-a--- 3/3/2021 9:01 PM 2139 x86 Native Tools Command Prompt for VS 2019 Preview.lnk
-a--- 2/16/2021 8:12 PM 2197 x86_x64 Cross Tools Command Prompt for VS 2019 Preview.lnk
PS C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\VC>
Let me explain what each one does:
x64 Native Tools Command Prompt for VS 2019 Preview
Use x64 compiler to compile for a x64 machine
x64_x86 Cross Tools Command Prompt for VS 2019 Preview
Use x64 compiler to compile for a x86 machine
x86 Native Tools Command Prompt for VS 2019 Preview
Use x86 compiler to compile for a x86 machine
x86_x64 Cross Tools Command Prompt for VS 2019 Preview
Use x86 compiler to compile for a x64 machine
In my opinion the last 2 are useless in the modern day. And have caused multiple CI crashes on our servers. So we only use 64 bit binaries.
Anyway you double click on one of the shortcuts:
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.10.0-pre.1.0
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview>
And bang you will have a command prompt loaded with an environment CMake can recognize. This is because the PATH environment variable now contains the compilers and what not.
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview>echo %PATH%
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\\Extensions\Microsoft\IntelliCode\CLI;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.29917\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Team Tools\Performance Tools\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Team Tools\Performance Tools;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\\x64;C:\Program Files (x86)\Microsoft Visual Studio\Shared\Common\VSPerfCollectionTools\vs2019\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\Tools\devinit;C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\\MSBuild\Current\Bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\LLVM\bin;C:\Program Files\Git\cmd;C:\Program Files\PowerShell\7\;C:\Program Files\CMake\bin;D:\Installations\Python\Scripts\;D:\Installations\Python\;C:\Users\juanr\AppData\Local\Microsoft\WindowsApps;D:\Installations\Microsoft VS Code\bin;C:\Users\juanr\AppData\Local\GitHubDesktop\bin;D:\Git\ninja;;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\Llvm\x64\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja
Now you can run cmake with Ninja:
NOTE: In this example I'm manually specifying the path to Ninja. You can also just add Ninja to your path. And depending on your visual studio installation this may already be done for you. If you download the "C++ CMake tools for Windows" you have Ninja added to your path for you. You even get CMake added for you.
cmake -S . -B build -G "Ninja" -DCMAKE_MAKE_PROGRAM=C:/foobar/ninja.exe

Related

error: Microsoft Visual C++ 14.0 or greater is required

While installing python package I get the following error even though I have latest Microsoft Visual Studio BuildTools installed.
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
I have added all possible paths to Path variables as well
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VisualStudio\v16.0\Python Tools
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
C:\Program Files (x86)\MSBuild\14.0\Bin
C:\Program Files (x86)\MSBuild
Installing fulll vs_BuildTools solved it for me . For details follow
https://www.scivision.dev/python-windows-visual-c-14-required
Note MSVC vXXX - VS YYYY C++ x64/x86 build tools version and Windows 10 SDK versions must match
Quick solutions like instaling vc_redist.x64 or VC_redist.x86 didn't
solve it for me.
Nothing needs to be added to environment variables.
Answer
If your vscode is supported in 32 bit version, it raises when you start install some packages. I recommended you to install 64 bit vscode if you are using windows 10.

How to set environment variables for MSVS 2019 with old version of MSVS?

I've installed MSVC++ 6.0 in my computer so it has set the environment variables.
Now I've installed MSVC++ 2019 but when I go to
Computer properties->advanced system settings->Environment variables
Include contains only C:\Program Files (x86)\Microsoft Visual Studio\VC98\atl\include;C:\Program Files (x86)\Microsoft Visual Studio\VC98\mfc\include;C:\Program Files (x86)\Microsoft Visual Studio\VC98\include
and the remaining path, libs... all contains folders of MSVC++6 but not 2019.
I need this because sometimes I want to compile my C++20 code from CMD not from IDE.
I've run as administrator VsDevCmd.bat but it didn't make changes!
So how can I set the environment variables for visual Studio 2019?

Visual Studio 2015 Installer doesn't install cl.exe

I had installed Visual Studio 2017 on my machine with VC2015.3 C++ compiler toolset. The VC14 compiler is now located in C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\bin\cl.exe
Now, when I install Visual Studio 2015 and select to install "Common Tools for Visual C++ 2015" there is no cl.exe in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin as expected and as it always have been.
It's just like the installer "know" that I have already VC14 compiler installed (through VS2017) and skips it completely.
What is the issue here?

visual studio 2005 32 bit and 64 bit building from command promt

I want to build a project automatically from command prompt. I am using Visual Studio 2005. I want to build for x86 and x64 platforms. I tried running "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" myproject.sln /Build "Release|x86" but its not working.
Use "Release|Win32" and "Release|x64".

Visual Studio C++ and OpenMPI in windows: missing files.h

I've just correctly installed Visual Studio Express C++ and OpenMPI.
I've added the path to PATH variable:
C:\Program Files\OpenMPI_v1.5.5-win32\bin;
C:\Program Files\Microsoft Visual Studio 10.0\VC\bin;
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE;
C:\Program Files\Microsoft Visual Studio 10.0\VC\include
and I've written a simple program in MPI on visual c++ express.
The problem is that when I try to compile from prompt dos
mpicxx -o mpi mpi01.cpp
it tells me that there are a lot of missing files.h (no such file od directory) which are all inside
C:\Program Files\Microsoft Visual Studio 10.0\VC\include
But, as I've just said, I've included this in PATH.
Why does it miss them all?
Your "prompt dos" should be the one you get when you launch the Visual Studio "Developper Command prompt". Shortcut located in your Start Menu -> Programs -> Microsoft Visual Studio -> Visual Studio Tools

Resources