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

Programming, for all ages and all languages.
Post Reply
User avatar
pvc
Member
Member
Posts: 201
Joined: Mon Jan 15, 2018 2:27 pm

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

Post 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?
Last edited by pvc on Mon Nov 02, 2020 5:52 am, edited 1 time in total.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post 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.
Every good solution is obvious once you've found it.
User avatar
pvc
Member
Member
Posts: 201
Joined: Mon Jan 15, 2018 2:27 pm

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

Post 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.
Post Reply