Make ignores -std = c99 flag when compiling and linking a C program

I tried to get a makefile to compile a file that requires -std=c99 to -std=c99 . In this case, to get an end-to-end loop.

This is my code (it was used "tab" before "$ (CC)"):

 CC = gcc CFLAGS = -c -std=c99 ... Download.o : Download.c $(CC) $(CFLAGS) Download.c 

Download.c contains methods used to download items from the Internet.

Error message

 $ make gcc -c -std=c99 Download.c gcc Download.c -o Program Download.c: In function 'downloadImageparts': Download.c:11:2: error: 'for' loop initial declarations are only allowed in C99 mode Download.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code Download.c:13:3: error: 'for' loop initial declarations are only allowed in C99 mode make: *** [comp] Error 1 

Attemt for debugging

If I run gcc -c -std=c99 Download.c in the terminal, it works fine.

These problems occur when running on Linux.

SOLVE:

I created a dummy project to show my lecturer, trying to solve my problem. In a fictitious project, everything works fine with the described code. For some reason, my code works in place, but not in another. If someone is reading this with the same problem as me, and would like to see an example project. let me know and i will write the code here. Thanks

+4
source share
2 answers

You are looking at the wrong rule. Download.c actually compiles fine, but the build phase is wrong.

  $ make
 gcc -c -std = c99 Download.c # Compile
 gcc Download.c -o Program # Link

Correct the make rule that binds the program. This should probably look something like this:

 Program: ao bo co $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) 

While you're on it, I suggest that a more complete Makefile would look something like this:

 all: Program clean: rm -f Program *.o .PHONY: all clean # -c is implicit, you don't need it (it *shouldn't* be there) # CC is also implicit, you don't need it CFLAGS := -std=c99 -g -Wall -Wextra Program: ao bo co $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) # Make will automatically generate the necessary commands # You just need to name which headers each file depends on # (You can make the dependencies automatic but this is simpler) ao: ac header.h bo: bc header.h header2.h co: cc header.h 

Examples of how to do it wrong

Linker flags are actually quite touching! Be sure to enter the line above, just like I wrote it, and don’t assume that what you wrote is equivalent. Here are some examples of incorrect commands, not :

 # WRONG: program must depend on *.o files, NOT *.c files Program: ac bc cc $(CC) ... # WRONG: -c should not be in CFLAGS CFLAGS := -c -std=c99 Program: ao bo co # WRONG: $(CFLAGS) should not be here # you are NOT compiling, so they do not belong here $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) # WRONG: $(LIBS) MUST come at the end # otherwise linker may fail to find symbols $(CC) $(LDFLAGS) -o $@ $(LIBS) $^ # WRONG: do not list *.o files, use $^ instead # otherwise it is easy to get typos here $(CC) $(LDFLAGS) -o $@ ao bo co $(LIBS) # WRONG: $(LDFLAGS) must be at the beginning # it only applies to what comes after, so you # MUST put it at the beginning $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) # WRONG: -c flag disables linking # but we are trying to link! $(CC) $(LDFLAGS) -c -o $@ $^ $(LIBS) # WRONG: use $(CC), not gcc # Don't sabotage your ability to "make CC=clang" or "make CC=gcc-4.7" gcc $(LDFLAGS) -o $@ $^ $(LIBS) # WRONG: ld does not include libc by default! ld $(LDFLAGS) -o $@ $^ $(LIBS) 
+11
source

I see the same results if I use spaces instead of tabs inside the makefile, and the output of make shows that the rule is not used:

 $ make cc -c -o Download.o Download.c Download.c: In function 'main': Download.c:4:3: error: 'for' loop initial declarations are only allowed in C99 mode Download.c:4:3: note: use option -std=c99 or -std=gnu99 to compile your code make: *** [Download.o] Error 1 

Try tab before starting a line with gcc .


After watching the update of the original question:

 $ make gcc -c -std=c99 Download.c gcc Download.c -o Program 

The first line (compilation) does not show errors.
This is the second line that recompiles Download.c , which fails.
I think you want to link the .o files to create an executable program here, as Dietrich Epp suggests.

0
source

Source: https://habr.com/ru/post/1445291/


All Articles