简体   繁体   English

GCC makefile不接受-std = c99 -lm

[英]GCC makefile doesn't accept -std=c99 -lm

I am having problem with my makefile with gcc compiler. 我的gcc编译器makefile文件出现问题。 If I use gcc directly as: 如果我直接将gcc用作:

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

Everything works fine. 一切正常。 I need -std-c99 and -lm. 我需要-std-c99和-lm。

However, I have been told to use makefile. 但是,有人告诉我使用makefile。 Here is my make file: 这是我的make文件:

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

With above makefile I get below errors. 使用上面的makefile,我得到以下错误。 My understanding is that -std=c99 and -lm doesn't work. 我的理解是-std = c99和-lm不起作用。 (look at the first line below. -std=c99 and -lm are not there) (请看下面的第一行。-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

Where would be the error? 错误在哪里? Thanks in advance. 提前致谢。

For the following three rules, you have forgotten the .o suffix on your targets: 对于以下三个规则,您忘记了目标上的.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

This is why the rules you have written do not apply. 这就是为什么您编写的规则不适用的原因。 Instead, it falls back to the default rule for compiling .c sources into .o . 而是将其恢复为将.c源编译为.o的默认规则。

You meant to include it, like so: 您打算包括它,如下所示:

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

I recommend refactoring your makefile, so you have a generic rule for compiling .c to .o : 我建议重构您的makefile,因此您具有将.c编译为.o的通用规则:

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

Then, you only need to specify the dependencies for your files: 然后,您只需要指定文件的依赖关系:

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

and the above rule is applied automatically. 并自动应用上述规则。 Refactoring your makefile to this will make it easier to spot errors :) 将您的makefile重构为此将使发现错误更容易:)

You don't want LFLAGS in the compile commands anyway - put -std=c99 in CFLAGS - the makefile should be more like this: 无论如何,您都不希望在编译命令中使用LFLAGS-std=c99放入CFLAGS LFLAGS应该更像这样:

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

(I've also added the missing .o suffixes as pointed out by Magnus.) (我还添加了Magnus指出的缺少的.o后缀。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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