Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c preprocessor - fail if compiling after certain date

Tags:

c

macros

I would like to make the compilation of some files to fail if attempted after a certain date. The reason for this: I found a couple of Y2K38 bugs which I don't have time to fix right now, but would like to make a note of them and I think it would be nice if compilation of the module would just fail after, say, 2020. (I might be insane, but this code is 20 years old I suspect it might survive another 30)

like image 369
MK. Avatar asked Feb 02 '11 04:02

MK.


People also ask

Which can generate preprocessor error?

The directive ' #error ' causes the preprocessor to report a fatal error. The tokens forming the rest of the line following ' #error ' are used as the error message. The directive ' #warning ' is like ' #error ', but causes the preprocessor to issue a warning and continue preprocessing.

What is preprocessor error in C?

#error is a preprocessor directive in C which is used to raise an error during compilation and terminate the process. Syntax: #error <error message> C. Usually, the error message is associated with preprocessor conditional statements like #if, #ifdef and others.

What does the #error directive do?

A preprocessor error directive causes the preprocessor to generate an error message and causes the compilation to fail. The #error directive is often used in the #else portion of a #if – #elif – #else construct, as a safety check during compilation.

What does ## mean in C preprocessor?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.


2 Answers

With GCC, you can do something like the following:

void __attribute__((error("Whoa. It's the future"))) whoa_the_future();

void check_for_the_future() {
    // "Feb  1 2011"
    const char *now = __DATE__;
    if (now[9] >= '2')
        whoa_the_future();
}

The way this works is that the error attribute tells GCC to generate a compile-time error if any calls to that function are left in the code after all of GCC's constant-folding, dead-code elimination, and similar passes have run. Since DATE is a compile-time constant, GCC can evaluate the if statement at compile time and remove the call.

At least one downside is that this depends on GCC's optimization passes, and so it won't work at gcc -O0

Honestly, you might be better off just adding a runtime check somewhere and failing fast.

like image 199
nelhage Avatar answered Oct 28 '22 06:10

nelhage


Instead of dealing with the awkward format of the __DATE__ macro, why not roll your own?

gcc -DTHIS_YEAR=`/bin/date +%Y` yourprogram.c

Then your code can use expressions like #if THIS_YEAR >= 2020.

like image 39
dan04 Avatar answered Oct 28 '22 07:10

dan04