3.6. Including Other Files

Using, once again, a very similar syntax to the C preprocessor, the NASM preprocessor lets you include other source files into your code. This is done by the use of the %include directive:

%include "macros.mac"

will include the contents of the file macros.mac into the source file containing the %include directive.

Include files are first searched for relative to the directory containing the source file that is performing the inclusion, and then relative to any directories specified on the Yasm command line using the -I option (see Раздел 1.3.3.3), in the order given on the command line (any relative paths on the Yasm command line are relative to the current working directory, e.g. where Yasm is being run from). While this search strategy does not match traditional NASM behavior, it does match the behavior of most C compilers and better handles relative pathnames.

The standard C idiom for preventing a file being included more than once is just as applicable in the NASM preprocessor: if the file macros.mac has the form

%ifndef MACROS_MAC
    %define MACROS_MAC
    ; now define some macros
%endif

then including the file more than once will not cause errors, because the second time the file is included nothing will happen because the macro MACROS_MAC will already be defined.

You can force a file to be included even if there is no %include directive that explicitly includes it, by using the -P option on the Yasm command line (see Раздел 1.3.3.4).