Microsoft Visual Studio is an environment which allows you to create, edit, save, debug, compile, and run C++ programs.
Projects Every C++ program is contained in its own project directory. Whenever you start a new homework assignment or any C++ program, you must create a project within Visual Studio first and add C++ source code files (text files with extension .cpp) to that project. In this guide, we will show you how.
Solutions A solution is a file that groups related C++ projects together in order to organize them. In this class we will likely create one solution file that contains all of our PIC10A projects.
To create your first project and solution for your homework, follow these steps:
Open Microsoft Visual Studio by clicking the purple infinity icon . We will be using Microsoft Visual Studio 2015 in this class. (any version is fine. In particular, I recommend the Community version for your home computer. It's free.)
If this is the first time Visual Studio is opened on your computer, you should see a pop-up similar to the below image. Uncheck the checkbox. Select "Visual C++ Development Settings." Then press "Start Visual Studio."
If you don't see this pop-up, don't worry about it. Just go straight to step 2.
Select "File," then "New Project."
Under the "Installed" and "Templates" headings, click "Visual C++" and then "Win32 Console Application" in the middle frame. Type in the name of your project "HelloWorld" in the text field labeled "Name". Be sure to create the project on your personal "Z:" network drive by entering the location "Z:\Documents\Visual Studio 2015\Projects" in the text field labeled "Location." Then click "OK."
The dialog below will appear. Click "Application Settings" and then select "Console Application," uncheck "Precompiled Header" and "SDL checks," and then check "Empty Project." Click the "Finish" button.
Your solution "HelloWorld" and Project "HelloWorld" are displayed in the Solution Explorer. Right-click "Source files," select "Add", then "New Item" to create source code.
Under Visual C++, select "Code." Then select "C++" File in the middle frame. Enter the name of your C++ source code file "HelloWorld.cpp" and press "Add." This will create the source code file (a plain text file) inside your HelloWorld Project folder and integrate it into your program.
Type this C++ code into the source editor and press CTRL-S to save.
After editing the source code file, select the "Build" menu and then "Build Solution" to compile your project. The compiler will translate your source code into a binary executable (.exe) file written in your machine's native language. The file "HelloWorld.exe" will be created as a result.
You will see the message "Build: 1 Succeeded" in the output window.
To run program, select the Debug menu, followed by "Start without debugging."
The output console will pop up and display the message "Hello World!"
(If you followed my instructions exactly, your final output won't have a space between "Hello World!" and "Press any key to continue...". I made a typo when setting this up, and now I'm too lazy to re-make the images)
Congratulations! You just completed your first C++ program! If you want to find the files associated to your project, look in the project folder "Z:\Documents\Visual Studio 2015\Projects"
- My program won't build (Step 8 gives an error message)
- This can be caused by a mistake during Step 4. Make sure that you create an empty project. To see if your project is empty, check your Source Files and Header Files in the solution explorer. The only thing there should be the HelloWorld.cpp that you created yourself.
- My program runs, but it outputs a gross error message before it prints "Hello World!". The message looks something like "'\\mrburns\YourBruinIDHere\...".
- This usually happens because of a mistake in Step 3. There's a complicated and annoying bug with how Visual Studio deals with Project Locations. I don't completely understand the problem, but (a) if you follow the instructions exactly the problem should disappear, and (b) this issue doesn't actually stop your code from running. It shows the error message, but then executes your code normally.