Linux random UUID generation

I am stuck in a strange predicament. I need to generate a UUID in my Linux program (which I distribute using RPM). I do not want to add another dependency to my application, requiring the user to install libuuid (it seems that libuuid is not included in most Linux distributions like CentOS).

Is there a standard Linux system call that generates UUIDs (for example, on Windows is there CoCreateGuid)? What does uuidgen use?

+7
c ++ linux guid uuid
source share
6 answers

Thanks for all your comments!

I went through each, and this is what met my requirement best:

What I needed was just time-based UUIDs that were generated from random numbers once for each user who installed the application. The version of UUID 4, as specified in RFC 4122, was just that. I reviewed the proposed algorithm and came up with a fairly simple solution that will work on both Linux and Windows (perhaps too simplistic, but it satisfies the need!):

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 
+5
source share

Am I missing something? You can not:

 cat /proc/sys/kernel/random/uuid 
+9
source share

A good way I found (for linux dev) is #include <uuid/uuid.h> . Then you have a few functions that you can call:

 void uuid_generate(uuid_t out); void uuid_generate_random(uuid_t out); 
+5
source share

Is there a reason you can't just bind statically to libuuid?

+3
source share

Maybe this will help? http://ooid.sourceforge.net/

+2
source share

POSIX does not have a system call to generate UUIDs, but I think you can find the BSD / MIT code somewhere to generate UUIDs. ooid is released under the Boost software license, which, according to wikipedia, is a BSD / MIT style licensing license. Then you can simply paste it into your application, without the need to add dependencies.

+2
source share

All Articles