C++ 在 Linux 中生成随机 UUID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2174768/
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
Generating random UUIDs in Linux
提问by themoondothshine
I am stuck in a strange predicament. I need to generate UUIDs in my Linux program (which I distribute using RPMs). I do not want to add another dependency to my application by requiring the user to install libuuid (seems like libuuid isn't included in most Linux distros, like CentOS).
我陷入了一个奇怪的困境。我需要在我的 Linux 程序(我使用 RPM 分发)中生成 UUID。我不想通过要求用户安装 libuuid 来向我的应用程序添加另一个依赖项(大多数 Linux 发行版中似乎不包含 libuuid,例如 CentOS)。
Isn't there a standard Linux system call which generates UUIDs (like say, in Windows there CoCreateGuid)? What does the command uuidgen use?
是不是有一个标准的 Linux 系统调用可以生成 UUID(比如,在 Windows 中有 CoCreateGuid)?uuidgen 命令有什么用?
采纳答案by themoondothshine
Thanks for all your comments!
谢谢你们的评论!
I went through each one, and here's what suited my requirement the best:
我经历了每一个,这是最适合我的要求的:
What I needed was just plain time-based UUIDs which were generated from random numbers once for every user who installed the application. UUID version 4 as specified in RFC 4122 was exactly it. I went through a the algorithm suggested, and came up with a pretty simple solution which would work in Linux as well as Windows (Maybe its too simplistic, but it does satisfy the need!):
我需要的只是简单的基于时间的 UUID,这些 UUID 是为每个安装该应用程序的用户从随机数生成一次。RFC 4122 中指定的 UUID 版本 4 正是它。我经历了建议的算法,并提出了一个非常简单的解决方案,它可以在 Linux 和 Windows 中工作(也许它太简单了,但它确实满足了需求!):
srand(time(NULL));
sprintf(strUuid, "%x%x-%x-%x-%x-%x%x%x",
rand(), rand(), // Generates a 64-bit Hex number
rand(), // Generates a 32-bit Hex number
((rand() & 0x0fff) | 0x4000), // Generates a 32-bit Hex number of the form 4xxx (4 indicates the UUID version)
rand() % 0x3fff + 0x8000, // Generates a 32-bit Hex number in the range [0x8000, 0xbfff]
rand(), rand(), rand()); // Generates a 96-bit Hex number
回答by VMcPherron
Am I missing something? Can't you:
我错过了什么吗?你不能:
cat /proc/sys/kernel/random/uuid
回答by Chef Pharaoh
A good way I found (for linux dev) is to #include <uuid/uuid.h>
. Then you have a few functions you can call:
我发现(对于 linux 开发人员)的一个好方法是将#include <uuid/uuid.h>
. 然后你有几个可以调用的函数:
void uuid_generate(uuid_t out);
void uuid_generate_random(uuid_t out);
回答by caf
Is there any reason why you can't just link statically to libuuid?
有什么理由不能只是静态链接到 libuuid?
回答by caf
Perhaps ooid will help? http://ooid.sourceforge.net/
也许 ooid 会有所帮助?http://ooid.sourceforge.net/
回答by Stefano Borini
No system call exists in POSIX to generate UUID, but I guess you can find somewhere a BSD/MIT code to generate the UUID. ooid is released under the Boost software license, which according to wikipedia, is a permissive license in the style of BSD/MIT. Then you can just paste it into your application, without any need to add dependencies.
POSIX 中不存在用于生成 UUID 的系统调用,但我想您可以在某处找到 BSD/MIT 代码来生成 UUID。ooid 是在 Boost 软件许可证下发布的,根据维基百科,它是 BSD/MIT 风格的许可许可证。然后您可以将其粘贴到您的应用程序中,无需添加任何依赖项。