Qbasicnews.com

Full Version: multiple files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Suppose I have created some subs and functions in freebasic,
and I want to include them in other projects instead of copying
the whole code, how would I do? can I just add the file at compile time with the -b option, and if so should I create a bi file with the
declarations? Include it in the project file? The function source file?
If I use sdl in the functions where should I include sdl.bi?

| Edit:: check out my fb logo Tongue
V
It's exactly like in QB. You create a BI file with the declarations of the FUNCTIONs and SUBs you want to use from another module. Then include that BI in the module from you want to use those FUNCTIONs/SUBs.

When compiling, you can specify a list of modules. The first one will be the main one and the EXE file will be named after it, for example:

Code:
fbc game.bas gfx.bas sound.bas cutscenes.bas

Will generate game.exe and will compile and link all those modules together.

My article about modular program applies completely here as well.
Another cool thing you can do in FB:

This is: MySub.Bi
Code:
declare sub Blah
sub blah
'do whatever
end sub


This is Main.Bas
Code:
cls
print "Hi there!"
'$include: "MySub.Bi"
blah



Bad coding practice, yes, but I like to have each sub/function in a separate file, and no header file with all the DECLARE SUB/function stuff.

Happy me :lol:
I don't know if it's bad coding practice or not (hell, I used that technique a LOT in Two Lords), but it can sometimes make code a tad untraceable. But hey, if you know your stuff, then...no problems. Big Grin
Quote:Another cool thing you can do in FB:

This is: MySub.Bi
Code:
declare sub Blah
sub blah
'do whatever
end sub


This is Main.Bas
Code:
cls
print "Hi there!"
'$include: "MySub.Bi"
blah



Bad coding practice, yes, but I like to have each sub/function in a separate file, and no header file with all the DECLARE SUB/function stuff.

Happy me :lol:

Bad coding practice? That's how C header files work.
One procedure in one file, that file holds both proc. and declare.

And then have the include in the middle of the code Tongue


I put all procedure includes at the end, and variable inits at the top, it looks nice, and I like it

Really easy to find a specific part of the code if you're not using a QB like IDE with F2 thingy (like VonGodrics), I personally prefer Notepad++ Big Grin
Quote:
Z!re Wrote:Another cool thing you can do in FB:

This is: MySub.Bi
Code:
declare sub Blah
sub blah
'do whatever
end sub


This is Main.Bas
Code:
cls
print "Hi there!"
'$include: "MySub.Bi"
blah



Bad coding practice, yes, but I like to have each sub/function in a separate file, and no header file with all the DECLARE SUB/function stuff.

Happy me :lol:

Bad coding practice? That's how C header files work.

Nope. It's bad coding practice in C as well. Header files should only contain specifications.

If you have a whole function in a header file and you include it in several modules, it will be replicated several times in your compiled EXE. Tracing and debugging can be a hell.
That's correct.

It's basically like the entire code being replicated whenever you call it...almost defeats the purpose of subroutines.
Yes, but why use many modules, you only have one main function.

Procedures shouldnt call other procedures anyways.


Main call all procs, proc never call proc.

You only include it once. And with FB you don't really do modular coding the way you do in QB, you compile one file (most of the time) as you can go 2gb source anyways.

So having all functions in different files (only included once) will make it easier to find a specific function, just look in the inc folde,r and you'll se: Video_Pset.bi, for example.

If you include it once in your main code, you can even call it from other procedures if you want. FB is not C, C is weird. Big Grin FB is just crazy :lol:
Umm after experimenting a lot I have now given up could you help me
with this?

---
Original source file:
-Uses functions/data types from sdl
-Uses functions/data types from myfunctions.bas
---
Myfunctions.bas
-Uses functions/data types from sdl
---
Myfunctions.bi
-Contains declarations for myfunctions.bas
-Uses functions/data types from sdl
---

Where should I put '$include: "sdl/sdl.bi" and/or '$include: "myfunc.bi"

Whatever I try, fbc seems to throw "duplicate definion" or the
"opposite" at me...
Pages: 1 2