C++ Lesson1: Preparations

You can talk about anything. (please read forum rules before posting)

Moderator: thunderchero

User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3197
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

C++ Lesson1: Preparations

Post by Flocke »

C++ Lesson1: Preparations for the C++ Programming Course

Previous: C++ Course, Signup!
Follower: C++ Lesson2: Introduction

=======================================
the course

First some words on the now starting C++ course itself:
As already told in the sign-up-thread, everyone interested in BotF is welcome to participate! There's no prerequirement but some very basic computer knowledge.
I'll try to split the whole topic into several lessons so it's a bit more sorted and more portioned.
I plan on a cycle of one to two weeks per lesson, but will adapt on need.
Each lesson I will begin with a little introduction, then we talk and do some practice.
What lessons we'll do is mostly up to you, but for the beginning we've to accomplish some basics to get you started.

Cause programming is best learned by practice, we'll immediately start with setting up a programming environment and code a first simple "Hello World"-program this lesson.
Developing a "Hello World"-program is a typical first step when learning a new language and simply prints a little message to the screen, really easy. :)
But before we do so, I wanna give you a little overview on the tools and compilers to use, so let's start!

=======================================
choosing compiler & IDE, decisions, decisions

When programming the most important thing you need is a compiler. I'm sure most have heard of, that is a little program to convert your source code into asm/machine code.
For each operating system you have own compilers, or compiler distributions. That is cause each operation system offers different library calls to interact with. So the very first important decision is what operating system to target.
I say Windows, hope you're ok with that. If you prefer anotherone, please tell so I can take attention.
The language itself is standardized, so for learning C++ it doesn't matter.
Porting issues come with using operation system specific library calls, but there are lots of cross-platform libraries available that can be used.
With these libraries and some caution, a program can be developed for multiple platforms and compiled with any C++ compiler.

QuasarDonkey does program on linux and uses gnu gcc as the compiler. Gcc was invented for unix/linux development, but there are two main approaches to get it work with Windows, too. As I always aimed for cross-platform development I decided to go with gcc early, and also used it for mpr++, so we'll use gcc, simple decision.
However, on windows, most people use Visual Studio from Microsoft which comes with it's own compiler, just to let you know.

As QuasarDonkey mentioned he uses "Cygwin" to get his code work with windows. We won't use that. What cygwin does is emulate a linux environment on top of windows to get linux code run on windows, not that a smart approach in my opinion.
Instead we go with MinGW, which adapts the compiler and all library code to windows to generate native code for windows.
But as I already told, some caution and the right libraries and it doesn't matter much.

So we have the compiler, and with it already come alot of little tools like a debugger we can use.
With that and a simple text editor we already have all we need, but I know you, you're no cracks that love to work with consoles. You prefer a bit of comfort so we'll go with an IDE (Integrated Development Environment).
Visual Studio is an IDE. It is very feature rich, e.g. allows for auto-completion of what you type or refactoring code (renaming variables for all occurences) and such, but for a novice, it's also bloated and even more important to us, it doesn't work well with gcc but comes with it's own compiler.
A very good alternative I got used to is Code::Blocks, which can be used on linux too btw. Let me decide again, we'll all use Code::Blocks, that makes things easier.
For people that learned to use Java, Eclipse can be used with gcc as well. But I absolutely don't recommend it. Eclipse is slow, even more bloated than Visual Studio, and designed for Java coding, not C++.

=======================================
setting up GCC MinGW + Code::Blocks

So now let's download, install and configure GCC MinGW and Code::Blocks and then implement a little "Hello World" program.

For MinGW best use the latest installer MinGW-Get from: http://sourceforge.net/projects/mingw/f ... -get-inst/ and install to C:\MinGW or C:\MinGW\Programme or somewhere. They say you should not have white spaces in between, however I didn't notice any issues in "Program Files" folder either.
On install select c (gcc) as well as c++ (g++) compiler but disregard other languages and MSYS. All important is contained with the c and c++ compilers.
In the improbable case someone notices, the MinGW GCC compiler version is a bit behind time. I'm eagerly awaiting version 4.70 QuasarDonkey already has, and he doesn't even make use of the cool new C++11 features of e.g. template aliases and in header file member-initialization. :lol: Damnit! :x

