简体   繁体   中英

compilation with Makefile fail but success in command line

I was using a Makefile to compile my project and compiled successfully, but when I added a new lib (libbcm2835.a) to linker (-lbcm2835) it fails, otherwise when using the following commands it compile and link with no error :

gcc -c ihome_*.c
gcc -o iHome_Start ihome*.o -lbcm2835 -lpthread

Makefile :

# project name (generate executable with this name)
TARGET   = iHome_Start

CC       = gcc
# compiling flags here
CFLAGS   = -std=c99 -Wall -I.

LINKER   = gcc -o
# linking flags here
LFLAGS   = -lpthread -lbcm2835

# change these to set the proper directories where each files shoould be
SRCDIR   = .
OBJDIR   = .
BINDIR   = .

SOURCES  := $(wildcard $(SRCDIR)/*.c)
INCLUDES := $(wildcard $(SRCDIR)/*.h)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
rm       = rm -f


$(BINDIR)/$(TARGET): $(OBJECTS)
    @$(LINKER) $@ $(LFLAGS) $(OBJECTS)
    @echo "Linking complete!"

$(OBJECTS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
    @$(CC) $(CFLAGS) -c $< -o $@
    @echo "Compiled "$<" successfully!"

.PHONEY: clean
clean:
    @$(rm) $(OBJECTS)
    @echo "Cleanup complete!"

.PHONEY: remove
remove: clean
    @$(rm) $(BINDIR)/$(TARGET)
    @echo "Executable removed!"

The problem is in

@$(LINKER) $@ $(LFLAGS) $(OBJECTS)

The linker processes the arguments in an order they appear. By the time it sees the libraries, it hadn't yet seen no object files, hence there are no unresolved symbols, hence it pulls nothing from the libraries. Swap $(OBJECTS) and $(LFLAGS) :

@$(LINKER) $@ $(OBJECTS) $(LFLAGS)

I would also recommend to rename LFLAGS to LIBRARIES .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM