'main'通常是一个非静态函数ERROR

时间:2014-07-07 11:41:23

标签: c linker getopt

出现两个错误: main.c:80:警告:'main'通常是一个非静态函数 main.c:88:错误:输入结束时的预期声明或语句  我似乎无法找到问题......花括号的数量相等......似乎有什么问题?

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include "main-getopt.h"


void print_usage_and_abort( const char *message )
{
    if( NULL != message )
        fprintf( stderr, "Error: %s\n", message );

    fprintf( stderr, "Usage: partitioner -n <nodes> [ -f <basename> ]\n\n" );
    exit( -1 );
}

void parsing (int argc, char **argv, struct Params *params)
{
    char error_message[256];

    params->nodes = 0;
    memcpy( params->filename_base, "output", strlen("output") + 1 );

    int opt;
    size_t len;
    int numarg;

    while ((opt = getopt(argc, argv, "n:f:")) != -1) {
        int i;
        for (i = 1; i < argc; i++)
        {
            if (argv[i][0] == '-')
            {
                if (i+1 == argc || argv[i+1][0] == '-')
                {
                    sprintf( error_message, "No Filename");
                    print_usage_and_abort( error_message );
                }
                if (argv[i][1] == 'n')
                {
                    numarg = atoi( optarg );
                    if( numarg < 1 || numarg > 2048 )
                    {
                        sprintf( error_message, "Number of nodes agrument expects a "
                             "number between 1 and 2048, actual %s", optarg );
                        print_usage_and_abort( error_message );
                    }
                }
                else if (argv[i][1] == 'f')
                    len = strlen( optarg );

                // limit to buffer capacity
                if( len >= MAX_FILENAME_BASE )
                {
                    sprintf( error_message, "Base filename parameter length is "
                        "expected to be less than %d but is %d",
                        (int)MAX_FILENAME_BASE, (int)len );
                    print_usage_and_abort( error_message );
                }
                else if(len<MAX_FILENAME_BASE)
                {
                    memcpy( params->filename_base, optarg, len + 1 );
                    break;
                }
                else
                {
                    sprintf( error_message, "Unknown command switch %c", (char)optopt );
                    print_usage_and_abort( error_message );
                    break;
                }
            }

        }
        if( 0==params->nodes )
        {
            sprintf( error_message, "Number of nodes switch -n is required" );
            print_usage_and_abort( error_message );
        }
    }

int main(int argc, char *argv[])
{
    struct Params params;
    parse_arguments( argc, argv, &params );

    fprintf( stdout, "Parameters are:\n\tNumber of nodes:\t%d\n\t" 
        "Filename base:\t%s\n\n", params.nodes, params.filename_base );

    return 0;
}

1 个答案:

答案 0 :(得分:6)

我为你编辑了缩进。你现在看到parsing函数中某处你错过了一个结束括号吗?