As you're using using Windows, you can also build with Visual C++ from the command line if you want to. So if you already have Visual Studio installed...
1 - Configuring the Build Environment
Unless you're already using Windows 8, the easiest way to do start is open up a
Visual Studio command prompt. This opens a command prompt which has been configured with the Visual Studio environment (the path to the tools, the include path, the library search path, plus environment variables).
This can be found in the Start Menu in the following location:
All Programs
> Microsoft Visual Studio 2008
> Visual Studio Tools
> Visual Studio 2008 Command Prompt
(same deal for other versions, at least up to 2010)
2 - Building a Single File
Then, using the Visual Studio Command Prompt, all you need to do to compile a single file with the default settings is (where I've assumed you've cd-ed to the right folder):
(the compiler automatically adds /out:hello.exe)
When I use this command line, I get the console o/p
W:\Test\cplusplus\cmd_build>cl hello.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
hello.cpp
c:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C
4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
hello.obj
W:\Test\cplusplus\cmd_build>hello.exe
hello, world
W:\Test\cplusplus\cmd_build> |
To cut down on the o/p you can do the following:
- you can use the /EHsc to tells the compiler to only handle C++ exceptions, as it suggests, to stop the complaint about exception unwind semantics (not needed for .c files, of course)
(Other options tell the compiler to handle no exception or to handle "structured exceptions", which are operating system (Windows) exceptions. But that costs more in terms of performance)
- and you can use /nologo to stop the compiler displaying the version and copyright banner
W:\Test\cplusplus\cmd_build>cl /nologo /EHsc hello.cpp
hello.cpp
W:\Test\cplusplus\cmd_build>hello.exe
hello, world
W:\Test\cplusplus\cmd_build> |
3 - Using a Batch File and Building Multiple Files
I usually create a little batch file to use for my simple little builds (those which don't take long to rebuild).
1 2
|
@echo off
cl /nologo /EHsc hello.cpp
|
then it's a case of
W:\Test\cplusplus\cmd_build>build.bat
hello.cpp
W:\Test\cplusplus\cmd_build>hello.exe
hello, world
W:\Test\cplusplus\cmd_build> |
This becomes more useful when a project uses more than one cpp file.
@echo off
cl /nologo /EHsc /W4 /Fehello_again.exe getmsg.cpp dispmsg.cpp main.cpp |
Here, /Fe is the new (preferred) way of defining the name of the exe file (/out, or /o, is still supported but deprecated). If you don't tell the compiler what name to use, it will use the name of the first cpp file (which means getmsg.exe in this case)
And I've added the /W4 switch to up the warning level to 4 from 1, which is the default warning level at the command line (when you create a new project using Visual Studio, it uses /W3).
(cl /? will get the compiler to list all the switches it understands. Then see MSDN for further details)
W:\Test\cplusplus\cmd_build>build.bat
getmsg.cpp
dispmsg.cpp
main.cpp
Generating Code...
W:\Test\cplusplus\cmd_build>hello_again.exe
hello, world
W:\Test\cplusplus\cmd_build> |
Andy
PS Various notes...
[1] When your projects get complicated enough, and if you still prefer using the command line, you should look at makefiles. In the GCC world, the tool to use is
make
, whereas in the Microsoft world it's
nmake
.
The advantage of a makefile over a batch file is that the make utilties will only rebuild what's changed (based on the file timestamps). This can be a big deal when you're got lots of cpp files.
(There are also a host of other tools (including Maven, Jam, SCons, CMake, and Ant) you might encounter or even use)
[2] You can also configure the Visual Studio environment and by opening a regular command prompt and then running the following in the newly opened command prompt.
"%VS90COMNTOOLS%vsvars32.bat" |
- This environment variable is set by the Visual Studio installer (VS80COMNTOOLS = Visual Studio 2005, VS90COMNTOOLS = Visual Studio 2008, VS100COMNTOOLS = Visual Studio 2010)
- The quotes are needed as there are probably spaces in the path.)
[3] Another (straightforward) programmer's editor is ScITE
http://www.scintilla.org/SciTE.html
I like it for basic editing as it's uncluttered. And it does syntax highlighting, like other programer's editors, which Notepad doesn't know how to do.
Note that programmer's editors, like Programmer's Notepad, Notepad++, and ScITE, can be configured to build using a menu.
[4] More info about building via the Visual Studio command prompt
Building on the Command Line
http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx
Walkthrough: Compiling a C Program
http://msdn.microsoft.com/en-us/library/bb384838.aspx
(note the
Other Versions dropdown on these page's menus)