【问题标题】:C Errors with FILE* [closed]FILE *的C错误[关闭]
【发布时间】:2018-03-27 22:11:36
【问题描述】:

我有以下 C 代码:

#include <stdio.h>
#include "helper.h"

int main(int argc, char ** argv){
    if (argc < 4){
    fprintf(stderr, "Usage: ./program_name <DISK> <LOCAL_FILE> <DESTINATION> \n");
    exit(1);
    }

    FILE * target; 
}

helper.h 定义如下:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
/*
A bunch of function headers and global variables below, none of which 
are called target.
*/

当我尝试使用以下命令编译上述内容时:

gcc -Wall -o program_name program_name.c

我收到以下错误:

program_name.c:19:12: error: use of undeclared identifier 'target'
    FILE * target;
           ^
1 error generated.

我发现删除原程序中的#include "helper.h" 语句会停止编译错误...但问题是我有点需要helper.hhelper.c 中定义的方法来完成程序。知道有什么问题吗?我已经束手无策了。

【问题讨论】:

  • 为什么&lt;DESTINATION&gt;\n"); 在单独的一行?您需要发布可编译并演示问题的代码。请参阅help center 中的Minimal, Complete, and Verifiable example
  • 当我将它从我的 IDE 复制到 StackOverflow 时,这是一个错字 - 在我的代码中不是这样。我会编辑它。
  • 在一些较旧的 C 迭代中,您必须在代码之前声明变量。您是否按照这些规则进行编译?
  • 这种情况通常意味着存在语法错误,例如bunch of other stuff 中缺少分号。一种方法是注释掉其他内容的一半,然后查看错误是否消失。继续二分查找,直到找到错误的行。
  • 我不认为这是 C90 与 C99 的问题。这将导致不同的错误消息,例如“警告:ISO C90 禁止混合声明和代码”。 (最新版本的 gcc 默认为-std=gnu11。)我们需要一个minimal reproducible example。我会注意到您向我们展示的代码中没有第 19 行,因此您没有向我们展示您的实际代码。我们无法调试我们看不到的代码。

标签: c gcc stdio


【解决方案1】:

我解决了这个问题。

helper.h 中的一个常量称为 FILE,这显然会导致问题。谢谢大家的帮助!

【讨论】:

  • 是的,这肯定会引起问题。
猜你喜欢
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多