Author Topic: Relative include files (in parent or sub directory)  (Read 3645 times)

Offline FrEEzE2046

  • New Member
  • Posts: 1
Relative include files (in parent or sub directory)
« 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?

Offline JoeCoder

  • Jr. Member
  • *
  • Posts: 72
  • Country: by
Re: Relative include files (in parent or sub directory)
« Reply #1 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.
If you can't code it in assembly, it can't be coded!

Offline cm

  • Jr. Member
  • *
  • Posts: 65
Re: Relative include files (in parent or sub directory)
« Reply #2 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.
C. Masloch