Creating and building your first C++ project in Visual Studio

In order to compile and run code from Visual Studio, it must be done from within a project.

Getting ready

In this recipe, we will identify how to create an actual executable running program from Visual Studio. We will do so by creating a project in Visual Studio to host, organize, and compile the code.

How to do it...

In Visual Studio, each group of code is contained within something called a Project. A Project is a buildable conglomerate of code and assets that produce either an executable (.exe runnable) or a library (.lib, or .dll). A group of Projects can be collected together into something called a Solution. Let's start by constructing a Visual Studio Solution and Project for a console application, followed by constructing a UE4 sample Project and Solution.

  1. Open Visual Studio, and go to File | New | Project...
  2. You will see a dialog as follows:
    How to do it...

    Select Win32 in the pane on the left-hand side. In the right-hand pane, hit Win32 Console Application. Name your project in the lower box, then hit OK.

  3. In the next dialog box, we specify the properties of our console application. Read the first dialog box and simply click Next. Then, in the Application Settings dialog, choose the Console Application bullet, then under Additional options, choose Empty project. You can leave Security Development Lifecycle (SDL) checks unchecked.
    How to do it...
  4. Once the application wizard completes, you will have created your first project. Both a Solution and a Project are created. To see these, you need Solution Explorer. To ensure that Solution Explorer is showing, go to View | Solution Explorer (or press Ctrl + Alt + L). Solution Explorer is a window that usually appears docked on the left-hand side or right-hand side of the main editor window as shown in the following screenshot:
    How to do it...

    Solution Explorer also displays all the files that are part of the project. Using Solution Explorer, we will also add a code file into the editor. Right click on your Project FirstProject, and select Add | New Item…

    How to do it...
  5. In the next dialog, simply select C++ File (.cpp), and give the file any name you'd like. I called mine Main.cpp.
    How to do it...
  6. Once you have added the file, it will appear in Solution Explorer under your FirstProject's source file filter. As your Project grows, more and more files are going to be added to your project. You can compile and run your first C++ program using the following text:
    #include<stdio.h>
    
    int main()
    {
      puts("Welcome to Visual Studio 2015 Community Edition!");
    }
  7. Press Ctrl + Shift + B to build the project, then Ctrl + F5 to run the project.
  8. Your executable will be created, and you will see a small black window with the results of your program's run:
    How to do it...

How it works...

Building an executable involves translating your C++ code from text language to a binary file. Running the file runs your game program, which is just the code text that occurs in the main() function between { and }.

There's more...

Build configurations are styles of build that we should discuss them here. There are at least two important build configurations you should know about: Debug and Release. The Build configuration selected is at the top of the editor, just below the toolbar in the default position.

There's more...

Depending on which configuration you select, different compiler options are used. A Debug configuration typically includes extensive debug information in the build as well as turning off optimizations to speed up compilation. Release builds are often optimized (either for size or for speed), take a bit longer to build, and result in smaller or faster executables. Behavior stepping through with the debugger is often better in the Debug mode than the Release mode.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.108.18