在 C++ 中生成独立于平台的 GUID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/543306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:52:25  来源:igfitidea点击:

Platform-independent GUID generation in C++?

c++cross-platformguiduuid

提问by Moses Schwartz

What is the best way to programmatically generate a GUID or UUID in C++ without relying on a platform-specific tool? I am trying to make unique identifiers for objects in a simulation, but can't rely on Microsoft's implementation as the project is cross-platform.

在不依赖特定平台的工具的情况下,在 C++ 中以编程方式生成 GUID 或 UUID 的最佳方法是什么?我正在尝试为模拟中的对象制作唯一标识符,但不能依赖 Microsoft 的实现,因为该项目是跨平台的。

Notes:

笔记:

  • Since this is for a simulator, I don't really need cryptographic randomness.
  • It would be best if this is a 32 bit number.
  • 由于这是一个模拟器,我真的不需要加密随机性。
  • 最好是 32 位数字。

采纳答案by Anonymous

If you can afford to use Boost, then there is a UUIDlibrary that should do the trick. It's very straightforward to use - check the documentation and this answer.

如果您负担得起使用 Boost,那么应该有一个UUID库可以解决问题。使用起来非常简单 - 检查文档和这个答案

回答by ubik

on linux: man uuid

在 linux 上: man uuid

on win: check out for UUID structure and UuidCreate function in msdn

在 win 上:查看 msdn 中的 UUID 结构和 UuidCreate 函数

[edit] the function would appear like this

[编辑] 该函数将如下所示

extern "C"
{
#ifdef WIN32
#include <Rpc.h>
#else
#include <uuid/uuid.h>
#endif
}

std::string newUUID()
{
#ifdef WIN32
    UUID uuid;
    UuidCreate ( &uuid );

    unsigned char * str;
    UuidToStringA ( &uuid, &str );

    std::string s( ( char* ) str );

    RpcStringFreeA ( &str );
#else
    uuid_t uuid;
    uuid_generate_random ( uuid );
    char s[37];
    uuid_unparse ( uuid, s );
#endif
    return s;
}

回答by Graeme Hill

If you cannot afford to use Boost, then there is a very minimal library that I implemented which simply acts as a wrapper around each operating system's native guid implementation. It should work on Windows (using CoCreateGuid), Linux (using libuuid), MacOS (using CFUUID), iOS (also using CFUUID), and Android (using JNI calls to java.util.UUID). The guid generation function has a different implementation on each system but there is single generic implementation for comparing, stringifying, and parsing.

如果您负担不起使用 Boost,那么我实现了一个非常小的库,它只是充当每个操作系统的本机 guid 实现的包装器。它应该适用于 Windows(使用CoCreateGuid)、Linux(使用libuuid)、MacOS(使用CFUUID)、iOS(也使用CFUUID)和 Android(使用 JNI 调用java.util.UUID)。guid 生成函数在每个系统上都有不同的实现,但有一个用于比较、字符串化和解析的通用实现。

It is MIT licensed and available on GitHub:

它已获得 MIT 许可,可在 GitHub 上获得:

https://github.com/graeme-hill/crossguid

https://github.com/graeme-hill/crossguid

回答by ShuggyCoUk

Simply using whatever guid/uuid is present on the target platform is best. Doing it right is hard (let's go shopping ;)).

最好使用目标平台上存在的任何 guid/uuid。做对了很难(让我们去购物吧;))。

The probability of a collision between any two identifiers from different but well executed implementations should be well beyond any reasonable chance of happening in this universe's lifetime.

来自不同但执行良好的实现的任何两个标识符之间发生冲突的可能性应该远远超出在这个宇宙的生命周期中发生的任何合理的机会。

回答by shoosh

A GUID generator usually relies on hardware characteristics of the machine, usually stuff like MAC addresses or IPs. The only thing you can do which is absolutely platform independent is use some kind of PRNGseeded manually or seeded from some time source. This usually does not generate a true globally unique identifier in the sense that you are guaranteed that no one in the world generates the same number.

GUID 生成器通常依赖于机器的硬件特性,通常是 MAC 地址或 IP 之类的东西。您唯一可以做的绝对独立于平台的事情是使用某种手动播种或从某个时间源播种的PRNG。这通常不会生成真正的全球唯一标识符,因为您可以保证世界上没有人生成相同的数字。

回答by Autodidact

Well one offbeat(?) solution would be to use a web service to get the GUID but I doubt this will be a good solution for a C++ program especially if it's not network enabled.

好吧,另一种(?)解决方案是使用 Web 服务来获取 GUID,但我怀疑这对于 C++ 程序来说是否是一个很好的解决方案,尤其是在它没有启用网络的情况下。

Here is a URL which might come in handy if you choose to pursue this option: http://www.hoskinson.net/GuidGenerator/default.asp

如果您选择采用此选项,以下 URL 可能会派上用场:http: //www.hoskinson.net/GuidGenerator/default.asp