I have two files with identical C code. I’m compiling one using Make and one using GCC directly (gcc NAME.c -o NAME).
In the GCC-compiled program, all fprintf statements work fine. In the Make-compiled program, only the fprintf statements in the if statements work. The other ones don’t print anything. I haven’t been able to figure it why.
The code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
int main(int argc, char ** argv) {
fprintf(stdout, "test\n");
if (argc != 2) {
fprintf(stderr, "You must have one argument: filename or -h\n");
return 1;
}
if (strcmp(argv[1], "-h") == 0) {
fprintf(stdout, "HELP\n"); /*ADD TEXT HERE*/
}
fprintf(stdout, "got to the end\n");
return 0;
}
My makefile:
COMPILER = gcc
CCFLAGS = -ansi -pedantic -Wall
all: wordstat
debug:
make DEBUG = TRUE
wordstat: wordstat.o
$(COMPILER) $(CCFLAGS) -o wordstat wordstat.o
wordstat.o: wordstat.c
$(COMPILER) $(CCFLAGS) wordstat.c
clean:
rm -f wordstat *.o
The GCC one (run with -h) outputs:
changed text
HELP
got to the end
The Make one outputs:
HELP
Any help would be much appreciated.
You forgot the -c option in the makefile:
Else this line doesn’t generate an object file but an executable elf file (a.out), and thus may lead to unexpected behavior because you recompile that to wordstat ( and it is already compiled).