NASM: Makefile for library

1k views Asked by At

I'm having trouble building a makefile for a library in nasm, since it requires that you run nasm with one input file at a time. I have tried with the %.o : %.s thing but I'm probably doing it incorrectly since it's not working. Here is what I have:

NAME = libfts.a
SRC_ASM =   file1.s \
        file2.s \
        file3.s \

OBJ_ASM = $(SRC_ASM:.s=.o)
FLAGS = -Wall -Werror -Wextra
CC_ASM = nasm
ASM_FLAGS = -f macho64

all :       $(NAME)

$(NAME) :   $(OBJ_ASM)
    @ar rc $(NAME) $(OBJ_ASM)
    @ranlib $(NAME)

#$(OBJ_ASM) :   $(SRC_ASM)
#   nasm -f macho64 -o $(OBJ_ASM) $(SRC_ASM)

%.o : %.s
    nasm $(ASM_FLAGS) -o $@ $<

clean:
    rm -f *.o main.o

fclean : clean
    rm -f libfts.a

re : fclean all

.PHONY: clean fclean re

I tried commenting and uncommenting the commented part, moving things around, but nothing seems to work for me :(

Right now I'm getting ar: no archive members specified Other errors I had included as running instead of nasm and for just the 1st file in SRC_ASM thanks :)

1

There are 1 answers

1
Michael On BEST ANSWER

The following appears to work fine for me:

NAME = libfts.a
SRC_ASM =   file1.s \
        file2.s \
        file3.s \

OBJ_ASM = $(SRC_ASM:.s=.o)
FLAGS = -Wall -Werror -Wextra
NASM = nasm
AR = ar
RANLIB = ranlib
ASM_FLAGS = -f macho64

all :       $(NAME)

$(NAME): $(OBJ_ASM)
    $(AR) rc $(NAME) $(OBJ_ASM)
    $(RANLIB) $(NAME)

%.o : %.s
    $(NASM) $(ASM_FLAGS) -o $@ $<

fclean : clean
    rm -f libfts.a

re : fclean all

.PHONY: clean fclean re

The terminal output I get is:

$ make -f makefile
nasm -f macho64 -o file1.o file1.s
nasm -f macho64 -o file2.o file2.s
nasm -f macho64 -o file3.o file3.s
ar rc libfts.a file1.o file2.o file3.o
ranlib libfts.a