-Wunused-command-line-argument编译时出错(C Makefile)?

时间:2017-10-02 06:44:52

标签: c gcc makefile

当我使用makefile编译时,我收到以下警告:

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

这可能是因为我在Makefile(下面)中弄乱了一些东西。我没有正确链接lib吗?有谁能指出我的问题?

  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

2 个答案:

答案 0 :(得分:3)

当前的问题出现在这条规则中:

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

选项-c表示仅编译,不链接,因此忽略任何与链接器相关的命令行参数,因此您会收到该警告。删除-c即可。

那就是说,这个Makefile “搞砸了”。通常,您的目标应该创建的文件,例如

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

你这样做,make并不比shell脚本更有用,因为它不知道创建了哪些文件,也无法检查是否需要重建。此外,如果偶然发现名为list的文件,它将完全停止工作。如果您有不创建文件的规则,则必须告知将它们作为 phony 目标进行制作,例如:

.PHONY: all clean

答案 1 :(得分:0)

makefile是一组需要完成的顺序事项,但是,如果某个创建的文件的“依赖项文件”没有变化,那么该文件将不会被重新创建。

最好具体说明要运行的具体应用程序。

应该使用.PSEUDO运算符标记make文件中实际不创建具有相同名称的文件的

目标。

这是一个正确编写的makefile的示例,使用发布的makefile作为基础:我在makefile中包含了一些注释,以阐明每一步的操作

注意那里(在这个make文件中)没有将编译参数和规则与链接参数和规则混合

# 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