-
Notifications
You must be signed in to change notification settings - Fork 0
0.4.Workflow
First off, create a folder somewhere to store your code. I advise creating subfolders for each section.
There's a really easy way of opening the command prompt with a specific working directory. Navigate to the desired folder with Explorer. Then, click on the folder breadcrumbs at the top, as shown in this image. It should highlight the full path to the file, simply type cmd
and press enter.
Visual Studio Code can also open a built-in Command Prompt, which is incredibly useful since it defaults to the folder we're working in.
Run VS Code, then go File > Open Folder
. Select the folder that will contain your code files, and click Select Folder. VS Code should now show the contents of your folder in the Explorer sidebar on the left.
Press the keyboard shortcut Ctrl + `
(the key to the left of 1
). This should open up a dock on the bottom. By default, VS Code uses Powershell for its built-in terminal. We need to change that to the regular command prompt.
Powershell is plenty powerful, and if you know how to use it, you should. But I don't know how to use it, and we don't need any of its more advanced features right now.
On the dock, you should see a drop-down box near the right that currently says 1: powershell
. Click it, then select the Select Default Shell option. Another drop down will appear at the top of the screen, click on the entry that says Command Prompt. Once you've done that, restart VS Code and your built in terminal should be CMD.
The VS Code terminal starts off blank. It's a weird oddity of the software. Just click anywhere inside it and press Enter, and it should spring to life.
Now we can finally write some code. In the Explorer sidebar on the left, right click and select New File. You can also press the New File icon next to the folder name (it only appears when you move your mouse over the area).
Name the file hello.cpp
. The editor should automatically open it in a tab. Now, type the following. We'll learn what it means in the actual tutorial.
#include <cstdio>
int main() {
printf("Hello World!\n");
return 0;
}
Press Ctrl + S
to save the file. Now press Ctrl + `
again. Your cursor should move down to the terminal. Now enter the following command:
g++ hello.cpp -o hello.exe
If all goes well, you should see no messages appear, merely the prompt again. In the Explorer sidebar, a new file called hello.exe
should have appeared. Now type hello
into your terminal to run your new program. It should display the text "Hello World".
- If you see a bunch of messages that begin with
hello.cpp
and have some numbers after them, double check that you copied the code correctly. You should see no red squiggles underneath any of it. Try copy pasting the code again.- If you get the message
'g++' is not recognized as an internal or external command, operable program or batch file
, Windows cannot find the compiler. Go to the previous page, and follow the instructions to add Mingw-w64 to the system path again.- If you get a short message, and you can see
undefined reference to WinMain
in it, you may have forgotten to save thehello.cpp
file. Save it, and try again. If it still doesn't work, make sure your command prompt's working directory is the same as the one that containshello.cpp
. Try runningdir
to double check.
A handy shortcut of most terminal applications is the ability to quickly run a previous command. Let's say you edit your code and want to compile it again. Instead of manually typing out that entire command, you can simply press the Up arrow key on your keyboard to cycle through all previously entered commands. Eventually, compiling your code will be as easy as pressing Up + Enter
.
Congratulations! You're now ready to actually begin learning to code. You can now follow most of the C++ tutorials on the internet.
What did we just type?
g++ hello.cpp -o hello.exe
-
g++
is the name of our C++ compiler program. It is used to compile C++ code, as opposed togcc
, which compiles C code. - As you may have guessed, the other parts are simply command line options.
-
hello.cpp
tellsg++
the name of the file we want to compile -
-o
is something known as a command line switch, named because it "switches" the functionality of the program. In this case, it tellsg++
that the next command line option is what we want to call the output file. -
hello.exe
is the next option after the-o
switch, sog++
uses it as the output file name.
g++
assumes that any options that aren't preceded with switches are input files. This means that g++ -o hello.exe hello.cpp
would also work, since hello.exe
is after the output switch, leaving hello.cpp
as the input file. However, putting it any other way would not have worked. Putting hello.exe
anywhere other than right after the -o
would make the compiler try and treat it as an input file, which won't work.
We sure did. GCC and G++ are part of the GNU Compiler Collection, a set of compilers developed by the GNU Project. Mingw-w64 is simply these tools ported to Windows.
Contents
Contents
- 1.0 - Section Overview