尝试为程序集/nasm 应用程序创建 Makefile 文件

问题描述 投票:0回答:2

我正在尝试创建一个简单的make文件,我现在使用的命令是:

$ nasm -f elf64 main.asm
$ ld -s -o test1 main.o

就是这样。我如何从它们创建一个 Makefile?我已阅读手册,但还没有找到解决方案。

assembly makefile compilation nasm
2个回答
4
投票

您可以使用最简单的规则构建一个:

<target>: <source>
       <build command>

你的 makefile 看起来像这样:

all: test1

main.o:  main.asm
        nasm -f elf64 main.asm

test1:   main.o
        ld -s -o test1 main.o

0
投票

这是一个稍微通用的 makefile

有了这个,您可以直接将 libc 宏直接包含在您的

.nasm
文件中,只要您将正确的标头添加到
$(HEADERS)

通过下面的 makefile,您可以使用

STDIN_FILENO
AF_INET
以及所有系统调用编号,例如
__NR_write

.PHONY: 
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

SRCS = $(wildcard *.nasm) 
TMPS = $(patsubst %.nasm, %.tmp, $(SRCS))
OBJS = $(patsubst %.tmp, %.o, $(TMPS))
HEADERS = \
   -imacros syscall.h \
   -imacros sys/socket.h \
   -imacros unistd.h \
   #add header files above, do not remove this comment
AS  = nasm
ASFLAGS += -g -f elf64
LDFLAGS += -static
NAME =  main 

all:  $(NAME)

$(NAME): $(OBJS)
> ld $(LDFLAGS) $(OBJS) -o $@ 

%.o:  %.tmp
> nasm ${ASFLAGS} -o $@ $^

%.tmp:  %.nasm 
> cpp -w -P $(HEADERS) -o $@ $^
© www.soinside.com 2019 - 2024. All rights reserved.