如何找到boost运行时版本 [英] How to find boost runtime version

查看:145
本文介绍了如何找到boost运行时版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用boost的C ++库。

I am writing a C++ library that uses boost.

在这个库中,我想包含用于编译我的二进制版本图书馆。
我可以使用宏 BOOST_VERSION ,这很好。

In this library I want to include information about the boost version that was used to compile the binary version of my library. I can use the macro BOOST_VERSION and this is fine.

我也想确定boost的运行时版本,所以我可以比较用于编译我的库的boost版本。显然我不能使用宏,因为它会在编译时给我硬编码版本,而不是在运行时。

I also wanted to determine which is the runtime version of boost so I could compare with the boost version that was used to compile my library. Obviously I cannot use the macro because it will give me the hard coded version at compile time, not at runtime.

我需要的是一个函数boost c $ c> boost :: get_version())。

What I needed is a function in boost (like boost::get_version()). Is there a way to do this in boost?

推荐答案

您可以使用宏创建一些代码,如下所示:

You can use the macro to create some code as follows:

std::cout << "Using Boost "     
          << BOOST_VERSION / 100000     << "."  // maj. version
          << BOOST_VERSION / 100 % 1000 << "."  // min. version
          << BOOST_VERSION % 100                // patch version
          << std::endl;

这适用于boost 1.51.x及以上版本。不确定这是否是你正在寻找的,但我会继续去看看是否有一种方法,从一个更优雅的方式从当前加载的dll。

This works on boost 1.51.x and upwards. Not sure if this is what you are looking for though, I will keep going and see if there is a way to get it from the currently loaded dll in a more elegant way.

查找运行时版本:

查看Boost系统后,你可以做的最简单的方法是使用平台相关的代码,在编译时使用不同版本的可执行文件。

After looking at the Boost system, it seems that the simplest way you could do what you are looking for would be to have platform dependant code which is utilised at compiletime to make the different versions of executable.

您需要使用 GetFileVersionInfoEx API GetFileVersionInfo API ,你还需要考虑操作系统是32位还是64位,所以这里是一些可能有用的代码:

You will need to query the DLL for its version using GetFileVersionInfoEx API or GetFileVersionInfo API, you also need to take into account if the OS is 32 bit or 64 bit, so here is some code which may be of use:

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS IsWow64ProcessFunc;

BOOL is64BitOS()
{
    BOOL retval= FALSE;
    // need to check if the OS is 64 bit, and we cant therefore indiscrimately call the 64 bit function for this, so we use the following:
    IsWow64ProcessFunc= (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if(IsWow64ProcessFunc != NULL)
    {
        if (!IsWow64ProcessFunc(GetCurrentProcess(),&bIsWow64))
        {
            // your error handling code in here
        }
    }
    return retval;
}

然后我们可以使用一个直接的代码来获得版本DLL:

Then we can utilise a straight-forward bit of code to get the version of the DLL:

void myWindowsDLLVersionChecker()
{
    DWORD  versionHandle = NULL;
    UINT   size      = 0;
    LPBYTE buffer  = NULL;
    DWORD  versionSize   = GetFileVersionInfoSize( szVersionFile, &versionHandle);

    if (verSize != NULL)
    {
        LPSTR versionData = new char[versionSize];

        if (GetFileVersionInfo( szVersionFile, versionHandle, versionSize, versionData))
        {
            if (VerQueryValue(versionData,"\\",(VOID FAR* FAR*)&buffer,&size))
            {
                if (size)
                {
                    VS_FIXEDFILEINFO *versionInfo = (VS_FIXEDFILEINFO *)buffer;
                    if (versionInfo->dwSignature == 0xFEEF04BD)  // value from http://msdn.microsoft.com/en-us/library/windows/desktop/ms646997(v=vs.85).aspx
                    {
                        if(is64BitOS())    //  if it is true then its a 64 bit Windows OS
                        {
                                major =     (versionInfo->dwProductVersionMS >> 16) & 0xff;
                                minor =     (versionInfo->dwProductVersionMS >>  0) & 0xff;
                                revision =  (versionInfo->dwProductVersionLS >> 16) & 0xff;
                                build =     (versionInfo->dwProductVersionLS >>  0) & 0xff;
                        } 
                        else //  32 bit Windows OS
                        {
                                major =     HIWORD(versionInfo->dwProductVersionMS);
                                minor =     LOWORD(versionInfo->dwProductVersionMS);
                                revision =  HIWORD(versionInfo->dwProductVersionLS);
                                build =     LOWORD(versionInfo->dwProductVersionLS);
                        }
                    }
                }
            }
        }
        delete[] versionData;
    }
}



对于Linux:



当前无法记住当前的代码,但是这里是内存的一个部分解决方案(我现在没有Linux机器,稍后会详细检查,

For Linux:

Horribly cant remember the code for that at present, however here is a partial solution from memory (I don't have a Linux machine to hand at the moment, will check this in more detail later, but should work with only minor modifications at most).

void myLinuxDLLVersionChecker() 
{
    // call readelf as an external proces son libboost_system.so, 
    // and use the information returned to extract the version from.
    // or use something like
    setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
    FILE *ldd = popen("/lib/libboost_system.so");
    //  information from http://linux.die.net/man/8/ld.so
    // the above pretty much replicates ldd's output, so you can pick out the version
}



结合两者:



在多个操作系统中发生同样的事情你可能需要使用类似的:

Combining the two:

To make the same thing happen in multiple OSes you would probably have to use something like:

#ifdef __unix__                    /* __unix__ is usually defined by compilers targeting Unix systems */
void detectBoostDllVersion()  // does not have to be void, can be whatever you need
{
    myLinuxDLLVersionChecker();
}
#elif defined(_WIN32) || defined(WIN32)     /* _Win32 is usually defined by compilers targeting 32 or   64 bit Windows systems */
void detectBoostDllVersion()  // has to match the same return type as the previous version
{
    myWindowsDLLVersionChecker();
}
#endif



现在取决于编译的操作系统,正确的函数将由 detectBoostDLLVersion()函数指向:)

这篇关于如何找到boost运行时版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