NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: FrEEzE2046 on June 09, 2011, 01:00:56 PM

Title: Relative include files (in parent or sub directory)
Post by: FrEEzE2046 on June 09, 2011, 01:00:56 PM
I want to include some files in the parent or sub directory of my source files. Is this possible?
Title: Re: Relative include files (in parent or sub directory)
Post by: JoeCoder on June 09, 2011, 03:51:21 PM
Funny you should ask that. Rob gave me some information today that should answer your question:

http://forum.nasm.us/index.php?topic=1095.0

Yes, you can do that.
Title: Re: Relative include files (in parent or sub directory)
Post by: cm on June 09, 2011, 03:57:20 PM
Short answer: No.

Long answer 1: No, not strictly; but you can include files relative to NASM's working directory, ie, the directory from which NASM is started. Since this is usually where your source files live, this is then good enough. I would suggest specifying the relative paths with forward slashes as usual. (Since both forward and backward slashes work on MS Windows systems, but only forward slashes work on Unix-like systems.)

Long answer 2: If you really need to include files relative to where your source files are (as opposed to NASM's working directory), it is possible to work with the single-line macro __FILE__ defined by the preprocessor. That works because if NASM's working directory isn't the same as your source file's, then the string that __FILE__ expands to must contain the relative (from NASM's working directory) or absolute path to your source file. Here's an example:

Code: [Select]
D:\Coding\Testing\sub>dir /b /s
D:\Coding\Testing\sub\sub
D:\Coding\Testing\sub\test
D:\Coding\Testing\sub\test.asm
D:\Coding\Testing\sub\sub\test.mac

D:\Coding\Testing\sub>type test.asm

%include "sub/test.mac"

D:\Coding\Testing\sub>nasm test.asm

D:\Coding\Testing\sub>cd sub

D:\Coding\Testing\sub\sub>nasm ../test.asm
../test.asm:2: fatal: unable to open include file `sub/test.mac'

D:\Coding\Testing\sub\sub>cd ..

D:\Coding\Testing\sub>type test.asm

%strcat INC __FILE__,"/../sub/test.mac"
%include INC

D:\Coding\Testing\sub>nasm test.asm

D:\Coding\Testing\sub>cd sub

D:\Coding\Testing\sub\sub>nasm ../test.asm

D:\Coding\Testing\sub\sub>

As you see, after modifying the source file to use the path information which must be stored in __FILE__'s expansion, assembling the file works even if NASM's working directory isn't the file's directory.