waffles
waffles

Reputation: 91

-Wunused-command-line-argument error when compiling (C Makefile)?

When I compile using my makefile I get these warnings:

clang: warning: -lllist: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-L./bin' [-Wunused-command-line-argument]

This is probably because I have messed something up in my Makefile (below). Am I not linking the lib properly? Can anyone point me toward the problem?

  all: list parser

parser: list parserCal bin/LinkedListAPI.o bin/CalendarParser.o
    ar cr bin/libcparser.a bin/LinkedListAPI.o bin/CalendarParser.o

list: listparser bin/LinkedListAPI.o
    ar cr bin/libllist.a bin/LinkedListAPI.o

listparser: src/LinkedListAPI.c include/LinkedListAPI.h
    gcc -Wall -std=c11 -c -Iinclude src/LinkedListAPI.c -o bin/LinkedListAPI.o

parserCal: src/CalendarParser.c include/LinkedListAPI.h include/CalendarParser.h include/HelperFunctions.h
    gcc -Wall -std=c11 -c -L./bin -lllist -Iinclude src/CalendarParser.c -o bin/CalendarParser.o

TEST: list parser main.c
    gcc -Wall -std=c11 -Iinclude main.c -o bin/runMe -L./bin -lllist -lcparser

clean:
    rm bin/*.o bin/*.a

Upvotes: 5

Views: 18458

Answers (2)

user3629249
user3629249

Reputation: 16540

a makefile is set of sequential things that need to be done, however, if a certain created file has no changes in its' dependency files, then that file will not be recreated.

It is best to be very specific about what specific utility program is to be run.

targets in the make file that don't actually create a file with the same name should be flagged using the .PSEUDO operator.

here is an example of a correctly written makefile, using the posted makefile as the basis: I've included a few comments in the makefile to clarify what is being done at each step

notice there (in this make file) is no mixing of compile parameters and rules with link parameters and rules

# use ':=' so the macro will only be evaluated once
# using just '=' results in the macro being reevaluated each time it is referenced.
# notice that desirable parameters are being set inside the macro definitions

CFLAGS := -Wall -Wextra -pedantic -std=c11

RM := /bin/rm -f

CC := /bin/gcc

AR := /bin/ar cr

# tell 'make' that these targets do not produce a file of the same name
.PSEUDO: all clean

# this is the first target in this makefile
# it lists the three 'final' targets
# so each of those target rules will be executed

all: bin/runMe bin/libcparser.a bin/libllist.a


# link step
bin/runMe: main.o bin/CalendarParser.o bin/LinkedListAPI.o 
    $(CC)  main.o bin/CalendarParser.o bin/LinkedListAPI.o -o bin/runMe -L./bin -lllist -lcparser


# archive creation step
bin/libcparser.a: bin/LinkedListAPI.o bin/CalendarParser.o
    $(AR)  bin/libcparser.a bin/LinkedListAPI.o bin/CalendarParser.o


# archive creation step
bin/libllist.a: bin/LinkedListAPI.o
    $(AR)  bin/libllist.a bin/LinkedListAPI.o


# notice the archive targets and final runnable file
# reference certain dependencies.
# when a dependency is not available or not 'up to date'
# then the associated rule is executed to bring that dependency 'up to date'

# compile ./src/LinkedListAPI.c to create ./bin/LinkedListAPI.o
bin/LinkedListAPI.o: src/LinkedListAPI.c include/LinkedListAPI.h
    $(CC) $(CFLAGS) -c  src/LinkedListAPI.c -o bin/LinkedListAPI.o -Iinclude


# compile ./src/CalendarParser.c to create ./bin/calendarParser.o
bin/CalendarParser.o: src/CalendarParser.c include/LinkedListAPI.h include/CalendarParser.h include/HelperFunctions.h
    $(CC) $(CFLAGS) -c src/CalendarParser.c -o ./bin/CalendarParser.o -Iinclude 


# compile main.c to create main.o
main : main.c include/LinkedListAPI.h include/CalendarParser.h include/HelperFunctions.h
    $(CC) $(CFLAGS)  -c main.c -o main.o -Iinclude


clean:
    $(RM) bin/*.o bin/*.a

Upvotes: 0

user2371524
user2371524

Reputation:

The immediate problem is in this rule:

TEST: list parser main.c
    gcc -Wall -std=c11 -c -Iinclude main.c -o bin/runMe -L./bin -lllist -lcparser

The option -c means only compile, do not link, so any linker-related command line arguments are ignored, therefore you get that warning. Remove -c and it will work.

That said, this Makefile is "messed up". Normally, your targets should be the files created, e.g.

bin/libllist.a: listparser bin/LinkedListAPI.o
    ar cr bin/libllist.a bin/LinkedListAPI.o

The way you do it, make is not more useful than a shell script, as it doesn't know which files are created and can't check whether rebuilding is necessary. Also, it would stop working at all if there was by accident a file named list. If you have rules that don't create a file, you must tell make about it by putting them as phony targets, e.g.:

.PHONY: all clean

Upvotes: 8

Related Questions