NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: fgiraldeau on May 20, 2013, 09:51:36 PM

Title: Build NASM executable with Automake
Post by: fgiraldeau on May 20, 2013, 09:51:36 PM
I would like to use Automake to build a NASM target, but I have some problem with linking. I tried this:

Code: [Select]
bin_PROGRAMS = myprog

myprog_SOURCES = myprog.asm

.asm.o:
$(NASM) $(NAFLAGS) -g -o $@ $<
ld $@

The .asm.o target works fine, but the linking part is obviously wrong. It produces a.out output, while I would like it to generate the "myprog" executable. Also, it seems that Automake tries to link it itself later and it fails.

The following rules are working well, but are non-generic:
Code: [Select]
bin_PROGRAMS = myprog

myprog.o: myprog.asm
$(NASM) $(NAFLAGS) -g -o $@ $<

myprog: myprog.o
ld -o $@ $<

What would be the best way to compile an NASM executable with automake using generic target?

Thanks!
Title: Re: Build NASM executable with Automake
Post by: Gunner on May 20, 2013, 10:48:59 PM
If automake is anything like make, then it needs to know what files are required to Assemble and Link your exe.

This is a makefile I use on Windows, for Linux it is basically the same, minus the res/rc files
Windows:
Code: [Select]
APP=SomeApp

all: clean $(APP)

$(APP): $(APP).obj $(APP).res
GoLink.exe /console /entry _start $(APP).obj $(APP).res @imports.inc

$(APP).obj: $(APP).asm
nasm -f win32 $(APP).asm -o $(APP).obj

$(APP).res: $(APP).rc
gorc /r=$(APP).res $(APP).rc

clean:APP=
del $(APP).exe
del $(APP).obj
del $(APP).res

Linux w/GCC:
Code: [Select]
APP=fslc_sfs

$(APP): $(APP).o   
gcc -o $(APP) $(APP).o `pkg-config --cflags --libs gtk+-2.0` -export-dynamic -lcurl

$(APP).o: $(APP).asm
nasm -f elf $(APP).asm

Linux w/ld:
Code: [Select]
APP = comparray

$(APP): $(APP).o
ld -o $(APP) $(APP).o -e main -lc -dynamic-linker /lib/ld-linux.so.2

$(APP).o: $(APP).asm
nasm -f elf $(APP).asm

Ok, the first $(APP):, tells make/ the linker what files are required to build your exe.  For windows it is an .obj and .res file, for linux it is just an .o file.  So, to get the obj file or o file, we must assemble it, that is what the label $(APP).obj, $(APP).o does, now in the case of windows, we need a res file also, so make will look for the $(APP).res file and carry out the command we have to build it.  Now that all the files are made, make will now link them to create our exe.