C++ Tutorial #1 – Setting Up & Getting Started with Code::Blocks

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Today I will be showing you how to setup Code::Blocks to write C++ code on Windows. We’re not making any video games today, but I will be showing you the first steps of programming. If you are planning on learning another language, I highly suggest learning C++ as well. If you learn C++, a lot of the other programming languages will be easier to learn. You will have a better understanding on how to assemble your programs.

 

What is Code::Blocks?

 

Code::Blocks is a free IDE that you can use to write C & C++ Programs, and uses MinGW Compiler to transform the code to an executable program. This is my favorite program to use because it’s really easy to install, and it highlights your code nicely. You can customize the colors of you syntax highlighting.

 

Installation

 

  1. Go to their download page, and click on the link labeled “Download the binary release”.
  2. Scroll down to the Windows section and download the file thats labeled “mingw-setup” at the end.
  3. Once the download is complete, run the setup.
  4. On the setup wizard, click “Next”.
  5. “Agree” to the terms.
  6. Choose the features you want to install. (I chose full 250 MB required)
  7. Pick a destination and click “Install”.

 

If you followed the steps correctly you should have Code::Blocks installed.

 

Writing Your First Code

 

1. Open Code::Blocks.

 

codeblocks-firstcode-img1

 

2. Create a New Project.

 

codeblocks-firstcode-img2

 

3. Select Empty Project.

 

codeblocks-firstcode-img3

 

4. Give your project a title.

 

codeblocks-firstcode-img4

 

5. Use GNU GCC Compiler, create “Debug” and “Release” configuration.

 

codeblocks-firstcode-img5

 

6. Add a new file to your project.

 

codeblocks-firstcode-img6

 

7. Select Empty C++ File.

 

codeblocks-firstcode-img7

 

8. Give it a name. I chose “main.cpp”. Add the file to “Debug” and “Release” build targets.

 

codeblocks-firstcode-img8

 

9. Copy and Paste this code:

 

 

10. Press F9 or press build&run the build and run icon.

 

11. If you have followed these steps correctly you should get this:

 

hello-world-complete

 

Congratulations! You’ve made your first program!

 

Let me explain what you just did.

 

 

This section of code is called the header. The ‘I’ and the ‘O’ stands for “Input” and “Output. Iostream is short for input/output stream. Iostream is part of the C Standard Library. A library in programming is a collection of commands that you can use is your code. when you added “#include<iostream>” to your code, that made it so you can use “cout” and “cin”.

 

 

“using namespace std;” means to use the standard library and we place a semicolon at the end to tell our program to end the line. This section of code is not required to make this program, but it makes it a little easier to read. For example, If I remove “using namespace std;”  and I compiled and ran to program. I will get an error, because “cout”, “endl”, and “cin” are part of the standard library. and the compiler doesn’t know that you are using “cout” and “cin” in the standard library. To correct it you can put “using namespace std;”, or you can just use “std::cout”, “std::endl”, and “std::cin” instead.

 

 

This is just another way to write that same code.

 

 

“int main()” is the main function of your code. “Int” stands for integer, and “main” is the name of the function. In an int function, the program looks for a returning number. “Return 0;” Means that the code worked perfectly.

 

 

“Cout” (pronouced C-Out), allows you to output strings and variables. String basically means text. A variable is a name that stands for a value. We will talk about these in another post. After “cout” you will see “<<“. You use these symbols anytime you want to add something to “cout”, and you place words you want to print in quotations afterwards. “Endl” is short for end line. You use this to end the line and start a new one, and then of couse you add a semicolon at the end to end the line in your code. “Cin” (pronounced C-In), It’s a command that makes it so users can input variables in the programs. I’ll explain it a little bit better in future posts. Right now, all you need to know is that “cin.get();” makes the screen pause. Without it, the program will just run for a few milliseconds and close immediately.

 

It sounds like a lot to learn now, but when I write my new tutorials it will make more sense. Practice, and mess around with the program a bit. Have Fun!

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

2 Comments

  1. Great article(or rather, tutorial)!
    Is it possible to make games without using an IDE? Maybe just with a text editor like Atom and a compiler like MinGW. My computer can’t handle IDEs(a pretty old pc without graphics drivers).

    • I suppose it is possible, you can build your projects and not run it, but that would be extremely difficult. I would recommend upgrading or buying a new computer. You need to be able to see your game and test your code for bugs.

      You’ll probably be able to do most of the tutorials. In my C++ tutorials, I don’t have any graphics rendering until tutorial #15, and you can probably skip that and read the other ones.

Leave a Reply

Your email address will not be published. Required fields are marked *