views:

129

answers:

4

I have recently started learning C++ and coming from a Ruby environment I have found it very hard to structure a project in a way that it still compiles correctly, I have been using Code::Blocks which is brilliant but a downside is that when I add a new header file or c++ source file, it will generate some code and even though it is only a mere 3 or 4 lines, I do not know what these lines do. First of all I would like to ask this question:

What do these lines do?

#ifndef TEXTGAME_H_INCLUDED
#define TEXTGAME_H_INCLUDED

#endif // TEXTGAME_H_INCLUDED

My second question is, do I need to #include both the .h file and the .cpp file, and in which order.

My third question is where can I find the GNU GCC Compiler that, I beleive, was packaged with Code::Blocks and how do I use it without Code::Blocks? I would rather develop in a notepad++ sort of way because that is what I'm used to in Ruby but since C++ is compiled, you may think differently (please give advice and views on that as well)

Thanks in advance, ell.

EDIT: I'm on Windows XP & thanks for the lighting fast replies!

+2  A: 

That's an inclusion guard, to prevent a .h file from being included twice. Besides saving time, this is often in fact required to avoid defining things twice.

You should include only the .h. The .c file will be linked to your program in some form. For small programs, you can just pass all the .c files to gcc, but larger programs will involve intermediate .o files or even libraries (static or dynamic).

You can definitely work without an IDE. There are many ways to install the gcc compiler on Windows, including Cygwin and MinGW. I think you are correct that Code::Blocks comes with a gcc executable, but I don't know where it is or what version.

Matthew Flaschen
it comes with mingw 5.1.6 which includes gcc 3.4.5 which is rather outdated, i for my self use TDM's GCC, which is much more recent, the new codeblocks version which will be released some day maybe, will include a more up to date version aswell.
smerlin
+1  A: 

To answer your questions:

  • The lines are include guards. They prevent the header file being included more than once in any given translation unit. If it was included multiple times, you would probably get multiple definition errors.

  • Header files are #included in .cpp files and in other headers. .cpp files are not normally #included.

  • The C++ compiler that comes with Code::Blocks is called MinGW GCC, and can be found in the bin directory of the MinGW installation. To find it, do a Windows search via explorer for 'g++'. To use it, you will need to put the directory it is in on your search path. Note the version of the compiler that ships with Code::Blocks is quite old - you can get a much more recent version from here.

anon
+1  A: 

Those lines make it so that if a file is #included twice, everything will continue to work. That in turn lets you treat header-file dependencies as a simple directed graph, which is definitely easiest.

You don't #include .cpp files. (Well, not unless you're an evil programmer. Don't do it!)

I'll let others (or google!) tell you about gcc, but it might help if you were to describe what platform you're using.

Donal Fellows
A: 

All of your questions have been answered by others, except this:

I would rather develop in a notepad++ sort of way because that is what I'm used to in Ruby but since C++ is compiled, you may think differently (please give advice and views on that as well)

I think this is a very bad idea. A fully fledged IDE with an integrated debugger, jump to symbol definitions, refactoring capabilities, a profiler, intellisense and more is practically a must for any real world project.

And the absolute best is Visual Studio* with Visual Assist X**. Code::Blocks pales in comparison ;)


* If you study in a university you can usually get it for free through MSDNAA; otherwise there the Visual Studio Express edition whicih is free
** 30 days evaluation period

Andreas Bonini
Not to mention Vi$ual Cash from PaymySoft (ps. I dind't downvote).
clyfe
But don't you think that it is better if you write all code yourself, rather than some being automatically generated?
Ell
With Visual Keyboard Xtreme plugin that allows you to edit your files using your keyboard :>
clyfe
Given that Ell is using Code::Blocks and is new to C++, I would recommend the free Visual C++ Express rather than suggesting a $800 software package which is way overkill for learning the language. It may be the best out there but it's not a helpful comment in this context IMO.
Tom Savage
@Tom: I didn't suggest a particular version of VS; Visual Assist X has a 40 days evaluation period, so he can follow my advice without spending a cent. @Ell: no code is "automatically generated"; you are given hints that you can follow. It saves time.
Andreas Bonini
@Andreas It should be noted though that Visual Assist is not compatible with express editions of visual studio and while I can see that it may be useful (especially on larger projects), it is not at all necessary for a beginner.
Tom Savage