A preprocessor macro to delete code if compiled after a specific date

I would like the three lines of code not to be included if they are compiled after a certain date. The reason is that they provide backward compatibility. In order to maintain a distributed release between the client and the investment, it must be there now.

As soon as the next release of the software arrives, this support must be removed in order to force clients to update the firmware. Since this is a few months ago, there is a risk that these lines will be forgotten.

Ideally, I would like

#if __DATE__ > MyDate code here #endif 

or something similar. Is there any way to do this?

* Code compiled using GCC

+4
source share
4 answers

This solution is specifically for the Windows platform, and I use it in production.

I use the% DATE% environment variable, and in the batch file used to run my IDE, I have VA_CURRENT_DATE =% DATE: ~ 6.4 %% DATE: ~ 3.2 %% DATE: ~ 0.2% (which converts to ISO8601 date for my specific locale).

Then in my preprocessor definitions for my project, I define VA_BUILD_DATE on VA_CURRENT_DATE

Then I have code like:

 long day = VA_BUILD_DATE; long year = day / 10000; day -= year * 10000; long month = day / 100; day -= month * 100; 
+5
source

You cannot do this with __DATE__ because it expands to a string constant, and string constants cannot be used in #if . Also, setting a fixed date is a bad idea, because you may need to make bug fixes in an older version, which should preserve backward compatibility.

(Do you really need to give up backward compatibility? If these are just three lines of code, consider keeping them forever. Your customers will not thank you for β€œforcing them to be updated.”)

A good way to do this is through a version control system. You must maintain a branch for each version, so write your code as follows:

 #ifdef BACKWARD_COMPAT_VERSION_1_0 compatibility code here #endif 

and then modify the Makefile only in the release branches, including -DBACKWARD_COMPAT_VERSION_1_0 in your CFLAGS.

+3
source

Now I risk not answering your question directly. But I took a chance and suggest you DO NOT do this. How many times is a project issued on time? Date is too subject to change. And you never know that.

Why don't you use the version of your project?

 // Only defined in old projects that you want this legacy code in. #ifdef OLD__VERSION code here #endif 
+2
source

Unfortunately, this will not work, as __DATE__ creates a line in the form of "Sep 5 2013" , which is useless for comparison.

Ideally, the compiler should support a constant like __DATEFLAT__ , which produces an integer like 20130905 , which is ideal for the task. However, this does not exist.

+1
source

Source: https://habr.com/ru/post/1500746/


All Articles