fprintf无法在使用Make编译的C程序中工作

时间:2013-02-14 02:18:12

标签: c makefile printf

我有两个具有相同C代码的文件。我正在使用Make和一个直接使用GCC编译一个(gcc NAME.c -o NAME)。

在GCC编译的程序中,所有fprintf语句都可以正常工作。在Make-compiled程序中,只有if语句中的fprintf语句有效。其他的不打印任何东西。我无法理解为什么。

代码是:

#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;                            
}

我的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

GCC one(以-h运行)输出:

changed text
HELP
got to the end

制作一个输出:

HELP

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你忘记了makefile中的-c选项:

.
.
.    
wordstat.o: wordstat.c  
    $(COMPILER) $(CCFLAGS) -c wordstat.c
                            ↑ - important!

否则这行不会生成目标文件,而是生成可执行的elf文件(a.out),因此可能会导致意外行为,因为您将其重新编译为wordstat(并且已经编译)。