Platform-independent GUID generation in C++?

C++Cross PlatformGuidUuid

C++ Problem Overview


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.

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.

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

on linux: man uuid

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

[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;
}

Solution 3 - C++

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.

It is MIT licensed and available on GitHub:

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

Solution 4 - C++

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

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.

Solution 5 - C++

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 PRNG seeded 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.

Solution 6 - C++

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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMoses SchwartzView Question on Stackoverflow
Solution 1 - C++AnonymousView Answer on Stackoverflow
Solution 2 - C++ubikView Answer on Stackoverflow
Solution 3 - C++Graeme HillView Answer on Stackoverflow
Solution 4 - C++ShuggyCoUkView Answer on Stackoverflow
Solution 5 - C++shooshView Answer on Stackoverflow
Solution 6 - C++AutodidactView Answer on Stackoverflow