Run .bat file on VS cmd prompt - c++

I have created a compile.bat and run.bat files, but when I double click on them they run on the Windows cmd prompt instead of the VS cmd prompt.
This is what I have in my compile.bat file:
devenv FileMgr.sln /rebuild debug
pause
The Windows cmd says "'devenv is not recognized as an internal or external command, operable program or batch file."

If I recall correctly, all the Visual Studio Command Prompt is is a batch script itself that initializes paths and environment variables. So if you simply call that batch script at the top of yours, you'll have all those settings for your script.
According to this question, MSVC 2008 has that batch file here:
call "C:\Program Files\Microsoft Visual Studio 2008\VC\vcvarsall.bat
It may not be in exactly the same place, but it should be something similar on your machine.

you need devenv.exe to be present at location where your compile.bat is running. Please look at solution given here-
Simply type devenv.exe from the command line. If you get a message like this, then you do not have devenv.exe in your path. >>> 'devenv.exe' is not recognized as an internal or external command, operable program or batch file. >>> To fix this simply run the batch file, vsvars32.bat that comes with Visual Studio.NET from the command line in the working folder. After you run this batch file devenv.exe will be available from the command line in that folder.
http://windowsclient.net/blogs/faqs/archive/2006/05/26/how-do-i-start-visual-studio-from-the-command-line.aspx
I would usually copy the devenv.exe manually to the location where my bat file is kept.

The VS cmd prompt sets some additional environment variables and adds data to existing (eg the PATH variable). You might consider adding the path of devenv to you normal path.

You may specify full path of devenv.exe:
C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\devenv.exe FileMgr.sln /rebuild debug
But your batch file won't be portable to other machines/IDEs.

Related

Qt 5.12.1 with Visual Studio2017, build from command line

I linked Qt5.12.1 with Visual Studio 2017.
I want to use Qt's cmd.exe terminal to qmake an application. But, each time I open the Qt's cmd.exe terminal, it tells that
Setting up environment for Qt usage...
Remember to call vcvarsall.bat to complete environment setup!
So I must to run vcvarsall.bat, and only then I can qmake. But I think that is too troublesome. Are there any ideas to solve that problem, so that I wouldn't need to manually run vcvarsall.bat every time I open the cmd.exe terminal.
See existing Qt bug reports about the same issue you have discovered like 1 and 2. Unfortunately, you can't avoid it in Qt 5 until the issue is fixed (if ever).
But I think you could check properties of the Qt command prompt entry (press right mouse button on top of the entry which you clicked to open the command prompt) in Start menu to see which bat file it's calling and edit that script.
I think you could add something like below to the end of the bat file to get vcvarsall called automatically. Check where vccarsall.bat is located in your VS version and depending on your environment use either x86 or amd64. Note that the following line is only to get you an idea how to call another bat file from a bat file, this line sets VS2015 64bit env.
CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64

How to Run MSBuild Outside of the MSVS Dev Prompt?

