T Nation

Programming Help


#1

Ok so I've tried google, I've tried asking my uber geek programming friends, I've downloaded online books, I've looked at examples and I still can't find a solid answer. So hopefully someone here will be able to help me.

I want to know how to create my own header files in C++, but I don't just want to know HOW to do it, I want to know why I do what I do so for example in my searches I've seen reference to #ifndef, #define, and #endif statements that are apparently quite important, but I don't know exactly what they encapsulate and why they're there. I also seem to be running into problems when referencing my own header files in my main .cpp file, it complains the header file doesn't exist in the directory or something even though I've added it to the project as a new header file (I'm using Visual C++ express edition). So do they have to be in the same folder as all the other standard header files or what?

tl;dr - What is general form of C++ header files with explanation of meaning of #ifndef, #define etc, and where does the header file need to be located for use.


#2

It’s a bit of a tedious process at first; you need to create (IIRC) 3 files in total for one header file. There’s the header file that you refer to in your main.c file. The header file refers to a .c/.cpp file that has your #defines and function headers in it (interface). The interface file refers to another file which contains all the function definitions you write (implementation).

That’s it in a nutshell.

edit: typo


#3

[quote]JLu wrote:
Ok so I’ve tried google, I’ve tried asking my uber geek programming friends, I’ve downloaded online books, I’ve looked at examples and I still can’t find a solid answer. So hopefully someone here will be able to help me.

I want to know how to create my own header files in C++, but I don’t just want to know HOW to do it, I want to know why I do what I do so for example in my searches I’ve seen reference to #ifndef, #define, and #endif statements that are apparently quite important, but I don’t know exactly what they encapsulate and why they’re there. I also seem to be running into problems when referencing my own header files in my main .cpp file, it complains the header file doesn’t exist in the directory or something even though I’ve added it to the project as a new header file (I’m using Visual C++ express edition). So do they have to be in the same folder as all the other standard header files or what?

tl;dr - What is general form of C++ header files with explanation of meaning of #ifndef, #define etc, and where does the header file need to be located for use.[/quote]

It doesn’t work to just create a new “myheader.h” file under the headers section, then just “#include myheader.h” in the main.cpp?

EDIT: That’s how I do it. I also have precompiled headers turned off though… Alt+f7 -> Configuration properties -> c/c++ -> Precompiled Headers -> Create/use precompiled header -> Not using precompiled headers


#4

i wished i knew what you guys are talking about, sounds cool


#5

It’s not cool in any way whatsoever.


#6

Take a look at this book - http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html - and see if it helps (I’m a C#/VB.NET developer so I’m not gonna be much more help :slight_smile: )


#7

[quote]Payaso wrote:
It’s a bit of a tedious process at first; you need to create (IIRC) 3 files in total for one header file. There’s the header file that you refer to in your main.c file. The header file refers to a .c/.cpp file that has your #defines and function headers in it (interface). The interface file refers to another file which contains all the function definitions you write (implementation).

That’s it in a nutshell.

edit: typo[/quote]

What the hell are you talking about?

OP: A C header file is, really, just a plain old C file. It contains valid C code. Typically a header contains declaration while a source file contains definitions. But it absolutely does not have to be this way. But the separation of declarations and definitions is good C programming practice: it provides for type checking on function calls in pre compiled libraries, among other things.

When you “compile” a program in a typical IDE or perhaps using GCC, three major steps occur: the preprocessor is run, the compiler is run, and then the linker is run.

#include is a preprocessor directive; it’s an instruction for the preprocessor. #ifdef and #ifndef are also preprocessor directives, which are if statements to test if a symbol (variable, function, macro) is defined or not (it is a relatively “advanced” feature). In fact any symbol that begins with a # in a C source file is a preprocessor directive. What include does is “cut and paste” the target file into the current file.

Here’s an example:

File foo.h

File foo.c

After the preprocessor runs on foo.c, the file foo.c will look like this:


#8

Oh, and if you’re really trying to learn a programming language (and the associated tools), you really need to find a good solid book (or books) on it.

Programming is a huge fascinating subject. I love it. Have fun! :slight_smile:


#9

[quote]JLu wrote:
So do they have to be in the same folder as all the other standard header files or what?
[/quote]

I can’t give you a specific answer for visual C++, HOWEVER, if you do this:

#include <myfile.h>

Then yes, myfile.h must be in the folder where all the other standard headers are. That’s where the preprocessor will look for it. (Don’t do this)

If you do this:

#include “myfile.h”

Then myfile.h must be in the folder where your source file is. That’s where the preprocessor will look for it. (Do this.)


#10

hmmmm


#11

Thanks for the responses guys. Linnrose, I actually downloaded that e-book and it was pretty useless lol. Goochadamg, I’ll try to put to use some of the info you’ve given me, I think I have an idea of how to do it now.


#12

[quote]JLu wrote:
for example in my searches I’ve seen reference to #ifndef, #define, and #endif statements that are apparently quite important, [/quote]

It’s called a file guard. Any time you create a header file, you must wrap the contents of the file with

#ifndef MACRONAME
#define MACRONAME

#endif

MACRONAME is usually the name of the header file itself, e.g. MYHEADER_H, and it must be unique to that file.

The purpose of the file guard is to prevent redefinition from multiple includes of the same code. For example: if C.cpp #includes A.h and B.h, but B.h also #includes A.h, the code will not compile without the file guard, because everything in A.h will be included twice, and C compilers do not look kindly on redefinitions.

What the file guard is actually doing, at the preprocessor step, is asking “does this macro not exist?” (#ifndef) if it does not, define it (#define) and leave the code before the #endif alone. If the macro does exist when the #ifndef is hit, the code between it and the #endif will be removed, because it has already been included once before.


#13

[quote]tom8658 wrote:
JLu wrote:
for example in my searches I’ve seen reference to #ifndef, #define, and #endif statements that are apparently quite important,

It’s called a file guard. Any time you create a header file, you must wrap the contents of the file with

#ifndef MACRONAME
#define MACRONAME

#endif

MACRONAME is usually the name of the header file itself, e.g. MYHEADER_H, and it must be unique to that file.

The purpose of the file guard is to prevent redefinition from multiple includes of the same code. For example: if C.cpp #includes A.h and B.h, but B.h also #includes A.h, the code will not compile without the file guard, because everything in A.h will be included twice, and C compilers do not look kindly on redefinitions.

What the file guard is actually doing, at the preprocessor step, is asking “does this macro not exist?” (#ifndef) if it does not, define it (#define) and leave the code before the #endif alone. If the macro does exist when the #ifndef is hit, the code between it and the #endif will be removed, because it has already been included once before.[/quote]

Ok thanks that makes a lot of sense, I got it working with the file guard but I wasn’t sure what they were for but this clears it up perfectly.


#14

Erm, C++ sucks, Java all the way.

If your gonna use C++ atleast use Eclipse :slight_smile: lulz, I hate Visual Studios haha

anyway, looks like you got what you wanted, and, my experience in programming is most likely inferior to these other guys too.