Page 1 of 1

[Solved]Is there a way for GCC to ignore invalid directives?

Posted: Mon Nov 02, 2020 4:15 am
by pvc
Hi, I want GCC NOT to error out on invalid directive.
I need some C code to be runnable from command line as scripts. I am using TCC's (Tiny C Compiler) -run option, which allows to do just that. To simplify some Shell scripts (some of these C scripts need special invocation) I am using interpreter line inside the script. Something like:

Code: Select all

#!/usr/bin/tcc -run

#include <stdio.h>

int main()
{
    printf("Trolololo!\n");
    return 0;
}
The problem is that it's difficult to debug code started this way. From time to time I need to properly compile and run such script with GCC and debug with GDB. But whenever GCC encounters first line, it errors out, complaining about invalid directive. For now I just comment out first line whenever I am using GCC, but sometimes I forget to uncomment it before commiting the code to repository and this causes some breakage in my Shell scripts. Is there some way to tell GCC to ignore first line or invalid directives?

Re: Is there a way for GCC to ignore invalid directives?

Posted: Mon Nov 02, 2020 4:27 am
by Solar
Not to my knowledge. And IMHO it would be the wrong way to solve this problem.

You talk about "forgetting to uncomment before comitting". That is where I would catch this kind of error: By means of a pre-commit check. All modern version control systems allow this kind of thing. Write a script that checks the first line for a commented-out interpreter directive, and have it reject the commit with a helpful error message.

Re: Is there a way for GCC to ignore invalid directives?

Posted: Mon Nov 02, 2020 5:51 am
by pvc
It turns out GCC can accept input from stdin. So I just remove offending line with sed. No need for temporary files or anything.