2013-02-14 55 views
1

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

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

我生成文件:

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酮(使用-h运行)输出:

changed text 
HELP 
got to the end 

的制作一个输出:

HELP 

任何帮助将不胜感激。

+0

'make DEBUG = TRUE'不是正确的语法;它应该是'make DEBUG = TRUE'。但是像这样递归的'make'可能不是最好的方法。 – 2013-02-14 02:59:53

+0

感谢您的语法修复。我该怎么做呢? (我没有太多想法,我在做什么,我是一个完整的C/Make新手) – Spinfusor 2013-02-15 05:19:57

回答

0

你忘了在makefile -c选项:

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

否则这行不生成目标文件,但可执行ELF文件(a.out的),从而可能导致意外的行为,因为你重新编译到wordstat(它已经编译)。

+0

谢谢!这解决了它。 – Spinfusor 2013-02-15 05:01:13