I've recently been busy working on making proper build scripts for a code library I've been designing, and have been stuck on getting MSBuild to properly compile for my Windows builds. What I'm trying to do is call MSBuild from a batch script without it opening the Visual Studio IDE, which it appears to constantly do if I'm not executing it from the Developer Command Prompt for Visual Studio. My script is simple enough, just calling
start C:\Program Files (x86)\...\MSBuild\15.0\Bin\MSBuild.exe <Project>.sln
Besides a few other specifiers tagged onto the end of it, that's what I'm trying to use to run my Windows builds. The issue here is that whenever this code is called outside of the VS Dev Prompt, Visual Studio itself opens, not building the code at all. I couldn't find anyone else struggling with this same issue either, as it seems to be new to the integration of MSBuild and Visual Studio. Testing with older versions of MSBuild went to show that I could build projects as I wanted to outside of the Dev Prompt. Could I be missing some environment variable supplied in the Dev Prompt that changes the executable behavior? I couldn't seem to find any executable specifiers that would change this, either.
Maybe I'm taking a completely wrong approach to this problem? My end goal is to provide consumers with a collection of build scripts, one for each platform they're targeting, so I am definitely open to other solutions.
The canonical answer:
You can use the devenv command with the /build command line option and the name of your project file.
Alternatively, you can run one of the vcvars*.bat scripts to set up the necessary environment and then use msbuild.
For more info, see https://learn.microsoft.com/en-gb/cpp/build/building-on-the-command-line.
Update: Contrary to the official advice above, this is what works on my laptop with VS2017 community edition.
Setup the environment by running the bat file that is targeted by the "developer command prompt for VS2017". (Right-click on that in the start menu, then select "Open file location" then right-click on the shortcut and select "properties". For me that is "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat".
You should call this file from your build script.
cd to your project directory and then use msbuild like this:
msbuild My-project.vcxproj /p:Configuration=Release
For some really strange reason I can use the devenv method if I open the official developer command prompt but launching devenv from another command prompt even with the environment variables set does nothing. (And I even compared the set of environment variables in both command prompts and they are equal.)
How to Run MSBuild Outside of the MSVS Dev Prompt?
If the necessary environment not be set correct, then the build command becomes " mysolution.sln". And executing it indeed starts VS. So you should run the vcvars*.bat scripts to set up the necessary environment and then use msbuild. Following is my a batch script, it has been working for some time, you can check it:
#echo OFF
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"
echo "Starting Build for all Projects with proposed changes"
MSBuild.exe "C:\Users\Admin\Source\repos\MyTestProject\MyTestProject.sln"
pause
echo "All builds completed."
Here is the test sample result:
See How to create a Simple Build Script for Visual Studio from the Command Line? for more details.

'cl' is not recognized as an internal or external command

Below is the error message when I run "scons" to compiler in windows7:
-----------------------------------------------------------------------------------
'cl' is not recognized as an internal or external command,
operable program or batch file.
scons: *** [out\windows-x86-MD-unicode-vs2008-rel\obj-static\src\featuresets\ada pters\im-history\AddToChatRoomHistoryVisitor.obj] Error 1
scons: building terminated because of errors.
-----------------------------------------------------------------------------------
I have added path to environment variables already, but still met this problem.
Below is the PATH info of environment variables:
--------------------------------------------------------------------------------------------
PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v
1.0\;C:\Program Files\CREDANT\Shield v7.1\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Fil
es\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\TortoiseSVN\bin;C:\Python27;C:\cygwin64\bin
;C:\Python27\scons-2.3.1;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program File
s\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Progra
m Files (x86)\Microsoft Visual Studio 10.0\VC\bin;C:\Python27\Scripts;C:\Program Files (x86)\Microso
ft Visual Studio 10.0\VC\lib;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include
--------------------------------------------------------------------------------------------
The location of the cl.exe executable is not in the system PATH environment variable.
In other words the cmd.exe command line processor has been asked to run the cl.exe executable but it can't find it. It is either not in the PATH or not installed.
I have added path to environment variables already, but still met this problem.
To test this open a command prompt and type in:
cl.exe -?
SCons has logic to find the location of the visual studio install and set the path and other environment variables needed by visual studio tools by inspecting the registry and also default install locations.
If it's failing for you then you should produce a debug log and pastebin it somewhere for us to see.
use:
set SCONS_MSCOMMON_DEBUG=%TEMP%\SCONS_MSVS_DEBUG.log
and then runs scons from the same shell. The log will be in
%TEMP%\SCONS_MSVS_DEBUG.log
You might also try the latest version of SCons 2.4.1 as I note there were some fixes for MSVS/MSVC in later versions. But in theory 2.3.1 should find Visual Studio 2010..
Simply do window search for cl.exe, and add that path to PATH variable, save it. Close the existing open command and reopen it it before executing cl.exe. Hope this will work.
I was having the same issue and found a better way to fix it from #bdbaddog answer to get the debug log from set SCONS_MSCOMMON_DEBUG=%TEMP%\SCONS_MSVS_DEBUG.log SCons seem to find the cl.exe
00073ms:MSCommon/vc.py:_check_cl_exists_in_vc_dir#588:checking for cl.exe at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x64\cl.exe:
00073ms:MSCommon/vc.py:_check_cl_exists_in_vc_dir#590:found cl.exe!:
And scons env env['ENV']['PATH'] has the cl.exe path but it doesn't use it, So I just updated the session path using scons path like below and it worked
os.environ['PATH'] = env['ENV']['PATH']

Visual Studio Command Prompt

As soon as I open the VS cmd prompt it says "ERROR: Cannot determine the location of the VS Common Tools folder." I'm trying to compile and eventually run my program using a compile.bat and run.bat file.
My program successfully compiled ONCE when I had the following in my compile.bat file:
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
FileMgr.sln /rebuild debug
pause
After it worked, I tried adding the path of vcvarsall.bat in the variable "Path" in Environment Variables, hoping I wouldn't need that initial call line in the .bat file. This didn't work so I deleted the newly added path from "Path" but now I'm getting this error.
This can only go wrong if the registry is messed up on your machine or the VS install didn't go well. The .bat file reads the setting from the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\VS7\10.0 value. Insert Wow6432Node for a 64-bit operating system. Double-check with regedit.exe, the SysInternals' ProcMon utility is always handy to troubleshoot registry problems.
Get your machine stable again, ask questions about that at superuser.com. Try a VS2010 repair to fix the registry first, reinstall next. Shove it out of a 4th story window if all fails.

visual studio 2010 error

When I press Ctrl+F5, I get project out of date debug win32.
Yes:
Would you like to continue and run the last build? (Yes)
\visual studio 2010\project\test123\debug\test123.exe isi not recognized as an internal or external command, operable program or batch file
No:
Would you like to continue and run the last build? (No)
\visual studio 2010\project\test123\debug\test123.exe isi not recognized as an internal or external command, operable program or batch file
If I run code I did on my Visual Studio in class, it works perfectly (files are saved in USB).
It's not a syntax error; I am guessing I'm missing some files but don't know how to fix it.
Open your project properties and make sure that your startup path is correct. VS can't find your exe, so I am assuming that it is not there for whatever reason.
It mean's Visual Studio can't find your exe file, or the exe doesn't exist. Why not create a new project, c/p all the code, and rebuild?