Import Question

BlitzMax Forums/BlitzMax Beginners Area/Import Question

Ant(Posted 2006) [#1]
Hi there a question about importing and dependencies. I have 2 types that I want to store in a seperate files (type A and Type B).

So including my main file, I want 3 files altogether -
main.bmx
file2.bmx (contains type A)
file3.bmx (contains type B).

Currently I import file2 and file3 in main.bmx which is fine. However, type A in file2 references a type which is contained within file3.bmx - does thiis mean that I have to import file3 into file 2? This does work but seems messy as I am importing the file twice. Are there any nicer solutions to this aside from restructuring my code?
Thanks!


Diablo(Posted 2006) [#2]
you could of course just use include
include "file1.bmx"
include "file2.bmx"


FlameDuck(Posted 2006) [#3]
This does work but seems messy as I am importing the file twice.
Doesn't matter, it's only linked once. It's not messy, it's the best way to do it and keep code modular and reusable.


Robert Cummings(Posted 2006) [#4]
So if I include the same file many times for each import, is the data duplicated and will it slow down my program?


FlameDuck(Posted 2006) [#5]
From what I understand, if you use include the code in question is inserted "inline" in your program, before it is compiled and yes it will potentially be duplicated. However it will not slow down your programs execution speed, as you'll get a "Duplicate identifier" (or something along those lines) compiler error if you manage to screw up.

In contrast import tells the compiler to generate symbols to another object file, instead of embedding it.

This means that if two different files both import "myLib.bmx" they will be linked against the same object file, where as two different files, both including "myLib.bmx" will have the myLib.bmx file embedded into their source code (and thus respective object files) and may give you dupelicate identifer errors.

Probably not very clear -- it's kinda late.


Ant(Posted 2006) [#6]
Ok Ive split up my program and seperate files with duplicate import files and I'm getting the error Unable to open file '.bmx/main.bmx.debug.win32.i'

One of my files now refuses to recognise an array within a type that is declared elsewhere...which worked fine when it was one large file.

The instance I have declared is a global variable - surely this will be recognised across all imported files?
Eek! Its all gone horribly wrong!


Dreamora(Posted 2006) [#7]
No
global vars are only known in the files they are declared and in files they are imported to. Other files don't know of the global variable as they never met its declaration.


Brucey(Posted 2006) [#8]
Imagine you had this :

File A
File B

In File A, you declare a global variable, and import File B.

If you try to use that global variable in File B, you will probably get a compile error, since File B doesn't know anything about File A and its variables.

However, if you really felt the need to have globals everywhere, you could create a File C, put your global declarations in it, and import File C in each of the other files.

One thing you shouldn't do, is have File A importing File B, and File B importing File A. This creates the "Duplicate Identifier" compile errors.
If you find you need to do that then your design is flawed, and you should try to modify the code so that you don't need to import one of them.


Ant(Posted 2006) [#9]
Ok boys that explains a great deal! Thanks