2

我的 makefile 与 gcc 编译器有问题。如果我直接使用 gcc:

gcc -std=c99 -lm tm.c tm_coins.c tm_options.c tm_stock.c tm_utility.c -o tm -Wall -pedantic

一切正常。我需要-std-c99 和-lm。

但是,我被告知要使用 makefile。这是我的制作文件:

CFLAGS=-ansi -Wall -pedantic
LFLAGS=-std=c99 -lm 
CC=gcc
all:tm
tm:tm.o tm_coins.o tm_options.o tm_stock.o tm_utility.o
    $(CC) $(LFLAGS) tm.o tm_coins.o tm_options.o tm_stock.o tm_utility.o -o tm $(CFLAGS)

tm.o: tm.h tm.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm.c

tm_coins.o:tm_coins.h tm_coins.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_coins.c

tm_options:tm_options.h tm_options.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_options.c

tm_stock:tm_stock.h tm_stock.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_stock.c

tm_utility:tm_utility.h tm_utility.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_utility.c

使用上面的makefile,我得到以下错误。我的理解是 -std=c99 和 -lm 不起作用。(看看下面的第一行。-std=c99 和 -lm 不存在)

gcc -ansi -Wall -pedantic   -c -o tm_options.o tm_options.c
tm_options.c: In function ‘purchase_ticket’:
tm_options.c:37: error: expected expression before ‘/’ token
tm_options.c:52: error: expected expression before ‘/’ token
tm_options.c:102: warning: ISO C90 forbids mixed declarations and code
tm_options.c: In function ‘display_tickets’:
tm_options.c:239: error: expected expression before ‘/’ token
tm_options.c: In function ‘add_ticket’:
tm_options.c:285: error: expected expression before ‘/’ token
tm_options.c:303: error: expected expression before ‘/’ token
tm_options.c:314: warning: ISO C90 forbids mixed declarations and code
tm_options.c: In function ‘delete_ticket’:
tm_options.c:387: error: expected expression before ‘/’ token
tm_options.c:405: error: expected expression before ‘/’ token
tm_options.c: In function ‘display_coins’:
tm_options.c:461: error: expected expression before ‘/’ token
tm_options.c: In function ‘restock_tickets’:
tm_options.c:501: error: expected expression before ‘/’ token
tm_options.c: In function ‘restock_coins’:
tm_options.c:526: error: expected expression before ‘/’ token
tm_options.c: In function ‘save_data’:
tm_options.c:555: warning: ISO C90 forbids mixed declarations and code

错误会在哪里?提前致谢。

4

2 回答 2

3

对于以下三个规则,您忘记了.o目标上的后缀:

tm_options:tm_options.h tm_options.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_options.c

tm_stock:tm_stock.h tm_stock.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_stock.c

tm_utility:tm_utility.h tm_utility.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_utility.c

这就是为什么你写的规则不适用的原因。相反,它回退到将.c源代码编译成.o.

你的意思是包括它,像这样:

tm_options.o:tm_options.h tm_options.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_options.c

tm_stock.o:tm_stock.h tm_stock.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_stock.c

tm_utility.o:tm_utility.h tm_utility.c
    $(CC) $(LFLAGS) $(CFLAGS) -c tm_utility.c

我建议重构你的makefile,这样你就有一个通用的编译.c规则.o

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

然后,您只需要指定文件的依赖项:

tm_options.o: tm_options.h tm_options.c
tm_stock.o: tm_stock.h tm_stock.c
tm_utility.o: tm_utility.h tm_utility.c

并且自动应用上述规则。将您的 makefile 重构为此将更容易发现错误:)

于 2013-11-14T11:22:43.343 回答
3

无论如何,您都不希望LFLAGS在编译命令-std=c99中-放入CFLAGS-makefile应该更像这样:

CFLAGS = -ansi -Wall -pedantic -std=c99
LFLAGS = -lm 
CC = gcc

all: tm

tm: tm.o tm_coins.o tm_options.o tm_stock.o tm_utility.o
    $(CC) $(LFLAGS) tm.o tm_coins.o tm_options.o tm_stock.o tm_utility.o -o tm

tm.o: tm.h tm.c
    $(CC) $(CFLAGS) -c tm.c

tm_coins.o: tm_coins.h tm_coins.c
    $(CC) $(CFLAGS) -c tm_coins.c

tm_options.o: tm_options.h tm_options.c
    $(CC) $(CFLAGS) -c tm_options.c

tm_stock.o: tm_stock.h tm_stock.c
    $(CC) $(CFLAGS) -c tm_stock.c

tm_utility.o: tm_utility.h tm_utility.c
    $(CC) $(CFLAGS) -c tm_utility.c

(我还添加了 Magnus 指出的缺失的 .o 后缀。)

于 2013-11-14T11:23:06.903 回答