Include or Import?

StrikerX

Member
#include "data/scripts/ilu.h"
#import  "data/scripts/library/ani0020.h"

Hi, I would like to know what is the difference between "include" or "import" used on .C files?

Another question. There is diference  between ".h" or ".C" files? Or are letters just a form of organization?

Alll the best.
 
StrikerX,

Letters are for organization, the script engine doesn't care (it will even read files that aren't .c or .h). That said, you'll get a lot more mileage by following general coding standards.

  • .h is meant for #defines and macros.
  • .c is functions and operating code.

If in doubt, just stick with ".c".

#include and #import look similar, but they work very differently from each other:

#include basically copies in everything from the #included file verbatim. It's simple, convenient, and the only way to bring in #defines directives, which is no problem since those are free. The catch is that for everything else (specifically functions), it's extremely wasteful. See also: Every time you #include a set of functions, they get another copy put into memory.

#import enables use of functions from the #imported file by making an internal reference instead of a copy. It's a bit more difficult to understand and you can't use #import on #defines, but it is far more resource efficient. Another advantage of #import is more accurate debugging. When an error occurs in a script with #included functions, the line count contains all the #included files. That gets real confusing, real fast. Errors in scripts with #imported functions are much easier to trace.

Here is the original post about #imports by Plombo.

One last note: You can #include a file with other #includes and #imports, and this leads to one of my favorite techniques. My libraries have a main.c that #includes the macros and #imports the functions. Then to use the library, you just #include the main.c file. The only things getting copied are #imports and #defines, so it's memory frugal and one stop shopping convenient. :)

HTH,
DC
 
It's crazy to imagine a world where OpenBoR does not supports #import and/or #include.

All my scripts are basically a bunch of functions, each of them in a different .c file,
 
Back
Top Bottom