What does “linker input file not used because link failed” mean? (C makefile)

I created a makefile to compile and link to my program, however I cannot understand why I am getting this error. Is it related to SDL?

GCC = gcc
CFLAGS = -c -std=c99 -lm -Wall -Wextra -pedantic -O3 -Wfloat-equal -g
SDL = -lSDL2 -lSDL2_ttf -lSDL2_image -lSDL2_mixer

all: ./game

game: global.o display.o player.o entities.o controls.o sound.o menu.o
    $(GCC) $(CFLAGS) global.o display.o player.o entities.o controls.o sound.o menu.o -o game

global.o: global.c
    $(GCC) $(CFLAGS) $(SDL) global.c

display.o: display.c
    $(GCC) $(CFLAGS) $(SDL) display.c

player.o: player.c
    $(GCC) $(CFLAGS) $(SDL) player.c

entities.o: entities.c
    $(GCC) $(CFLAGS) $(SDL) entities.c

controls.o: controls.c
    $(GCC) $(CFLAGS) $(SDL) controls.c

sound.o: sound.c
    $(GCC) $(CFLAGS) $(SDL) sound.c

menu.o: menu.c
    $(GCC) $(CFLAGS) $(SDL) menu.c

clean:
    rm *o game
+4
source share
3 answers

there are a few minor flaws in the published make file.

Among them:

  • library names are used only during the link phase, and not at the compilation stage.
  • suggest using the wildcard command to get a list of source files. Then use the patterm replacement operator to get a list of object files:

eg:

SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o)
  1. (, ) , .PHONY: make:

:

.PHONY : all clean
  1. make . . OPs , / .

  2. : rm *o game name.o, . 'o'. , '-f' 'rm'.

:

rm -f *.o game 
  1. : all: ./game .

:

all: game
  1. ( ) :

make:

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@ -I.
  1. -g . gdb, -ggdb

  2. , , = :=

  3. , game , chmod 'link'

, makefile, , makefile

, , .

CC := /user/bin/gcc
RM := /usr/bin/rm

CFLAGS := -c -std=c99 -Wall -Wextra -pedantic -O3 -Wfloat-equal -ggdb
LFLAGS := -std=c99 -O3 -ggdb

SDL := -lSDL2 -lSDL2_ttf -lSDL2_image -lSDL2_mixer

SRC := $(wildcard *.c)
OBJS := $(SRC:.c=.o)


.PHONY : all clean
all: game


game: $(OBJS)
    $(CC) $(LFLAGS)  $(OBJS) -o $@ $(SDL) -lm

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@ -I.


clean:
    $(RM) -f *.o game
+5

:

gcc -c -std=c99 -lm -Wall -Wextra -pedantic -O3 -Wfloat-equal -g global.o display.o player.o entities.o controls.o sound.o menu.o -o game

, , -c. -c gcc . . (.o , , )

. , , -c -std=c99 -Wall -Wextra -pedantic -O3 -Wfloat-equal -g, -lm -lSDL2 -lSDL2_ttf -lSDL2_image -lSDL2_mixer -g.

+5

-lm SDL CFLAGS, . LDLIBS game :

game: global.o display.o player.o entities.o controls.o sound.o menu.o
    $(GCC) $(CFLAGS) -o $@ global.o display.o player.o entities.o controls.o sound.o menu.o $(LDLIBS)

The operator -lm(this is not an option) and the operands for the SDL are used only when binding, so it should not be part of CFLAGSand should not be specified when compiling without binding (i.e. when -cattached).

0
source

Source: https://habr.com/ru/post/1621354/


All Articles