include and import

BlitzMax Forums/BlitzMax Beginners Area/include and import

yossi(Posted 2014) [#1]
what is the difference between include and import ?


xlsior(Posted 2014) [#2]
IIRC:

If you use "import", the imported code won't get re-compiled each and every time. If it was already compiled during a previous run, it will be re-imported as-is if the imported code wasn't modified in the mean time.

with include, it will -- it behaves the same as if the included code was actually typed right where the include statement occurs.


Derron(Posted 2014) [#3]
Included files are copied into the includer file when getting compiled.

See it as a "placeholder" of the actual content.


Means: the order is of importance too:


global i:int = 10
include "i_modifier.bmx"
print i

VS

global i:int = 10
print i
include "i_modifier.bmx"



i_modifier.bmx:
i = i + 10


bye
Ron


yossi(Posted 2014) [#4]
so if I understand you right then when I use include the exe file already contains the code of the included files and therefore the exe file is more bigger but I can distribute it alone with no need to the included files but
when I use import the exe file is more small but I should distribute the imported files a long with the exe file because he need the imported files on run time.

am I right ?


Brucey(Posted 2014) [#5]
No.

Imported files are pre-compiled into object files which are included in your exe at build time.
Included files are inlined into the place in your source where you have the Include call, which is then compiled into your exe.

Unless you are using 3rd-party DLLs, you won't have any extra binary files.


yossi(Posted 2014) [#6]
I can understand the difference technically but I can't understand the actual mining of the difference or in another words when should I use import and when should I use include ?


Derron(Posted 2014) [#7]
Use "import" for things which do not rely on code of your main file.
So: self contained code (they can import other files as well).

Include is just to split code into different "parts" to not loose overview.


Example: Your class "TEnemy" could be in a "import" file as it does everything on its own, it does not rely on variables defined somewhere else.

But as soon as your projectname.bmx-file contains an object "game" with properties like "difficulty" and your "TEnemy"-class uses "Game.difficulty" you cannot import the file anylonger - you then have to include the file.

If you now split the "Game-class" again into a new file, self containing code etc. - you can "import gameclass-file" within the enemy-file. Your projectname now could "import enemy-file" again.


My English isn't the best for explaining such things that is why I try a summary: Imports are possible as soon as the imported file "could get compiled". Includes would have some errors during compilation (unknown function names, types ...).


bye
Ron


yossi(Posted 2014) [#8]
now I understand.
thank you very much.