摘自:https://www.jianshu.com/p/3f9c2e8ac9d0
1 #include <stdio.h> 2 #include <uuid/uuid.h> 3 // yum -y install libuuid-devel.x86_64 4 5 int test_libuuid() 6 { 7 uuid_t uuid1, uuid2, uuid3, uuid4, uuid5; 8 char uuid1_str[37], uuid2_str[37], uuid3_str[37], uuid4_str[37]; 9 10 uuid_generate(uuid1); 11 uuid_unparse(uuid1, uuid1_str); 12 fprintf(stdout, "uuid1 result: %s\n", uuid1_str); 13 14 15 uuid_generate_random(uuid2); 16 uuid_unparse_lower(uuid2, uuid2_str); 17 fprintf(stdout, "uuid2 result: %s\n", uuid2_str); 18 19 uuid_generate_time(uuid3); 20 uuid_unparse_upper(uuid3, uuid3_str); 21 fprintf(stdout, "uuid3 result: %s\n", uuid3_str); 22 23 uuid_generate_time_safe(uuid4); 24 uuid_unparse(uuid4, uuid4_str); 25 fprintf(stdout, "uuid4 result: %s\n", uuid4_str); 26 27 uuid_parse(uuid1_str, uuid5); 28 int ret = uuid_compare(uuid1, uuid5); 29 fprintf(stdout, "uuid1 compare uuid5: %d\n", ret); 30 31 ret = uuid_is_null(uuid1); 32 fprintf(stdout, "uuid1 is null: %d\n", ret); 33 uuid_clear(uuid1); 34 ret = uuid_is_null(uuid1); 35 fprintf(stdout, "uuid1 is null: %d\n", ret); 36 37 ret = uuid_type(uuid2); 38 fprintf(stdout, "uuid2 type: %d\n", ret); 39 ret = uuid_variant(uuid2); 40 fprintf(stdout, "uuid2 variant: %d\n", ret); 41 42 struct timeval tv; 43 time_t time_reg = uuid_time(uuid3, &tv); 44 fprintf(stdout, "uuid3 time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec, ctime(&time_reg)); 45 46 return 0; 47 } 48 49 int main() 50 { 51 test_libuuid(); 52 53 return 0; 54 }
gcc uuid.c -o uuid -luuid