Regardless of the generation GUID platform in C ++?

What is the best way to programmatically generate a GUID or UUID in C ++ without relying on a platform-specific tool? I try to make unique identifiers for objects in the simulation, but I can not rely on the implementation of Microsoft, because the project is cross-platform.

Notes:

  • Since this is for a simulator, I do not need cryptographic randomness.
  • It would be better if it is a 32-bit number.
+49
c ++ cross-platform guid uuid
Feb 12 '09 at 21:17
source share
6 answers

If you can let Boost be used, then there is a UUID library that should do the trick. It is very simple to use - check the documentation and this answer .

+43
Feb 12 '09 at 21:26
source share

on linux: man uuid

on victory: check the UUID structure and the UuidCreate function in msdn

[edit] function will look as follows

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; } 
+33
Oct 26 '09 at 17:56
source share

If you cannot afford to use Boost, then there is a very minimal library that I implemented that simply acts as a wrapper for each native version of the operating system. 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 guideline generation function has a different implementation for each system, but there is a single common implementation for comparison, trimming, and parsing.

This is a MIT license and is available on GitHub:

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

+9
Jun 01
source share

Simple use of any guid / uuid is present on the target platform. Is it right difficult (let go of purchases;)).

The likelihood of a collision between any two identifiers from different but well-executed implementations should be significantly higher than any reasonable chance of what happens in this life of the universe.

+2
Feb 12 '09 at 21:25
source share

A GUID generator typically uses the hardware characteristics of a machine, usually such as MAC addresses or IP addresses. The only thing you can do, absolutely independent of the platform, is to use some kind of PRNG , seeded manually or seeded from some time source. Usually this 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.

+1
Feb 12 '09 at 21:24
source share

Well, one solution (?) Would be to use a web service to get a GUID, but I doubt it would be a good solution for a C ++ program, especially if it is not connected to the network.

Here is a URL that may come in handy if you decide to use this option: http://www.hoskinson.net/GuidGenerator/default.asp

-one
Feb 12 '09 at 21:36
source share



All Articles