Import Vs Include

BlitzMax Forums/BlitzMax Programming/Import Vs Include

zambani(Posted 2008) [#1]
Hi everyone, I'm relatively new to Bmax.
What's the diffrence between Import and Include. It seems like Include is all you need. When is Import usefull.
Also, how can I precomplie a .bmx file to be used later on

Thanks.


christian223(Posted 2008) [#2]
Include includes all the code in a file into another file, so the end result is a bigger file. Import does not include, so the file size is smaller. Use the search function, im sure youll find a better answer in posts of the pasts. There is also a sticky on the beginers forum you should check.


ziggy(Posted 2008) [#3]
imported files are compiled separately, while included files are compiled like one big file. There are several diferences when dealing with several files in the same application using import and include.

The first one is that imported files doesn't share globals and types with the file they are being imported to, while this is possible on included files. Imported files are also compiled separatelly, that improves compilation time sometimes and that files can be safely shared across several programs.
In general therms I would recommend you to use include untill you're more familiar with BlitzMax.

the file size of the resultant executable should be very similar (if not the same) when using imports or includes to add files to your program or application.


zambani(Posted 2008) [#4]
The first one is that imported files doesn't share globals and types with the file they are being imported to, while this is possible on included files. Imported files are also compiled separatelly, that improves compilation time sometimes and that files can be safely shared across several programs.
In general therms I would recommend you to use include untill you're more familiar with BlitzMax.



So what's the most common reason to use import instead of include?


Gabriel(Posted 2008) [#5]
So what's the most common reason to use import instead of include?

When you find yourself wondering :
how can I precomplie a .bmx file to be used later on


That's how you do it. If the imported file doesn't change, it is not recompiled.


zambani(Posted 2008) [#6]
When you find yourself wondering:

I don't get it.


ziggy(Posted 2008) [#7]
If you make a set of functions you regulary use in most of your programs, you can put all them on a file, and import this file on any program that needs those functions.
As a side note, when you create an exe, there are 2 parts involved:
-compiling the source code
-linking the program

the first part (compiling) can take a while if your program is very big, linking is ussually a lot faster.

When using an imported file. The compilation part for this file doesn't have to be redone unless this file is changed.

Example:
1.- File1.bmx includes file2.bmx
2.- I compile my program for the first time so:
3.- File2.bmx is included inside file1 before compiling
4.- File1.bmx + file2.bmx is compiled
5.- The program is linked
6.- I get the EXE

Then I modify file1.bmx and want to make the exe again:
1.- File1.bmx includes file2.bmx
2.- I compile my program again:
3.- File2.bmx is included in file1 before compiling
4.- File1.bmx + file2.bmx is compiled
5.- The whole program is linked
6.- I get the EXE

Then I modify file2.bmx and want to make the exe again:
1.- File1.bmx includes file2.bmx
2.- I compile my program again:
3.- File2.bmx is included in file1 before compiling
4.- File1.bmx + file2.bmx is compiled
5.- The whole program is linked
6.- I get the EXE


The same example using imports:

Example:
1.- File1.bmx IMPORTS file2.bmx
2.- I compile my program for the first time so:
3.- File2.bmx is compiled
4.- File1.bmx is compiled
5.- The program is linked
6.- I get the EXE

Then I modify file1.bmx and want to make the exe again:
1.- File1.bmx IMPORTS file2.bmx
2.- I compile my program again:
3.- File1.bmx is compiled
4.- The whole program is linked using the old compilation of file2, as the file2 source has not changed since last compilation.
5.- I get the EXE

Then I modify file2.bmx and want to make the exe again:
1.- File1.bmx IMPORTS file2.bmx
2.- I compile my program again:
3.- File2.bmx is compiled
4.- The whole program is linked using the old compilation of file1, as the file1 source has not changed since last compilation.
5.- I get the EXE

Imagine file2.bmx has a lot of functions I usually put in my programs, only the first program to import this file compiles it, all the others only need to link it (unless modified again, wich is automatically detected by blitzmax, so no worries.)


Brucey(Posted 2008) [#8]
In general therms I would recommend you to use include untill you're more familiar with BlitzMax.

I'd disagree, as once you learn a bad habit it's hard to let it go...


ziggy(Posted 2008) [#9]
I'd disagree, as once you learn a bad habit it's hard to let it go...

It is not a bad habit to include files. It is a bad habit to include everything, as it is to import everything.
I think that a novice coder (sory zambani if that's not your case) should start with the language strutures before learning how to organize the correct compilation clusters (what to import and what to include), but I may be wrong here, as the learning process is very personal.


zambani(Posted 2008) [#10]
Thanks a lot ziggy. I got it now.
On a side note, I purchased BLIde Plus about a month ago and I really like it. Keep up the good work. One quick question. After creating and app using BLIde solution, can I then take all the individual files (.bmx,etc) and recompile them with just the standard blitzmax on a mac?


ziggy(Posted 2008) [#11]
@zambani: Yes of course! Just compile the main bmx file of each exe (the one created by BLIde as a program manager). This files contains all the includes imports framework etc sentences to compile the entire app, so compiling this from the regular ide on a mac or linux computer works, as long as you haven't put there any windows secific code (windows api cals and the like).