简体   繁体   English

Makefile -std = c99错误

[英]Makefile -std=c99 error

all: matrices.c calculations.o
    gcc -std=c99 matrices.c calculations.o -o -lm PROGRAM2_EXE

lib: matrices.h calculations.c  
    gcc -c -std=c99 calculations.c -o calculations.o

clean: 
    rm matrices.o calculations.o PROGRAM2_EXE

This is my makefile for my project. 这是我的项目的makefile。 Inside my matrices and my calculations.c there are multiple for loops. 在我的矩阵和Calculations.c中,有多个for循环。 The error it gives me is error: 'for' loop initial declarations are only allowed in c99 mode but i have my -std=c99 in my file. 它给我的错误是错误:'for'循环初始声明仅在c99模式下允许,但我的文件中有-std = c99。 My questions is 我的问题是

  1. How do i fix my makefile to run with the for loops? 我该如何修复我的makefile以与for循环一起运行?

You don't have a rule for calculations.o . 您没有任何calculations.o规则。 That means that the makefile does not specify how to generate calculations.o . 这意味着makefile没有指定如何生成calculations.o You only have a rule for generating all and lib . 您只有生成alllib的规则。

Gmake has some default rules. Gmake有一些默认规则。 One of them is that if you do not have a rule for filename.o then it is compiled using $(CC) $(CPPFLAGS) $(CFLAGS) -c filename.c . 其中之一是,如果您没有filename.o的规则,则使用$(CC) $(CPPFLAGS) $(CFLAGS) -c filename.c

Your make all line is: 您的make all行是:

all: matrices.c calculations.o

When you issue make or make all , gmake checks the dependencies first. 发出makemake all ,gmake首先检查依赖性。 matrices.c already exists so that's fine. matrices.c已经存在,所以很好。 But if calculations.o does not exist, then it decides it needs to build calculations.o . 但是,如果calculations.o不存在,那么它决定它需要建立calculations.o You didn't specify a rule for calculations.o , so the implicit rule is used, which doesn't have -std=c99 in it. 您没有指定一个规则calculations.o ,所以隐含规则使用,不具有-std=c99在里面。

Note that your lib rule is badly written. 请注意,您的lib规则写得不好。 All rules should either create a file of the corresponding name, or be declared as .PHONY . 所有规则都应该创建一个具有相应名称的文件,或者声明为.PHONY It seems as if you think that make should somehow deduce that it needs to do make lib if the calculations.o file does not exist, but that isn't how make works. 好像您认为make应当以某种方式推断出,如果calculations.o文件不存在, make lib要做make lib ,但这不是make起作用的方式。

To fix your problem , just change lib: to calculations.o: . 为了解决您的问题,只要改变lib:calculations.o: Also, it would be good style to fix the all line. 另外,固定all行将是一个好的样式。 The rule does not make all , it makes PROGRAM2_EXE , so: 该规则并不all ,而是PROGRAM2_EXE ,因此:

.PHONY: all
all: PROGRAM2_EXE

PROGRAM2_EXE: matrices.c calculations.o
    gcc -std=c99 matrices.c calculations.o -o PROGRAM2_EXE -lm

(Edit: as Chris Dodd points out, the -o switch must be immediately followed by the filename) (编辑:正如Chris Dodd所指出的, -o开关必须紧随文件名之后)

It would be better style to have a rule matrices.o that compiles matrices.c , and then a rule PROGRAM2_EXE: matrices.o calculations.o that links the two. 最好有一个规则matrices.o可以编译matrices.c ,然后有一个规则PROGRAM2_EXE: matrices.o calculations.o可以将两者链接在一起。

This action: 这个动作:

gcc -std=c99 matrices.c calculations.o -o -lm PROGRAM2_EXE

is telling gcc to produce an executable called -lm , which is probably not what you want. 告诉gcc生成名为-lm的可执行文件,这可能不是您想要的。

Since you have no rule to produce connections.o , make will use its default rule to build it from connections.c , which is likely something like (from gmake): 由于您没有产生connections.o规则,因此make将使用其默认规则从connections.c进行构建,这很可能类似于(来自gmake):

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

Since you don't set -std=c99 in CFGLAGS or CPPFLAGS , it won't be used. 由于您没有在CFGLAGSCPPFLAGS设置-std=c99 ,因此不会使用它。

I use the following CFLAGS: 我使用以下CFLAGS:

CFLAGS = -Wall -g -std=c99 CFLAGS =-墙-g -std = c99

It works very good for me. 对我来说很好。

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

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