For Code::Blocks you could go here: http://www.codeblocks.org/downloads and use a binary install even including MinGW out of the box, but that's outdated and just for lamerz. Instead download the latest 'Nightly'-build from here: http://forums.codeblocks.org/index.php/board,20.0.html. These builds normally work pretty well with no crashes but are more up to date.
The text and "how it works" you can simply skip, just download the CB_ *** _win32.7z, and extract it where you want, no need to install, just copy over. Also download the wxWidget dll from same location and copy over. Further you need mingw10.dll, but that should be included with previous MinGW install so you don't have to download it seperate, just copy over from MinGW install if needed.

When that's all done and you first start Code::Blocks, it'll ask for a compiler to use. It should automaticly detect the already installed GCC MinGW distribution, and offer that to use. However to check or configure manually you can do so in Settings->Compiler and debugger->Toolchain executables. Entries should be:
C compiler: mingw32-gcc.exe
C++ compiler: mingw32-g++.exe
Linker for dynamic libs: mingw32-g++.exe
Linker for static libs: ar.exe
Debugger: gdb.exe
Resource compiler: windres.exe
Make program: mingw32-make.exe
Maybe someone likes to add a screenshot. :)

=======================================
Hello World

Now to see that all works right, start a new Project, select Console application.
Give it a name and select a place to store, the rest leave as is.
When done it should already generate the following code for you:

Code: Select all

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
Compile and run from inside Code::Blocks, that's it. I'm sure you'll find where to click, if not others will help. :P
If you run your application from outside, the console immediately closes. To prevent this, you can either call the program from inside an already opened console or add #include <cstdlib> to the top, and system("PAUSE"); before of the return statement.


Congratulation, you just finished your first C++ application! Enjoy! :D
Let's hear when there are questiones or troubles occure and have some talk.

cheers, Flocke
Last edited by Flocke on Thu Jul 05, 2012 11:14 am, edited 6 times in total.
Dr_Breen
Commodore
Commodore
Posts: 889
Joined: Wed Apr 30, 2008 2:00 am
Location: Zurich, Switzerland
Contact:

Re: C++ Lesson1: Introduction

Post by Dr_Breen »

"using namespace std;"

i know namespaces from using xsl in web applications. but i am not familiar with it on c++.

does the above statement mean that we can use "cout" instead of "std.cout"? at least this is what it would mean in xsl.
whats the best ressource to look up the std class and other classes (are they called classes in c++?) ?

i know i'm probably a bit ahead but all i need is a starting kick in the butt to get into it properly.
Public BotF / EF2 Teamspeak 3 Server: 83.169.13.55
User avatar
Lathon
Lieutenant-Commander
Lieutenant-Commander
Posts: 116
Joined: Thu Oct 15, 2009 2:00 am

Re: C++ Lesson1: Introduction

Post by Lathon »

Dr_Breen wrote:"using namespace std;"

i know namespaces from using xsl in web applications. but i am not familiar with it on c++.

does the above statement mean that we can use "cout" instead of "std.cout"? at least this is what it would mean in xsl.
whats the best ressource to look up the std class and other classes (are they called classes in c++?) ?

i know i'm probably a bit ahead but all i need is a starting kick in the butt to get into it properly.
Just to answer the using part, that is correct. instead of always having to write "std::cout, std::cin", etc, you can use the "using namespace std".
Dr_Breen
Commodore
Commodore
Posts: 889
Joined: Wed Apr 30, 2008 2:00 am
Location: Zurich, Switzerland
Contact:

Re: C++ Lesson1: Introduction

Post by Dr_Breen »

uh oh. now i have a new question about the "::"

i tought in c++ you call functions with a "." like classname.functionname ?
Public BotF / EF2 Teamspeak 3 Server: 83.169.13.55
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3197
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: C++ Lesson1: Introduction

Post by Flocke »

Dr_Breen wrote:i know i'm probably a bit ahead but all i need is a starting kick in the butt to get into it properly.
correct, you are a bit ahead :D
all these things we gonna talk on later, when all participators have a running environment.
But to add to what Lathon said, a namespace is not a class, there is no class "std". A namespace simply does what is says, it's an encasement of names, like class names, and it is used in larger projects or for larger libraries to encapsulate identifiers to avoid naming conflicts.

Like in Java the dot '.' is used to call methods or access member variables of an initialized instance. The '::' is used to access static members and encapsulated type definitions, but that better fits next lesson.

