I need to run scripts that build a visual studio solutions using devenv.exe (or devenv.com for that matter). For visual studio 2015 there was an environment variable %VS140COMNTOOLS% that I could use to find the install location of devenv. Since there is no %VS150COMNTOOLS% for Visual Studio 2017, what would be a reliable way to find the install location of devenv in a script (bat or powershell).
One way is to use power shell and vswhere.exe. But I'm bit lazy to install new tools and ...
I was trying to find simpler solution and found it from registry - there exists registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7, which lists all Visual studio installations.
One of limitations mentioned in this link:
https://developercommunity.visualstudio.com/content/problem/2813/cant-find-registry-entries-for-visual-studio-2017.html
If there is more than one edition of 2017 installed, then it seems the last one installed will have their path in this key.
But typically you install only one visual studio for build or use purpose.
Also I've coded this sample from 64-bit machine perspective, I think Wow6432Node does not exits in 32-bit machines, but really - how many developers use 32-bit machines nowadays ?
So if you're fine with limitations above, here is a simple batch which can query visual studio installation path:
test.bat :
#echo off
setlocal
call:vs%1 2>nul
if "%n%" == "" (
echo Visual studio is not supported.
exit /b
)
for /f "tokens=1,2*" %%a in ('reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" /v "%n%.0" 2^>nul') do set "VSPATH=%%c"
if "%VSPATH%" == "" (
echo Visual studio %1 is not installed on this machine
exit /b
)
echo Visual studio %1 path is "%VSPATH%"
endlocal & exit /b
:vs2017
set /a "n=%n%+1"
:vs2015
set /a "n=%n%+2"
:vs2013
set /a "n=%n%+1"
:vs2012
set /a "n=%n%+1"
:vs2010
set /a "n=%n%+10"
exit /b
Can be executed like this:
>test 2010
Visual studio 2010 path is "C:\Program Files (x86)\Microsoft Visual Studio 10.0\"
>test 2012
Visual studio 2012 path is "C:\Program Files (x86)\Microsoft Visual Studio 11.0\"
>test 2013
Visual studio 2013 path is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\"
>test 2014
Visual studio is not supported.
>test 2015
Visual studio 2015 path is "C:\Program Files (x86)\Microsoft Visual Studio 14.0\"
>test 2017
Visual studio 2017 path is "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\"
You can use vswhere.exe or powershell to find your Visual Studio instances:
for /r "usebackq tokens=1* delims=: " %%i in (`vswhere.exe -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop`) do (
if /i "%%i"=="installationPath" set dir=%%j
)
and
Install-Module VSSetup -Scope CurrentUser
Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64
The path to specific workloads can be found through this api as well.
https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/
Here's a PowerShell function that can be used to get the DevEnv.exe path. You can also easily modify it to find other common components, like MsBuild.exe or TF.exe simply by changing the hardcoded DevEnv.exe string used in the -Filter.
function Get-DevEnvExecutableFilePath
{
[bool] $vsSetupExists = $null -ne (Get-Command Get-VSSetupInstance -ErrorAction SilentlyContinue)
if (!$vsSetupExists)
{
Write-Verbose "Installing the VSSetup module..."
Install-Module VSSetup -Scope CurrentUser -Force
}
[string] $visualStudioInstallationPath = (Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.Component.MSBuild).InstallationPath
$devEnvExecutableFilePath = (Get-ChildItem $visualStudioInstallationPath -Recurse -Filter "DevEnv.exe" | Select-Object -First 1).FullName
return $devEnvExecutableFilePath
}
It works by installing the VSSetup module if it's not already present, and then querying that module for the latest Visual Studio installation path. From there, it searches the Visual Studio installation path for the DevEnv.exe file and returns its full path.
My own solution using registry here does not work unfortunately for Visual studio 2019, for it it's possible to query vs2019 installation path from Uninstall registry path, but is starts to be little bit difficult as you might need to walk through sub registry keys.
I have tried also a little bit vswhere.exe, but it's usage is also bit difficult - need to reparse it's output.
Meanwhile I've needed something similar to vswhere, and took a glance look on it's implementation and re-wrote it under my own command line tool, called cppexec.exe.
Usage is like this:
D:\Prototyping\cppscriptcore>cppexec.exe -location
Visual studio 2019:
location: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise
Visual studio 2017:
location: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise
D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2017
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise
D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2019
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise
To use it, download two files:
https://github.com/tapika/cppscriptcore/blob/master/cppexec.exe
https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel.dll
If you're interested in integrating into your own project, take a look on following source codes:
https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.cpp
https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.h
I've recoded vswhere into slightly simpler manner.
I found a very simple way, just run the Visual Studio Developer Command Prompt, and run this command: where devenv
It will return the full path for devenv.com and devenv.exe file
Location for Visual Studio 2022 : C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE
None of the other solutions worked for me. I want to do this with a simple snippet in a bat/cmd file (no powershell), with no external tools, and without depending on the vcvars32 or other bat files having been run already.
The idea of using vswhere has been mentioned by others, but they were not including the path to the tool, and syntax for parsing the output seemed to be off.
The following snippet works for me:
for /f "usebackq tokens=1* delims=: " %%i in (`"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -latest`) do (
if /i "%%i"=="productPath" set devenv=%%j
)
echo Using Visual Studio: %devenv%
"%devenv%" MySolution.sln &
Related
None of my terminals on Windows 10 recognize cl 'as an internal or external command'.
I have Visual Studio 2022 installed and I've tried it on every terminal, including terminals in the Visual Studio 2022 under the Start menu.
I've tried the other solutions to Stack Overflow. I tried setting an environment variable of cl to "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx86\x86\cl.exe"
Under 'Modify' in Visual Studio 2022 I have the boxes for 'Desktop development with C++' and 'Universal Windows Platform development' checked and installed.
I know there are other answers to this question but Visual Studio 2022 seems like it make have another solution.
Please find in your system this path:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\Hostx64\x64
and add this path in the user variable path for your user name in the edit system environment variables.
this will work.
Add the following path in both system and user environment variables path option
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\bin\Hostx64\x64
msbuild : The term 'msbuild' is not recognized as the name of a cmdlet,
function, script file, or operable program.
As far as my googling goes, Visual Code should come with MSbuild. I have installed the C/C++ and msbuild Tools extensions to no avail. What can I do?
Edit: I am using Visual Studio Code 1.19.2
Likely it's a path issue. If you have VS2017 installed, it's probably in the directory C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin
Try adding that to your path, then restart VS Code and try again.
I suggest you rethink you command-line approach.
Short plan
Install Visual Studio Build Tools 2017
Find proper MSBuild
Use it
Details
Using Build Tools will give you independence from Visual Studio installation.
Download Build Tools for Visual Studio 2017 from Visual Studio Downloads page (direct link)
Command-line arguments documented here: Use command-line parameters to install Visual Studio 2017
All workloads and components are listed here: Visual Studio Build Tools 2017 component directory
You can use PowerShell module VSSetup. Download it or install from here: Github: Microsoft/Visual Studio Setup PowerShell Module
Run MSBuild with build target (you can add additional required parameters)
# 1. Find MS Build
Import-Module $PSScriptRoot\VSSetup\VSSetup.psd1
$msBuildPath = (Get-VSSetupInstance | Select-VSSetupInstance -Version 15.0 -Product Microsoft.VisualStudio.Product.BuildTools).InstallationPath
if ([System.IntPtr]::Size -eq 8)
{
$global:msbuildPath = Join-Path $msBuildPath 'MSBuild\15.0\Bin\amd64'
}
else
{
$global:msbuildPath = Join-Path $msBuildPath 'MSBuild\15.0\Bin'
}
Write-Output "Using MSBuild from $global:msbuildPath"
Write-Output "MSBuild /version"
$msbuild = Join-Path $global:msbuildPath msbuild
& $msbuild /version
# 2. Build
& $msbuild "$sln_file" /t:Build /v:q /nologo
I don't believe that this is a good solution, but it's what I hacked together since the other suggestions didn't work. I installed Visual Studio Installer and installed MSBuild through that. I am having other issues now but at least msbuild is running.
I faced the same issue, what worked with me was to use dotnet instead of msbuild :
dotnet build
I downloaded Visual C++ and Visual Studio, but I cannot find cl.exe to compile my C++ file. The path to the install is `C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin.
Where can I find the compiler to compile C++ code?
Visual Studio 2015 doesn't install C++ by default. You have to rerun the setup, select Modify and then check Programming Language -> C++
In Visual Studio 2019 you can find cl.exe inside
32-BIT : C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\bin\Hostx86\x86
64-BIT : C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.20.27508\bin\Hostx64\x64
Before trying to compile either run vcvars32 for 32-Bit compilation or vcvars64 for 64-Bit.
32-BIT : "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars32.bat"
64-BIT : "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
If you can't find the file or the directory, try going to C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC and see if you can find a folder with a version number. If you can't, then you probably haven't installed C++ through the Visual Studio Installation yet.
For me that have Visual Studio 2015 this works:
Search this in the start menu: Developer Command Prompt for VS2015 and run the program in the search result.
You can now execute your command in it, for example: cl /?
For first need check is installed cl.exe,
open Developer Command Prompt for VS2015 and type "where cl"
When its founded on the PC need add $(VC_LibraryPath_x64), $(WindowsSDK_LibraryPath_x64) and $(NETFXKitsDir)Lib\um\x64 to Project->Properties->VC++ Directories
I can compile from the command line by running vcvarsall.bat, then running cl.exe with my source file as the only argument. The problem is that just running cl.exe without first setting up the environment using the aforementioned batch file causes errors due to "missing libraries".
I can picture a couple of workarounds to this, for example creating a batch file in my project directory that includes vcvarsall.bat and does all the compiling. That seems to me to be a very cumbersome and inflexible solution though. I would much prefer to be able to compile and run any C or C++ file from Vim like one would in Unix:
:nnorebind <F5> :! g++ %<CR>
I don't want to add the pile of paths that vcvarsall.bat adds to the environment permanently though. I don't know how to phrase my question any better than this: How do I set up Vim to be able to use the Visual Studio 2010 C++ compiler?
You can update environment variables from within Vim. For instance, to add "c:\foo" to the start of your PATH:
let $PATH='c:\\foo;'.$PATH
I copied the relevant portions of my vcvars*.bat file to my vimrc:
if !exists("visual_studio_paths_added")
let visual_studio_paths_added = 1
let $PATH="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\BIN;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\Tools;C:\\Windows\\Microsoft.NET\\Framework\\v3.5;C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\VCPackages;C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\bin;".$PATH
let $INCLUDE="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\ATLMFC\\INCLUDE;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\INCLUDE;C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\include;".$INCLUDE
let $LIB="C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\LIB;C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A\\lib;".$LIB
let $LIBPATH="C:\\Windows\\Microsoft.NET\\Framework\\v3.5;C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\ATLMFC\\LIB;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\LIB;".$LIBPATH
endif
Adapt as needed given your version of Visual Studio and installation directory.
In my case, I put this in a function. I call :vcvars when I want to work with Visual Studio. But you could just have those changes always included.
Expanding on the comment by David RodrÃguez - dribeas, and seeing how vcvarsall.bat accepts parameters, I found it helpful to have the following file in my source trees:
vc.bat:
call "%VS100COMNTOOLS%\..\..\VC\vcvarsall.bat" amd64 & %*
Adjust MSVS version and platform as appropriate, or make them additional parameters if you like. The %VS...COMNTOOLS% environment variable is set by the MSVC installer, which makes the above independent of the actual installation path.
This allows me to run any <command> in the proper MSVC environment by running it as:
vc.bat <command>
If called without parameters, it will just set the environment for the current shell without executing any further commands.
If you want to go one step further, there is CMake, a meta-buildsystem that allows you to generate Unix Makefiles, MSVC solutions and several other buildfile types from a single (CMake syntax) configuration. One of the buildfile types supported on Windows are NMake Makefiles... and nmake <target> on Windows handles just about as well as make <target> does on Unix.
Mud's solution did not work for me with Visual Studio 2017 Community. Paths are different. I think DevSolar's way to use vcvarsall.bat is more reliable, as it lets VS take care of the correct paths.
As I don't want to carry around a .bat file with my vim config, I was looking for a solution to work only inside the .vimrc:
Add vcvarsall.bat to your PATH. In my case c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC.
Put the following in your .vimrc to compile current file by F8
nnoremap <F8> :!vcvarsall.bat && cl /EHsc % <CR>
When trying to build a boost 1.33.1 library with bjam, I keep getting thousands of errors with command similar to this:
CALL "C:\Program Files\Microsoft Visual Studio\VC98\bin\VCVARS32.BAT" >nul
Is there any way to change where bjam searchs for Microsoft Visual Studio for boost 1.33.1? My Visual Studio installation is located in "C:\Program Files\Microsoft Visual Studio 8"
Thank you for the suggestions ildjarn and Nicol Bolas.
I did a manual text search for VC98 and found an HTML help file, msvc-tools.html, which states that if the MSVC_ROOT variable defaults to "c:\Program Files\Microsoft Visual Studio\VC98" if not specified. I simply set the MSVC_ROOT variable to my Visual Studio 8 directory and things compiled.