Donald Duck Donald Duck - 3 months ago 11
C Question

Make a preprocessor error at a specific line

I'm making a cross-platform program for Windows, Unix and Mac. To be able to use things that are specific for each OS, I use a

#define
, for example when I'm compiling for Windows, I use
#define WINDOWS
. To make sure not to define an OS that doesn't exist, or at least that my program doesn't support, I use this:

#ifndef WINDOWS
#ifndef UNIX
#ifndef MAC
#error "OS must be WINDOWS, UNIX or MAC"
#endif
#endif
#endif


When I define an invalid OS, is gives me an error as it should, but it puts the red rectangle that means error in front of the
#error
line. I would like to put it in front of the line where I defined the OS. For example, if I'm supposed to define the OS on line 11, I would like it to send me to line 11. Is there a way to do this?

Answer

If you would like to show an error on specific line if there is no defined macros on your code, try to use #line

1 #ifndef UNIX
2 #ifndef MAC
3 #line 11 
4 #error "OS must be WINDOWS, UNIX or MAC" 
5 #endif 
6 #endif 
7 #endif 
8    
9  int main ()
10 {
11   // here will be error from line 4
12 }
Comments