Thread: Using argc without argv. (-Wfatal-errors)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    121

    Using argc without argv. (-Wfatal-errors)

    Hi,

    I'm starting to notice how much I reuse certain code. For instance, this is a common `test` in many of my programs:
    Code:
    if(SOME_TEST == FAILED)
    {
        fprintf(stderr, "%s: FUNCTION_NAME error: LAST_COMMAND failed (%s)\n", program_name, strerror(errno));
        exit(EXIT_FAILURE);
    }
    I recently learned about some gcc variables, namely `program_invocation_short_name` which give the basename of the called program, and `__func__` which gives the name of the called function.

    Now, `program_invocation_short_name` is useful to me as in the main function of every program, I do the following to get the program name (`program_name` being a global variable):
    Code:
    int main (int argc, char *argv[])
    {
    // Get program name for error reporting.
        strcpy(program_name, basename(argv[0]));
    
    // Check number of arguments.
        if(argc != 1)
        {
            fprintf(stderr, "Usage: %s\n", program_name);
            exit(EXIT_FAILURE);
        }
    
    // Run process.
        run_process();
    
    // Exit cleanly.
        exit(EXIT_SUCCESS);
    }
    So using `program_invocation_short_name` stops me from having a global variable and I don't have to load `libgen.h`. The problem I'm running into is needing to test the number of variables from the command line, but no longer needed any of the `argv` arguments.

    I also try to program with a lot of gcc flags to make my programming better. But the `-Wfatal-errors` flag won't allow unused variables. If I don't use `basename(argv[0])` to get the program name, the compiler complains that `argv` is unused. If I try to remove just the `argv` declaration from the main function, then gcc complains with a lie to me:
    Code:
    dea_template.c:28:5: error: ‘main’ takes only zero or two arguments [-Werror=mai
       28 | int main (int argc)
          |     ^~~~
    compilation terminated due to -Wfatal-errors.
    cc1: all warnings being treated as errors
    I say that what gcc reports is a lie, because the following generates zero errors:
    Code:
    int main(int argc, char * argv[], char *envp[])
    So is there a way to stop the compiler from complaining without removing the `-Wfatal-errors` flag or by adding:
    Code:
    if(argv[0])
        {;}
    ? My ultimate goal is to try to make my `tests` into a macro to reduce the clutter of my programs.
    Last edited by Yonut; 07-15-2020 at 11:12 AM. Reason: Fixed code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv and argc
    By antros48 in forum C Programming
    Replies: 19
    Last Post: 09-30-2011, 08:26 AM
  2. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 09:39 AM
  3. argc, **argv in C++.
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2007, 12:09 AM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM

Tags for this Thread