-4

I have the following C code:

#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 is defined as follows:

#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.
*/

When I try to compile the above with the following command:

gcc -Wall -o program_name program_name.c

I get the following errors:

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

I've found that deleting the #include "helper.h" statement in the original program stops the compilation errors...but the problem is that I sort of need the methods defined in helper.h and helper.c to complete the program. Any idea what's wrong with it? I'm at my wit's end.

everybody0523
  • 152
  • 1
  • 8
  • Why is `\n");` on a separate line? You need to post code that compiles, and demonstrates the problem. See [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) in the [help center](https://stackoverflow.com/help). – user3386109 Mar 27 '18 at 22:14
  • That was a typo from when I copied it over from my IDE to StackOverflow - its not that way in my code. I'll edit that. – everybody0523 Mar 27 '18 at 22:16
  • 1
    In some older iterations of C, you had to have variable declarations before the code. Are you compiling under those rules? – Aric TenEyck Mar 27 '18 at 22:16
  • Where is 'target' used? – ulmer-a Mar 27 '18 at 22:18
  • No, I'm compiling with C99. I've tried doing the variable declarations at the top as well, to no effect. – everybody0523 Mar 27 '18 at 22:18
  • I haven't used target in the code at all yet (I commented the rest of the program). – everybody0523 Mar 27 '18 at 22:19
  • 3
    This scenario typically means that there's a syntax error, e.g. a missing semicolon, in the `bunch of other stuff`. One approach is to comment out half of the other stuff, and see if the error goes away. Continue with the binary search until you find the line with the error. – user3386109 Mar 27 '18 at 22:20
  • I just tried deleting everything in helper.h (and deleting the contents of helper.c) except for the `#include ` statements. It still fails to compile, with the same error. I should also add that I have a second program that also uses helper.h, and the original program compiles and executes just fine, so I doubt its that. – everybody0523 Mar 27 '18 at 22:27
  • You're not using C99--you have to add "-std=c99" to the gcc command to do that. Then it will accept the intermingled code and declarations. – Lee Daniel Crocker Mar 27 '18 at 22:29
  • 1
    I don't think it's a C90 vs. C99 issue. That would cause a different error message, something like "warning: ISO C90 forbids mixed declarations and code". (Recent versions of gcc default to `-std=gnu11`.) We need a [mcve]. I'll note that there is no line 19 in the code you've shown us, so you're not showing us your actual code. We can't debug code we can't see. – Keith Thompson Mar 27 '18 at 22:32
  • What version of gcc are you using? Use `gcc -std=c99` to be sure you're using C99 mode (or `gcc -std=gnu99` if you're using gcc-specific extensions, or `c11` or `gnu11` if you want the latest C standard). – Keith Thompson Mar 27 '18 at 22:34

1 Answers1

1

I resolved the issue.

One of the constants in helper.h was called FILE, which was apparently causing issues. Thanks for the help, everyone!

everybody0523
  • 152
  • 1
  • 8