Greetings, and
welcome to the second article on Command Prompt, on behalf of Jelly’s blog.
Today I’d like to introduce you to Visual Studios Command Line, and show you
how to build a simple application for a project. First, you will need Visual
Studios 2010 Express, which is free: if you can’t get a version of 2010, then
2008 should suffice. If possible, try right-clicking it and running as
Administrator to avoid permission issues.
First, start a new
project in Visual Studio as a “Windows 32 Command Line” project, and name it
whatever you like, ”Hello” is easy enough. To make it easier on the typing of
the file address type, remember to start the project on the C: drive directly;
you may need admin permission, go ahead and allow it! Then, when the project is
loaded, create a new .cpp (that’s C++)
folder by right clicking the Source
file and selecting, New Folder from the add tab. Now name the file something
simple.
When that is
complete, go ahead and double click the new file and and the following code:
#include <iostream>
int main()
{
std::cout
<< "Hello, this is my first program on
the command line!!!YAY!!!" << std::endl;
return 0;
}
You can also add
any variant of the code you’d like the program to say in-between the Double
Quotes, just remember to add them back in and only those two! That part is critical for the program to function
right, and not to confuse the compiler. The quotations are part of C++, and the
double quotes indicate what is called a string
literal, a string inside of the program itself, instead of just a variable.
From here, save the file and close. Keep the project open for now.
Now to make this
easier, go ahead and find the file location by opening My Computer in the Start
menu, go to the drive your project is on, enter the project and find the source
file you just created. When you do, go ahead click the white bar at the very top
of the window; this will give you your current address in text form to that
source file. Remember this address, or keep this window open for now and refer
back to it.
Now, in your
project, open the Tools tab at the
very top of the Visual Studios window, and select Visual Studio Command Prompt.
When the prompt is open, Type the following:
cd C:\
This is a command
to Change Directory, and it simply moves the current path the command line is
on. In this example, it moves the current path to the C: hard drive. If you
project is on a different drive, then change that letter to whatever letter
your drive is on. From here, enter cd
again, and type in the path to your source file exactly as is, even with spaces
or capitals. If successful, the command prompt will display your path to the
left of your cursor icon.
Now, with this path
in your prompt, enter the following command:
cl filename.cpp
This command stands for Compile
and Linker, also known as cl.exe.
This executable file is the main file in VS C and C++ that builds to programs,
and the command tells that exe to build the source file you provided into an .exe of it’s own. Hopefully there are no
errors, if so, it should give an error number, but I’ll assume for now it
worked as is.
Now, after building, check for the files in the project folder by entering this command:
dir filename* (not
.cpp)
This command will
output the files with that similar name. There should be a .cpp, .obj, and a
.exe file after building with the cl
command. dir stands for Directory, and it outputs the names of
all the files with that file name, or part of it. And the * at the end of the file name suggests any file with that name,
regardless of type.
If you found the dir command outputting the .exe with
your file name, then go ahead and type your file name in the command line with
the .exe at the end to run your
program! Hopefully there will be no errors in this, and your message should be
output into the command line!
Go ahead and try this again if you’d like, and experiment with the cl and dir command to get comfortable. I hope you found this insightful, if not, then at least not too confusing :D. Thank you for your time!
Review:
cl: Command line
argument for Visual Studios C++. Stands for Compiler and Linker, it builds the
applications in C++.
dir: Command in many
command-line OSs that outputs the entire contents of a file if used alone,
including the paths of other files. Can also be used to find specific files with the * character..
cd: This command line argument, common to many command lines, simply changes the current working directory address used by the prompt to the one inputted by the user.
.cpp: C++ file
that holds the source code for use in building applications and obj files.
.obj: The result
of the compilation, this is used in the linker to build the final application
.exe.
Links:
Visual Studios Express for Windows (Free):
http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx
No comments:
Post a Comment