#include once vs. #include

General FreeBASIC programming questions.
Post Reply
BobPaw
Posts: 41
Joined: Dec 13, 2014 2:03
Location: Texas, USA

#include once vs. #include

Post by BobPaw »

So on the forum, I've seen people using "#inlcude once", but I usually use just "#include". What's the difference?
Last edited by BobPaw on Jan 19, 2015 21:40, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: #include once vs. #inlcude

Post by fxm »

The once specifier tells the compiler to include the file only once even if it is included several times by the source code.
BobPaw
Posts: 41
Joined: Dec 13, 2014 2:03
Location: Texas, USA

Re: #include once vs. #inlcude

Post by BobPaw »

What would happen if you tried to include it multiple times?
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: #include once vs. #inlcude

Post by fxm »

The compiler includes the file once only.
Last edited by fxm on Jan 20, 2015 7:57, edited 1 time in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: #include once vs. #include

Post by caseih »

So the C standard is to use ifdef fences in header files to prevent multiple including. With include once this would not be necessary, correct? Is it still good practice to use ifdef fences in .bi files? Is there ever really a use case for only include vs include once?

I'm glad things are a lot more clearer in FB than in PHP. Ugg. include vs include_once vs require vs require_once. Quite confusing in that language.
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: #include once vs. #include

Post by PaulSquires »

Using #Include Once saves the compiler from having to parse the code in the file more than once. This could presumably help speed up the compile process.

caseih - rather than use the ifdef fence method, try using the #Pragma Once statements. It is a little cleaner approach.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: #include once vs. #include

Post by counting_pine »

One problem I've read of with #pragma/include once is that it is not always easy for the compiler to tell when code tries to include a file twice.

It could be that your program #includes headers 'inc/foo.bi' and 'inc/bar.bi'. But inc/foo.bi might #include 'bar.bi' as well. The compiler has to know that 'inc/bar.bi' (accessed from './') and 'bar.bi' (accessed from './inc/') are the same file. And I'm sure you can conceive more complicated relationships.

It should be fairly easy to resolve most simple ones, and anything more complicated (e.g. absolute paths, changing the case) shouldn't appear in a well-written project, but it's something to be aware of.

A potential advantage of #ifndef-style guards is that the compiler can treat them like a #pragma once, and doesn't need to reopen a file if it can detect that such a device is used in it, and the relevant define is present.
In the worst case, if it fails to detect the file has been parsed before, it just has to reopen the file and skip over everything in the #ifndef block.
You can read a bit about it here.
http://www.bobarcher.org/software/include/index.html

Note that FB doesn't actually support anything like this though currently, so using #pragma/include once is still the fastest way.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: #include once vs. #include

Post by dodicat »

If you include crt.bi inside a function you get a million compile errors.
Obviously, with extern "c" and declarations of functions e.t.c.

But if you #include "crt.bi" the usual way, that is, at the program start, all is OK, even if you still include it in the function.

Example, a format using the c function sprintf().
This format simulates print using (or near enough)

Code: Select all

 #include "crt.bi"
 
function Cformat(byval x as double,byval precision as integer=30) as string
     #include "crt.bi"
    if precision>30 then precision=30
    dim as zstring * 40 z,s="%." &str(abs(precision)) &"f"
    sprintf(z,s,x)
    return rtrim(rtrim(z,"0"),".")
end function 



for n as integer = 1 to 50
    var x=(rnd*5-rnd*5)
    print x,Cformat(x,5),using "###.#####";x
    next n

sleep


 
For the million errors comment out #include "crt.bi" at the beginning.
Post Reply