M
M
Mingun2014-10-09 07:08:15
Software porting
Mingun, 2014-10-09 07:08:15

Portable macro to get C++ compiler version string and application build time?

It is clear that there is no standard macro. You can build it yourself, but for this you need to know all the compiler-specifc macros (in general, it’s not difficult to find them) and it is desirable to be able to check them in operation.
I want the executable binary to have the version of the compiler in the form of a string.
Also interested in the same portable way to get the build time of the application, it’s probably more complicated here, because you need the linking time, not the compilation time. How to sew this information into a binary?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
EXL, 2014-10-09
@Mingun

We find a serious open source cross-platform program, I chose Qt Creator.
We go into the "About" dialog and see just what we need:
We find the construction of this dialog in the source code - [src/plugins/coreplugin/versiondialog.cpp | 77] .
We see that the trivial predefined macros __DATE__ and __TIME__ are used to get the build date and time , which are guaranteed to be predefined in all compilers.
But to find the compiler version, the special function ICore::buildCompatibilityString() is used . Let's follow the chain of calls: [src/plugins/coreplugin/icore.cpp | 472] .
In functionICore::buildCompatibilityString() uses a call to the static function compilerString() to get information about the compiler and QSysInfo::WordSize to get information about the bitness of the code generated by the compiler. compilerString()
function [src/plugins/coreplugin/icore.cpp | 440-458] looks like this:

static QString compilerString()
{
#if defined(Q_CC_CLANG) // must be before GNU, because clang claims to be GNU too
    QString isAppleString;
#if defined(__apple_build_version__) // Apple clang has other version numbers
    isAppleString = QLatin1String(" (Apple)");
#endif
    return QLatin1String("Clang " ) + QString::number(__clang_major__) + QLatin1Char('.')
            + QString::number(__clang_minor__) + isAppleString;
#elif defined(Q_CC_GNU)
    return QLatin1String("GCC " ) + QLatin1String(__VERSION__);
#elif defined(Q_CC_MSVC)
    if (_MSC_VER >= 1800) // 1800: MSVC 2013 (yearly release cycle)
        return QLatin1String("MSVC ") + QString::number(2008 + ((_MSC_VER / 100) - 13));
    if (_MSC_VER >= 1500) // 1500: MSVC 2008, 1600: MSVC 2010, ... (2-year release cycle)
        return QLatin1String("MSVC ") + QString::number(2008 + 2 * ((_MSC_VER / 100) - 15));
#endif
    return QLatin1String("<unknown compiler>");
}

Macros Q_CC_CLANG , Q_CC_GNU and Q_CC_MSVC can be replaced with __clang__ , __GNUC__ || __MINGW32__ and _MSC_VER respectively. QLatin1String() calls can be replaced with regular std::string, QString::number() - with something like sprintf, itoa, std::to_string (if you are using c++11).
And QSysInfo::WordSize is simple:
enum Sizes {
        WordSize = (sizeof(void *)<<3)
};

Good luck!

S
Sergei Borisov, 2014-10-09
@risik

There is nothing complicated. If you do not look at the original text as something given from above. For example, you can do everything by analogy with this one: stackoverflow.com/questions/151299/embedding-svn-r...

D
Don Kaban, 2014-10-09
@donkaban

in elf2, the compiler and platform version (including GLIBC and GLIBCXX) are written that way. gcc and clang write to .comment on the link, which is easy to see -
I don't know what's wrong with non-kosher PE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question