Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to #include a single line in C

Tags:

c++

c

gcc

Apperantly rumor has it that it is possible to #include a single line in C (possibly with gcc)

e.g.

#include <file.h> 5

To get like line 5. Cannot find anything like that in the cpp docs.

Or which preprocessors allow doing ugly hacks like that?!

like image 635
Dima Avatar asked Dec 08 '11 11:12

Dima


2 Answers

I've never heard of such a feature, and I have certainly not come across any preprocessors that would support it.

In any case, it sounds like a really bad idea. What if someone inserted a blank line at the top of file.h... would you be happy for that change to break your program?

If you control file.h, I think the best approach is to extract line 5 into a separate header, and #include it both into file.h and into your main program.

like image 136
NPE Avatar answered Sep 28 '22 23:09

NPE


I don't think that's possible with GCC or any other compiler for that matter.

like image 31
Pablo Santa Cruz Avatar answered Sep 29 '22 00:09

Pablo Santa Cruz