Advertisement

ifndef, endif, ifdef and all the little creatures in the woods

Started by February 27, 2002 03:21 AM
3 comments, last by shakazed 22 years, 8 months ago
Hello, can someone explain to me what all these preprocessing commands mean? #ifdef #endif #ifndef And one more thing, what is the difference between .dll, .lib and .h files? Just smaller things that bugs my mind. Thanks in advance. sHaKaZeD
The first three are preprocessor macros.

#ifdef FOO checks if there has been a #define FOO command issued earlier (i.e. if FOO has been #defined). #ifndef checks for the opposite. If #ifdef (or #ifndef) evaluates to false then the code that follows it is ignored, up to the #endif statement.

A .h file is a ''header'' file, where you put declarations of functions, classes, (extern) variables that are used in several source (.cpp) files.
A .lib file is a static library file. It contains compiled code that gets added to your own compiled code. They come with .h file which let your compiler know which functions they provide and how to interact with them.
A .dll file (dynamically linked library) is similar, except that its code doesn''t get lumped in with your own code, but lives in its own file (.dll) which can thus be used by several applications without being duplicated. To use one when developping an application, you usually use a stub .lib file which placates your linker (so that it doesn''t complain that it cannot find a function that will be provided ''later'' by the dll), and the corresponding .h file. Or you can try loading the library yourself and access the functions manually, but you don''t need that kind of complications right now, do you ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
#ifdef __HELLO

would mean if somewhere in your code there has been a
#define __HELLO
then the code under the #ifdef would execute until a matching
#endif
was found
#ifndef __HELLO
means the code under #ifndef would run if there HADN''T been a
#define __HELLO
somewhere in your code.

those 3 things are handy to avoid infinite recursion on includes (or at least, i use it lots for that

dll''s are code that can be imported into many programs at once, runnning seperate versions all at once. so 1 program could use a dll and have it''s own state for that code in the dll, and another program could be using the dll and have it''s state for it. (don''t know if that''s clear).

a lib is a collection of code.

a .h is a header file which includes code.

all 3 contain code, they are just used differently. it''s a pretty long explanation to explain the difference ( i think , so i won''t here, there''s some stuff on each in MSDN.

anyway, don''t know if i''ve helped. someone better than me will prolly explain it much better than that
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
Thank you very very much! Hehe guess I´ll wait with that for a while Fruny

sHaKaZeD
As an additional note : code within an #ifdef and an #endif doesn''t even get compiled if the symbol #ifdef checks for hasn''t been #defined : the preprocessor removes the code and the compiler nevers sees it, just like comments. Which is different from code within a C/C++ if block.

Which means that you can have system-dependent (*NIX, Windows, MacOS) code in there. Since every compiler #defines a number of macros identifying the system you are compiling for, that''s the way to write cross-platform code. If you''d put it in an if block, the compiler would scream.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement