0

So I have a header file and 2 .c files within the beginnings of my program. I go to compile and I get the error message (tons of these over and over)

command_parser.c:74:6: error: static declaration of ‘read_args_file’ follows non-static     declaration
command_parser.h:9:6: note: previous declaration of ‘read_args_file’ was here

Now I do not use the static keyword ANYWHERE in my program...so why would GCC go and think that I've declared a static function???

Below is the relevant code for read_args_file's declaration in the .h and .c files:

void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out));

void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out)) {
.....
}

EDIT:

The entire .h file is:

#ifndef COMMAND_PARSER_H_
#define COMMAND_PARSER_H_

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* line 8 follows: */
void switch_parsing(int argc, char* argv[], int (*command_read)(char* command, FILE* out), char* (*pr    int_usage)()) {
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command,     FILE* ou    t));
void read_args_input(int (*command_read)(char* command, FILE* out));

#endif

The command_parser.c file until the function definition is:

void switch_parsing(int argc, char* argv[], int (*command_read)(char* command, FILE* out), char* (*print_usage)()) {

    char* arg;
    char* return_string;
    char* wrong_string = "Please enter either -i, -h, or -f as a switch. Use -h for help.\n";
    char* invalid_f_args = "You entered an invalid number of arguments for the -f switch! Only two are permitted, <commands_file> and <output_file>.\n";
    int str_len = 0;

    char cur;

    if (argc > 1) {
        arg = argv[1];
    }
    else {
        arg = "\0";
    }

    str_len = strlen(arg);
    if (str_len == 2) {
        if (arg[0] == '-') {
            cur = arg[1];
            if (cur == 'i') {
                read_args_input(command_read);
                return_string = "";
            }
            else if (cur == 'f') {
                if (argc == 4) {
                    read_args_file(argv[2], argv[3], (*command_read));
                    return_string = "";
                }
                else {
                    return_string = invalid_f_args;
                }
            }
            else if (cur == 'h') {
                return_string = print_usage();
            }
            else {
                return_string = "The switch ";
                return_string = strcat(return_string, &cur);
                return_string = strcat(return_string, " is an invalid switch.\n");
            }
        }
    }
    else if (str_len == 1) {
        return_string = wrong_string;
    }
    else if (str_len > 2) {
        return_string = wrong_string;
    }
    else if (str_len == 0) {
        return_string = print_usage();
    }
    else {
        return_string = wrong_string;
    }
}

/**
 * Reads arguments from a passed in file name, and writes the output from the commands
 * in the file to the out_file_name. Arguments are run through command_read function
 * passed in to be executed.
 */
/* line 74 follows: */
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out)) {
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Ethan
  • 1,206
  • 3
  • 21
  • 39
  • 2
    What does the code look like *before* the declaration and the definition? Do you have a stray definition or hidden macro somewhere? – Adam Rosenfield Oct 27 '12 at 18:53
  • I invoke the function before the definition in another function. However I include the .h file with the declaration at the top read_args_file(argv[2], argv[3], (*command_read)); Also I should note that I'm a bit new to function pointers, so I may be passing them incorrectly. I had the code above as read_args_file(argv[2], argv[3], command_read); before, and I get the exact same output – Ethan Oct 27 '12 at 18:57
  • Which is command_parser.c line 74? – aschepler Oct 27 '12 at 18:58
  • @KingsIndian, that is not the case, it's included – Ethan Oct 27 '12 at 18:59
  • @aschepler line 74 is the function definition, so the second line of code above – Ethan Oct 27 '12 at 18:59
  • 4
    The error message says that the definition at command_parser.c line 74 is a static definition (as far as the compiler can see). What is on line 74 **and the lines immediately preceding it**? – hmakholm left over Monica Oct 27 '12 at 19:00
  • The problem must be somewhere on lines 68 to 73. – hmakholm left over Monica Oct 27 '12 at 19:08
  • added them....they're comments – Ethan Oct 27 '12 at 19:10
  • Seems the two files in question are not extremely large - maybe you can post files that will repro the problem on other machines? – Michael Burr Oct 27 '12 at 19:13
  • There is little chance this is the compiler's fault. Just post the entire files. – netcoder Oct 27 '12 at 19:13
  • 2
    There is a brace open in the .h file line 8: `void switch_parsing(int argc, ..... ){` – wildplasser Oct 27 '12 at 19:16
  • well damn me for using vim....and not an IDE that sees this......that was the problem -_- if you'd like to post that as an answer wildplasser I'll mark it as correct – Ethan Oct 27 '12 at 19:18
  • 1
    What's with the extra spaces in the header file in the lines `char* (*pr int_usage)` and `FILE* ou t`? Is that your actual code or a copy+paste error? – Adam Rosenfield Oct 27 '12 at 19:20
  • Note that your header should include `` since it references the type `FILE *`; the other two headers are not needed to use the header, and should ideally be omitted. See also the `chkhdr` script in [Linking against a static library](http://stackoverflow.com/questions/4507896/linking-against-a-static-library/4514024#4514024) which I use to validate that headers are complete. It would have diagnosed this problem. (I used it yesterday to find out which functions were defined twice in two headers, using GCC's `-Wredundant-decls` to tell me.) – Jonathan Leffler Oct 27 '12 at 19:38
  • For future reference, please post code *without* line numbers so we can copy-and-paste it if necessary. You can add comments on relevant lines; for example, if a message refers to line 42, you can add a `/* line 42 */` comment. I've just edited it. It's also very helpful to post complete self-contained code that reproduces the problem. – Keith Thompson Oct 27 '12 at 20:08

1 Answers1

5

There is a brace open in the .h file line 8: void switch_parsing(int argc, ..... ){

The lines that follow are treated by the compile as one big function body, and the final error will be found after the compiler fails to find a matching '}'. Many lines (and files) later. The OP got lucky: the compiler first found another (semantic) error.

wildplasser
  • 43,142
  • 8
  • 66
  • 109