August 1999

Determining compiler version

by Kent Reisdorph

If you are writing applications or components that must compile under different versions of C++Builder then you may need to determine the version of the compiler in order to conditionally compile sections of code. Each version of the compiler assigns a value to a macro called __BORLANDC__. You can test the version of the compiler by checking the value of this macro in your code. For example:

#if (__BORLANDC__ >= 0x530)
#pragma package(smart_init)
#endif

In this case, the #pragma package line is only compiled if the code is being compiled on C++Builder 3 or greater (C++Builder 1 does not use packages, of course). We use code like this to insure that the examples for the journal will compile under any version of C++Builder. You can easily find out the value of the __BORLANDC__ macro by running this code under various versions of C++Builder:

char buff[10];
sprintf(buff, "%x", __BORLANDC__);
Label1->Caption = buff;

Table A shows the values of the __BORLANDC__ macro for the current versions of C++Builder.
Armed with this information you can easily write both applications and components that work on all current versions of C++Builder. 

Table A: Values of __BORLANDC__ by compiler 
Value     Compiler version
0x520     C++Builder 1
0x530     C++Builder 3
0x540     C++Builder 4