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:
? My ultimate goal is to try to make my `tests` into a macro to reduce the clutter of my programs.