Explanation please

General FreeBASIC programming questions.
Post Reply
Rens
Posts: 256
Joined: Jul 06, 2005 21:09

Explanation please

Post by Rens »

What is the meaning or the use of the following lines in a .bi file

#Ifndef __DirExists_bi__
#define __DirExists_bi__
......
......
......
#endif

I think it is to see if a bi file already is 'defined'.
Never seen it used in code though.

Maybe you can give me a sample of how it can be used.

Thanx in advance.
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Post by Merick »

it's a preprocessor macro. if __DirExists_bi__ has not yet been defined (#define __DirExists_bi__ ) then whatever code it between the #ifndef and #endif statement will be compiled into the exe, on the other hand, if __DirExists_bi__ has already been defined, then the code block will be ignored by the compiler.

The reason for it is because sometimes different headers will have the exact same code segments in them. Using this prevents program bloat by making sure that the code is only compiled into the exe once.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

The # prefix means it is a preprocessor statement.
In this use, it means, suppose some one used this line twice:

#include "DirExists.bas"
#include "DirExists.bas"


That would put all of the code from DirExists into your program twice, which would cause duplicates (ie every variable would be created twice). But with the code you have posted, DirExists will only be included in the program once.

Note that ifndef means "if not defined." ;-)
Rens
Posts: 256
Joined: Jul 06, 2005 21:09

Post by Rens »

Yep i understand it now.

I use compiler directives when code isn't finished yet
Like the following code:

Code: Select all

#ifndef __CloseDir_bi__
#define __CloseDir_bi__

  #if 0 ' the following code isn't finished (yet), so it won't be compiled

    Sub CloseDir
      _Findclose(DirHandle)
      DirInUse = 0
    end Sub

  #endIf

#endif
When the code is finished replace #if 0 with #if 1 or delete it and don't forget to delete the corresponding #endif
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

I wonder, why not just use #include once?
wallace
Posts: 72
Joined: Dec 08, 2007 16:31

Post by wallace »

Because large projects get modulated and often lots of them all include the same header file. #defining it makes sure that the compiler doesn't add the header more than once in that case.
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Post by elsairon »

wallace wrote:Because large projects get modulated and often lots of them all include the same header file. #defining it makes sure that the compiler doesn't add the header more than once in that case.
If I understand you correctly;

Using #include once does not prevent a file from being included more than once in a multi-module compile, but does in a single module compile.

Is this accurate?
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

Just use #pragma once on top of all your headers.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

elsairon wrote:If I understand you correctly;

Using #include once does not prevent a file from being included more than once in a multi-module compile, but does in a single module compile.

Is this accurate?
Each module in your program can include the header once.
It's entirely possible (indeed, probable) that multiple modules will want to use the same header file, so it's a case of "whichever modules want the header, they each get it once", rather than "the first module can have the header, the others, go fish".

#include once should have the same effect as "#ifndef whatever ... #define whatever .. #endif" (the define is only remembered for the duration of the module), Except one is indicated by the including code, and one is indicated by the included code.
"#pragma once", as cha0s suggests, is a simpler, less hackish way of doing "#ifdef ... #endif" in the included code.
All three work for the duration of the module, so can be used for multi-module projects.

(Conveniently, the "per-module" scope means the compiler doesn't have to remember, between modules, whether a header has been used or not. This would be particularly difficult if you don't compile all the modules at the same time :)
Post Reply