nasm include
nasm include
I use nasm for my work. Say I have a file named init.asm executed by the bootloader which includes some files but 1 of the included files need a system call from a file not included by init.asm, so I could include the needed file in the include file BUT then if init.asm executes another file called main.asm which includes the files included in init.asm PLUS the file needed for 1 of those included files nasm would show a "conflicting defenitions" error. So is there any way of including a file and making sure that no other files include it one more time?
Re: nasm include
I don't see the problem, why complicate it so much when all you need to do is to only include everything needed in the main file? If something is needed by an included file, you just make sure to include that too in the main file. Also, finding what you needed took 30 seconds with google, it's in the nasm manual:
4.5 Including Other Files
Using, once again, a very similar syntax to the C preprocessor, NASM's 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 searched for in the current directory (the directory you're in when you run NASM, as opposed to the location of the NASM executable or the location of the source file), plus any directories specified on the NASM command line using the -i option.
The standard C idiom for preventing a file being included more than once is just as applicable in NASM: 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 NASM command line (see section 2.1.7).
Re: nasm include
Some good advice: don't put code in include files. EQU's, %define's and %macro's, fine, but no code. If you need some code, it should be in INIT.ASM to start with.
Re: nasm include
As opposed to C/C++, where the calling code needs to know certain things about the called code (parameter list, return type), such requirements do not exist for ASM. (Besides, even C/C++ can give you trouble if you include code in header files.)
You can have init.asm calling a function implemented in lib.asm, and you can have main.asm calling the same function in lib.asm, and you don't need to "include" anything. You simply assemble each of the *.asm files into an *.o file and link them together, just like that.
If you want to share certain %defines or %macros across files, +1 to what Hobbes said.
You can have init.asm calling a function implemented in lib.asm, and you can have main.asm calling the same function in lib.asm, and you don't need to "include" anything. You simply assemble each of the *.asm files into an *.o file and link them together, just like that.
If you want to share certain %defines or %macros across files, +1 to what Hobbes said.
Every good solution is obvious once you've found it.
Re: nasm include
I think you need EXTERN external_function_name for that? although you can have that line in your ASM, or centralize it in a header file.Solar wrote:You can have init.asm calling a function implemented in lib.asm, and you can have main.asm calling the same function in lib.asm, and you don't need to "include" anything. You simply assemble each of the *.asm files into an *.o file and link them together, just like that.
Re: nasm include
Also, you don't need to declare a symbol "before use" (i.e. in some previous source line, as it is in C). So, if you split the code into several files, then in your main file you can do:The symbols in both files will be visible between them (as long as the main file is an input to nasm!).
Code: Select all
%include "somefile.asm"
%include "otherfile.asm"
Re: nasm include
Well, that's a feature - not a bug - so the outcome depends on the user.berkus wrote:That looks a lot more like a bad side-effect rather than something desirable.
Re: nasm include
It's not. That's standard behavior. Besides, that's exactly what I tried to tell him in my first reply almost a month ago. No idea where this sudden interest from everybody to reply so late came from.berkus wrote:That looks a lot more like a bad side-effect rather than something desirable.ydoom wrote:The symbols in both files will be visible between them (as long as the main file is an input to nasm!).