Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make: Circular dependency dropped

I've already searched a long time on stackoverflow and other make manuals, websites but cannot find any trailing whitespace or miss usage in make functions. Can you help me solve this warning message ?

make: Circular main.asm.o <- main.asm dependency dropped.

Makefile:

AS:=yasm
CC:=gcc
OUTPUTDIR:=$(shell pwd)/bin
ASFLAGS:=-g dwarf2 -f elf64 -a x86
CFLAGS:=-g

SOURCES=$(wildcard *.asm)
OBJECTS=$(patsubst %.asm,%.o,$(SOURCES))

%.o: $(SOURCES)
    $(AS) $(ASFLAGS) -o $(OUTPUTDIR)/$(OBJECTS) $<

all: $(OBJECTS)
    $(CC) $(CFLAGS) -o httpd $(OUTPUTDIR)/$(OBJECTS)

clean:
    rm $(OUTPUTDIR)/*
    rm httpd

main.asm:

section .text
  global main
  extern exit

main:
  mov rdi, 1
    call exit   

thanks you :)

like image 771
Amaury Brisou Avatar asked Jul 23 '14 08:07

Amaury Brisou


Video Answer


2 Answers

Your error is this line:

%.o: $(SOURCES)

which presumably expands to something like

%.o: main.asm foo.asm bar.asm

What that means is something very approximately like

main.asm.o: main.asm
foo.asm.o: foo.asm
bar.asm.o: bar.asm
    ....

That's 'approximately' because you're mixing up the syntax here.

You're confusing an ordinary rule (target: source) with a wildcard rule (%.target: %.source). What you probably want is

%.o: %.asm
    $(AS) $(ASFLAGS) -o $@ $<

which teaches Make how to make .o files from .asm files, combined with

httpd: $(SOURCES:.asm=.o)
    $(CC) $(CFLAGS) -o httpd $*

which tells Make how to combine the various .o files into the httpd executable. The $(SOURCES:.asm=.o) variable reference expands to a list of .o files as dependencies, and Make now knows how to create those .o files from the corresponding .asm files.

Extra observations:

  1. In case it's not obvious, what $(SOURCES:.asm=.o) does is to expand to the value of $(SOURCES), but with trailing .asm replaced by .o.
  2. What I personally would do with this Makefile is to replace the wildcard in the definition of SOURCES with an explicit list of files. That way, you won't get unexpected results if you, for example, add a my-temp-test.asm file to the directory. (This comes under the heading of ‘how can I be nice to future-me?’)
like image 136
Norman Gray Avatar answered Nov 02 '22 13:11

Norman Gray


AS:=yasm
CC:=gcc
ASFLAGS:=-g dwarf2 -f elf64 -a x86
CFLAGS:=-g

OBJECTSDIR:=$(shell pwd)/bin
SRCDIR:=$(shell pwd)/src
SOURCES=$(wildcard $(SRCDIR)/*.asm)
OBJECTS=$(shell find $(OBJECTSDIR) -name *.o)


%.o: %.asm
    $(AS) $(ASFLAGS) -o $(subst $(SRCDIR),$(OBJECTSDIR),$@) $<

httpd: $(SOURCES:.asm=.o)
    $(CC) $(CFLAGS) -o httpd $(OBJECTS)


clean:
    rm -f $(OBJECTSDIR)/*
    rm -f httpd

Thanks to you explication Norman, I did that. It's important for me to have distinct folders, /bin and /src so that everything stay clear.

Thank you, It's working and I understand my error.

note: if I put any object file in the Makefile folder I got weird error from make... just deleting them make it working again.

like image 43
Amaury Brisou Avatar answered Nov 02 '22 11:11

Amaury Brisou