【发布时间】: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.h 和helper.c 中定义的方法来完成程序。知道有什么问题吗?我已经束手无策了。
【问题讨论】:
-
为什么
<DESTINATION>\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 行,因此您没有向我们展示您的实际代码。我们无法调试我们看不到的代码。