As for the resources, the most useful I frequently use are http://www.cplusplus.com/ and for C++11 http://en.cppreference.com/w/. ;)
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3197
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: C++ Lesson1: Preparations

Post by Flocke »

Hey people, it's weekend, you should have time to install a little program! :)
Dr_Breen
Commodore
Commodore
Posts: 889
Joined: Wed Apr 30, 2008 2:00 am
Location: Zurich, Switzerland
Contact:

Re: C++ Lesson1: Preparations

Post by Dr_Breen »

works like a charm.
Public BotF / EF2 Teamspeak 3 Server: 83.169.13.55
User avatar
anjel
Past Administrator
Past Administrator
Posts: 666
Joined: Thu May 08, 2008 2:00 am
Location: Bs As - Argentina

Re: C++ Lesson1: Preparations

Post by anjel »

well, been trying for a while, without success, to just launch codeblock, and all what i`ve got it´s these message:


I´ve got windows 7 64bit
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3197
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: C++ Lesson1: Preparations

Post by Flocke »

anjel wrote:well, been trying for a while, without success, to just launch codeblock, and all what i`ve got it´s these message:



I´ve got windows 7 64bit
Flocke wrote:The text and "how it works" you can simply skip, just download the CB_ *** _win32.7z and extract it where you want, no need to install, just copy over. Eventually you also need the wxWidget dll. mingw10.dll should be included with previous MinGW install.
have a look at where you downloaded the code::blocks nightly build, there's also a link to download your missing dll, e.g. "wxmsw28u_gcc_cb_wx2812_gcc452-TDM.7z"
btw with > "how it works" you can simply skip < I didn't mean you're forbidden to take a look when having troubles ;)

thereby I notice it's all 7z archive files so to those who've trouble extracting them, get 7-Zip from http://www.7-zip.org/

Such troubles is what this lesson is meant for, probably the others just hide and wait with same problem for not having to embarrass themselfes. :P *jokingly*
Last edited by Flocke on Mon May 28, 2012 9:19 am, edited 1 time in total.
Dr_Breen
Commodore
Commodore
Posts: 889
Joined: Wed Apr 30, 2008 2:00 am
Location: Zurich, Switzerland
Contact:

Re: C++ Lesson1: Preparations

Post by Dr_Breen »

winrar can extract 7z as well.

i have to admit i'm not so happy with code::blocks. i guess i'm to used to eclipse. perhaps i'm one of these lamerz who like bloated ide's
Public BotF / EF2 Teamspeak 3 Server: 83.169.13.55
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3197
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: C++ Lesson1: Preparations

Post by Flocke »

Use eclipse if you want, but I can't give much help on it cause I didn't ever use it for C++ myself.
Or try free Visual Studio C++ Express from MS, that I can help on but might complicate with the different compiler.
What exactly you're missing from Code::Blocks?
User avatar
anjel
Past Administrator
Past Administrator
Posts: 666
Joined: Thu May 08, 2008 2:00 am
Location: Bs As - Argentina

Re: C++ Lesson1: Preparations

Post by anjel »

well.. i won´t say a word... lol

here you go:

Image

is it ok ?


and here we are now, ROCK ON !!!

Image
User avatar
Flocke
BORG Trouble Maker
BORG Trouble Maker
Posts: 3197
Joined: Sun Apr 27, 2008 2:00 am
Location: Hamburg, Germany
Contact:

Re: C++ Lesson1: Preparations

Post by Flocke »

edit: you admins cheat and hide your edit :lol:
well done anjel! :)
User avatar
thunderchero
Site Administrator aka Fleet Admiral
Site  Administrator aka Fleet Admiral
Posts: 7852
Joined: Fri Apr 25, 2008 2:00 am
Location: On a three month training mission, in command of the USS Valiant.

Re: C++ Lesson1: Preparations

Post by thunderchero »

ok I had some time to play today here is my screen :D

Image

I was able to add the pause after a few attempts and reading a little closer to instructions :wink:

thunderchero
User avatar
anjel
Past Administrator
Past Administrator
Posts: 666
Joined: Thu May 08, 2008 2:00 am
Location: Bs As - Argentina

Re: C++ Lesson1: Preparations

Post by anjel »

Interesting, post the code so i can givea try :). I've thought the same but didn't know how to do it
Post Reply

Return to “General Chat”