CSC231 Makefile for linking ASM and C Files

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 01:23, 27 October 2010 (UTC)
Revised --D. Thiebaut 10:10, 22 October 2012 (EDT)



References

Makefile for Simple ASM Program


  • Create a file called Makefile in the directory where you have your assembly programs
  • Copy the code below in the file.
  • Replace 16Fibs by the name of your current assembly project.
  • This Makefile assumes that 16Fibs (or whatever your program name is) requires to be linked with 231Lib.asm.
  • Important Note: The lines that are indented in the Makefile must be indented by a TAB, not by spaces.


PROG = 16Fibs

CC = gcc 
LD = ld 
NASM = nasm   

LDFLAGS = -melf_i386 
NASMFLAGS = -f elf -F stabs  


OBJS = 231Lib.o $(PROG).o 

default: $(PROG)  

$(PROG): $(OBJS)  
	$(LD) $(LDFLAGS) $(OBJS) -o $(PROG)  

231Lib.o: 231Lib.asm 
	$(NASM) $(NASMFLAGS) 231Lib.asm  

$(PROG).o: $(PROG).asm 
	$(NASM) $(NASMFLAGS) $(PROG).asm

clean: 
	rm -rf *.o *~  

cleanall: 
	rm -rf *.o $(PROG) *~


  • You can now edit your file and recreate the executable by typing:
  make
at the Linux prompt. Run your program as usual:
 ./16Fibs

  • To remove the object files from your directory, type:
 make clean

  • To remove the executables as well as the object files, as well as the files that have a ~ at the end of their names, type:
 make cleanall


Makefile Combining C and Nasm


CC = gcc
LD = gcc
NASM = nasm 

CFLAGS =  -c -m32
LFLAGS = -m32
NASMFLAGS = -f elf -F stabs

PROG = printBinary

OBJS = asm_io.o $(PROG).o driver.o


default: $(PROG)

$(PROG): $(OBJS) 
	$(LD) $(LFLAGS) $(OBJS)  -o $(PROG)

asm_io.o: asm_io.asm
	$(NASM) $(NASMFLAGS) asm_io.asm

$(PROG).o: $(PROG).asm asm_io.inc
	$(NASM) $(NASMFLAGS) $(PROG).asm

driver.o: driver.c 
	$(CC) $(CFLAGS) driver.c

clean:
	rm -rf *.o *~

real_clean:
	 rm -rf *.o $(PROG) *~


Usage

  • Edit any of driver.c, asm_io.asm, asm_io.inc, or printBinary.asm, and to generate the executable, simply type:
   make
and the executable will be generated. Run it with
   ./printBinary
  • If you want to force a "re-make", simply touch any of the .asm or .c files. See examples below:
[231a@grendel ~/handout]$ make
make: Nothing to be done for `default'.

[231a@grendel ~/handout]$ touch driver.c

[231a@grendel ~/handout]$ make 
gcc -c -m32 driver.c
gcc -m32 asm_io.o printBinary.o driver.o  -o printBinary

[231a@grendel ~/handout]$ make
make: Nothing to be done for `default'.
 
[231a@grendel ~/handout]$ touch asm_io.inc

[231a@grendel ~/handout]$ make
nasm  -f elf -F stabs printBinary.asm
gcc -m32 asm_io.o printBinary.o driver.o  -o printBinary

[231a@grendel ~/handout]$ touch asm_io.asm

[231a@grendel ~/handout]$ make
nasm  -f elf -F stabs asm_io.asm
gcc -m32 asm_io.o printBinary.o driver.o  -o printBinary

[231a@grendel ~/handout]$