Generate UUIDs in C++
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C++
What: Generate UUIDs in C++ using Boost.UUID (most popular) or a custom implementation with <code><random></code>. Boost provides RFC 4122 compliant UUID generation.
Try it: Use our interactive Uuids generator or integrate this code into your C++ application.
Generate UUIDs in C++ using Boost.UUID (most popular) or a custom implementation with <random>. Boost provides RFC 4122 compliant UUID generation.
Looking for other languages? Check our code examples in
PHP
,
JavaScript
,
Python
,
Java
,
C#
,
Ruby
and
Go
or use our interactive web generator.
C++ Code Example
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
#include <string>
int main() {
// UUID v4 (random) using Boost
boost::uuids::random_generator gen;
boost::uuids::uuid uuid = gen();
std::cout << uuid << std::endl; // Example: 3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f
// Convert to string
std::string uuid_str = boost::uuids::to_string(uuid);
std::cout << uuid_str << std::endl;
// Generate multiple UUIDs
for (int i = 0; i < 5; ++i) {
std::cout << gen() << std::endl;
}
// UUID v5 (namespaced)
boost::uuids::name_generator_sha1 name_gen(boost::uuids::ns::dns());
boost::uuids::uuid uuid5 = name_gen("example.com");
std::cout << uuid5 << std::endl; // Always same
// Parse UUID from string
std::string uuid_string = "3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f";
boost::uuids::string_generator string_gen;
boost::uuids::uuid parsed = string_gen(uuid_string);
std::cout << parsed << std::endl;
return 0;
}
// Compile: g++ -std=c++11 uuid_example.cpp -lboost_system
[EXPLANATION]
Install Boost with package manager or from boost.org. random_generator creates UUID v4 using std::random_device. name_generator_sha1 creates deterministic UUID v5.
Expected Output
3d6f9e64-7d8a-4c3e-8f2b-1a4e5c6d7e8f cfbff0d1-9375-5685-968c-48ce8b15ae17
Common Use Cases
- Game development entity IDs
- Qt/wxWidgets application identifiers
- Unreal Engine unique object IDs
- Embedded systems device identifiers
- Cross-platform desktop apps
- C++ microservices
Important Notes
- Boost.UUID is the de facto standard for C++ UUIDs
-
Header-only alternative:
stduuidlibrary - Requires C++11 or later
-
Link with
-lboost_systemif not header-only
Try Our Interactive Generator
Don't want to write code? Use our free web-based Uuids generator with instant results.
TRY UUIDS GENERATOR →Other Programming Languages
View Uuids generation code examples in PHP
View Uuids generation code examples in JavaScript
View Uuids generation code examples in Python
View Uuids generation code examples in Java
View Uuids generation code examples in C#
View Uuids generation code examples in Ruby
View Uuids generation code examples in Go