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. Damnit!
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;
}
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!
Let's hear when there are questiones or troubles occure and have some talk.
cheers, Flocke