0

所以我在程序的开头有一个头文件和 2 个 .c 文件。我去编译,我收到错误消息(一遍又一遍)

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

现在我不在我的程序中使用静态关键字 ANYWHERE ......那么为什么 GCC 会认为我已经声明了一个静态函数?

以下是 .h 和 .c 文件中 read_args_file 声明的相关代码:

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)) {
.....
}

编辑:

整个 .h 文件是:

#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

command_parser.c 文件直到函数定义为:

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)) {
4

1 回答 1

5

.h 文件第 8 行中有一个大括号打开:void switch_parsing(int argc, ..... ){

后面的行被编译器当作一个大的函数体,编译器找不到匹配的'}'后会发现最终的错误。很多行(和文件)之后。OP 很幸运:编译器首先发现了另一个(语义)错误。

于 2012-10-27T19:28:38.387 回答