如何查找boost运行时版本
How to find boost runtime version
我正在编写一个使用boost的C++库。
在这个库中,我想包括有关用于编译我的库的二进制版本的boost版本的信息。我可以使用宏BOOST_VERSION
,这很好。
我还想确定哪一个是boost的运行时版本,这样我就可以与用于编译我的库的boost版本进行比较。很明显,我不能使用宏,因为它会在编译时而不是运行时给我硬编码的版本。
我需要的是boost中的一个函数(比如boost::get_version()
)。有办法在助推中做到这一点吗?
您可以使用宏创建如下代码:
std::cout << "Using Boost "
<< BOOST_VERSION / 100000 << "." // maj. version
<< BOOST_VERSION / 100 % 1000 << "." // min. version
<< BOOST_VERSION % 100 // patch version
<< std::endl;
这适用于升压1.51.x及以上。不过,我不确定这是否是你想要的,我会继续努力,看看是否有办法以更优雅的方式从当前加载的dll中获取它。
附录:
查找运行时版本:
在查看了Boost系统之后,似乎最简单的方法就是拥有依赖于平台的代码,这些代码在编译时被用来制作不同版本的可执行文件。
对于Windows:
您需要使用GetFileVersionInfoEx API或GetFileVersionInfo API来查询DLL的版本,您还需要考虑操作系统是32位还是64位,因此这里有一些可能有用的代码:
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的版本:
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机器,稍后会详细检查,但最多只能进行小的修改)。
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
}
将两者结合:
要在多个操作系统中发生相同的事情,您可能需要使用以下内容:
#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中没有这样的功能,我认为你只能扫描二进制文件来查找这个东西(例如linux中的ldd
)。
相关文章:
- 我在使用 boost::serialization 时遇到了一个问题,我的代码在 Linux 中运行良好,但在 Wind
- boost SHA1 哈希与 md5sum/sha1sum 的运行时比较
- 使用boost::geometry::intersects时出现运行时错误
- 如何在运行时填充Boost :: Fusion ::向量
- 管理Boost::运行时共享库之间的任意
- 包含boost::asio标头时引发运行时异常
- 在运行时使用C++/boost从资源中解析.ini文件
- 运行时在boost::mpl::vector中查找第一个匹配项
- boost::spiritqi解析运行时出错
- 在运行时初始化 boost::accumulator_set
- 如何查找boost运行时版本
- 如何在运行时在boost::fusion::vector中查找元素
- 编译时与使用boost::fusion的运行时
- 使用 boost::iostreams 和 zlib 读取.gz文件的运行时错误
- Boost.Fusion运行时开关
- 为什么WP8.1运行时组件在调用boost::filesystem::file_size()时抛出C++异常
- 使用在运行时指定尺寸的boost几何体
- 如何使用boost::multiprecision在运行时更改数字精度
- boost 是否使用 C++11 "inline namespaces" 来避免运行时出现 ABI 不兼容错误?
- Boost运行时错误