Please somebody, what #define #ifndef #endif does ?
Please somebody, what #define #ifndef #endif does ?
Okay maybe I can shed some light on this. Usually you will see all 3 of those in header files. When you see # followed by something, those are pre-processor directives. Take for example a simple header file
We'll take this one line at a time. The first line is #ifndef MYFILE_H This tells the compiler if this symbol (MYFILE_H) is NOT defined, then procede. The next line is #define MYFILE_H This says define the symbol MYFILE_H so the compiler will not include the file more than once. At this point you can declare any constants or function prototypes, classes, structs, etc. you want. The very last line is #endif this simply just states it is the end of what you wanted to include. Hope this cleared up a little bit of confusion for you. Also, you can use #define in your programs instead of const. Take the following code for exampleCode:#ifndef MYFILE_H #define MYFILE_H // Declarations here const float PI = 3.1415 // Function prototypes perhaps void Foo( void ); #endif
Although this program isn't terribly interesting it does do something. When you use #define you don't put a semicolon after the statement. What this does is actually put 100 wherever the compiler see's MAX_SIZE in your program. It is a direct swap and doesn't take memory like const int MAX_SIZE = 100; would. No type checking is done of #define's so it is generally not as safe. It's up to you which you use, but pretty much everyone in C++ uses const.Code:#include <iostream> using namespace std; #define MAX_SIZE 100 int main( void ) { char Array[ MAX_SIZE ]; return ( 0 ); }
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
Did I understood right MYFILE_h is like MYFILE.h and then you define it. But why you cant just include header with all those definations?This is where I use fn() and I define it in main.hCode:#include "main.h" int main() { fn(); return 0; }
Or I just didnt understand anythink??Code:void fn(); void fn() { //Bla bla }
I'm beginner in programming in C/C++, and also I'm not god in English..
I think it's not a good idea to use real code in headers. In my opinion headers are for declarations classes, variables, constants.. and other things which names I don't know in English
But I wanted to create my own library with some useful functions(gcd, testing primes, ..) and also something for mouse. I thought it's not very difficult, so I had read some original headers(for example stdio.h). But I have found out that there is no code here, only some __some_function_or_variable_name and another unreadable constructions so the code must be somewhere else.
So if someone know how to write my own library (and how to use it with my compiler - I have BC++ 3.1 and not-running DJGPP, I don't have enough RAM, only 8MB please write it..
But after reading some messages on this board I think it's not possible..
and if you find some mistakes in my English (and you will find certainly please correct them..
No, I use headers for function declarations and stuff like that...
And in source files you can use functions that you have declared in header file. That is like you include time.h and use it's functions include you'r own header and use it's functions.
And what did you mean with real code
You can, but #defining a unique name for each header you can make sure your compiler doesn't load things twice, which could happen if header 1 loads header 2 and you forgot about it.Originally posted by Fake
Did I understood right MYFILE_h is like MYFILE.h and then you define it. But why you cant just include header with all those definations?
Also, check this out:
Header1:
Header2:Code:#ifndef header1isnowloaded #define header1isnowloaded struct blabla {somestuff in struct}; #endif
That's how you could use defines. If you now load Header2 without loading Header1 first, you still get the functions and declared stuff outside of the #ifdef block.Code:#ifdef header1isnowloaded extern struct blabla Blablastruct1, Blablastruct2; void SomeFunkyFunction(struct blabla *blabla_input); #endif void SomeFunctionThatDoesntRequireABlablaStruct(int a);
(Okay, that example was a bit weird...)
Last edited by Boksha; 05-31-2002 at 09:39 AM.
Ok now I got it. Thanks a lot !!!!
What I mean by 'real code'?
Let's see:
// this is file someprog.h
class SomeC {
public:
int x;
void set(int setvalue);
}
// file: someprog.cpp
int main(void)
{
SomeC someclass;
someclass.set(1);
return 0;
}
And this won't work, because function SomeC::set() has a prototype(in header someprog.h), but its body isn't defined, and I meaned this missing code as real code, for example
void SomeC::set(int setvalue)
{
x = c;
}
If you see some original header (e.g. stdio.h), there is no real code, only declarations; and where is the 'real code' ?
Well, these functions from header(printf, ..) are running good, so somewhere must be the code.. I think it's in some *.lib or *.obj files, but how can I create them?
If I understand your question, you create a header file and put your class and function declarations in it. For example we'll say 'myfirstheader.h'. Then you create a source file where you define all of your function and class declarations and name it 'myfirstheader.cpp'. Then in your program when you use the preprocessor directive #define "myfirstheader.h" the compiler will group that with 'myfirstheader.cpp' when it compiles your program. Now as common practice header files are usually shipped uncompiled to give the user access to the protoypes and their associated source files are shipped compiled for space saving reasons, as well as to prevent alterations.