Include or Import?

BlitzMax Forums/BlitzMax Beginners Area/Include or Import?

Could someone please explain the difference between the Import command and the Include command. Is Import just for Importing modules or can it be used to include other files too, in place of include?

I`m a little confused. Any help would be great.

Jason.

Include works as if the files you are including were part of the same, very large file. That is, when you compile, it replaces the Include line with the contents of the file you are including.

Importing on the otherhand keeps the source separate at compile time, instead linking each compiled section together. This has benefits in that depending on which files you've made changes to, only the changes need to be re-compiled, and therefore speeding up the compile time significantly.
One issue with Importing is its inability to support cyclic imports, where A imports B imports A. You might find you need to do that if a Type in A referred to a Type in B, and vice versa. However, more often than not, a change to your code logic can get around that problem.

I use imports exclusively :-)

Cheers that make much more sense now :)

Jason.

Thanks Brucey. So..... if you use import, it's up to you to compile ahead of time any changes because import does pre-compile. Right?

Anyone know how else Import is different from Include? When I did a quick experiment with it this morning, I started getting compile errors in some outside code. Things like duplicate local variables and missing type references... but this stuff didn't show up when it was an include. I'm guessing that there's some sort of different set of compile time dependencies that are in play?

Remember that include effectively replaces the Include line with the source from the included file. Therefore, all those files are basically really one big file. That means that any included files that depend on Types/Functions etc in other files don't really depend on the other files, since it is just one *big* file split into parts.

However, since Imported files are compiled separately, you cannot have file A importing B when you have B depending on code in type A. You will get compilation errors.

You can always create files for Globals if you need them. Say, call it C, which both A and B can import.

Using Import is all down to the design of your code. If you build it modularly then you won't have a problem. If however, everything depends on everything else, then you might find difficulty using Imports.
A good design can avoid that problem.

HTH

So..... if you use import, it's up to you to compile ahead of time any changes because import does pre-compile. Right?
No, the imported code will be recompiled by BMK if it has changed (source timestamp later than object timestamp*).

This is only relevant for normal code if 'QuickBuild' is enabled, otherwise the imported code will *always* be recompiled before being linked.


*This is determined differently if you're using the speed-up hack, I believe.