Building Program Execuables from the Command Line
The Visual Studio IDE is very convenient to use, and even helpful, when you are working on the development of a program over time, such as you would be doing for a program required for a weekly submission, for example. However, sometimes you just have a short program for which you want to build the executable and run it to perform a check or a test of come kind. If that's the case, then it's a bit of overkill to have to fire up Visual Studio and put the program into a project just to be able to build and run it. Fortunately, there's a simpler way.
For a simple program like hello.cpp
that just outputs a
greeting, you can compile, link and build the executable directly from
the command line with the following command:
> cl /EHsc hello.cpp
We will not explain the /EHsc
qualifier here ... suffice
it say that you need to have it, and it is case-sensitive.
We are assuming here that you have right-clicked on a folder to open a
Visual Studio command window on that folder (even though you are not
using the Visual Studio IDE itself), and that hello.cpp
is
in that folder.
The above command produces two files: hello.obj
and
hello.exe
. Of course, once you have hello.exe
.
you no longer need hello.obj
.
If, on the other hand, the program in hello.cpp
uses
something from the Scobey
namespace in the
utilities
package, you can use the same
utilities.h
file, but you need an alternate version of the
utilities.obj
file. This alternate version is called
utilities_cl.obj
and is available from the Q: drive.
This version appears to word for command-line compiling on the SMU
desktop, but it may not word elsewhere becuse of version incompatibilities..
The command to use in this case is this one:
> cl /EHsc hello.cpp utilities_cl.obj
This will give you both the hello.exe
which incorporates
whatever part of the utilities
package you included, as well
as hello.obj
, which may be discarded as before.
In order to get a command window that is "Visual C++ aware", you may have to run the following batch file:
C:\Program Files(x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat