GCC makefile doesn't accept -std=c99 -lm

蹲街弑〆低调 提交于 2020-01-15 08:36:11

问题


I am having problem with my makefile with gcc compiler. If I use gcc directly as:

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.

However, I have been told to use makefile. Here is my make file:

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. My understanding is that -std=c99 and -lm doesn't work. (look at the first line below. -std=c99 and -lm are not there)

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.


回答1:


For the following three rules, you have forgotten the .o suffix on your targets:

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.

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:

%.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 :)




回答2:


You don't want LFLAGS in the compile commands anyway - put -std=c99 in CFLAGS - the makefile should be more like this:

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.)



来源:https://stackoverflow.com/questions/19976302/gcc-makefile-doesnt-accept-std-c99-lm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!