1.程序源码
#include <stdio.h>
#include <uuid/uuid.h>
int test_libuuid()
{
uuid_t uuid1, uuid2, uuid3, uuid4, uuid5;
char uuid1_str[37], uuid2_str[37], uuid3_str[37], uuid4_str[37];
uuid_generate(uuid1);
uuid_unparse(uuid1, uuid1_str);
fprintf(stdout, "uuid1 result: %s\n", uuid1_str);
uuid_generate_random(uuid2);
uuid_unparse_lower(uuid2, uuid2_str);
fprintf(stdout, "uuid2 result: %s\n", uuid2_str);
uuid_generate_time(uuid3);
uuid_unparse_upper(uuid3, uuid3_str);
fprintf(stdout, "uuid3 result: %s\n", uuid3_str);
uuid_generate_time_safe(uuid4);
uuid_unparse(uuid4, uuid4_str);
fprintf(stdout, "uuid4 result: %s\n", uuid4_str);
uuid_parse(uuid1_str, uuid5);
int ret = uuid_compare(uuid1, uuid5);
fprintf(stdout, "uuid1 compare uuid5: %d\n", ret);
ret = uuid_is_null(uuid1);
fprintf(stdout, "uuid1 is null: %d\n", ret);
uuid_clear(uuid1);
ret = uuid_is_null(uuid1);
fprintf(stdout, "uuid1 is null: %d\n", ret);
ret = uuid_type(uuid2);
fprintf(stdout, "uuid2 type: %d\n", ret);
ret = uuid_variant(uuid2);
fprintf(stdout, "uuid2 variant: %d\n", ret);
struct timeval tv;
time_t time_reg = uuid_time(uuid3, &tv);
fprintf(stdout, "uuid3 time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec, ctime(&time_reg));
return 0;
}
int main()
{
test_libuuid();
return 0;
}
2.编译源码
$ gcc -o uuid uuid.c -I/usr/local/include -L/usr/local/lib/ -luuid -Wl,-rpath=/usr/local/lib
3.运行程序
$ ./uuid
uuid1 result: 5754d7de-3501-4b35-a1f5-92e30625b28c
uuid2 result: 004cabf9-a95c-4f88-a674-d9093dccab01
uuid3 result: 059830B4-324A-11EB-B8D2-932B42800A49
uuid4 result: 0598312c-324a-11eb-b8d2-932b42800a49
uuid1 compare uuid5: 0
uuid1 is null: 0
uuid1 is null: 1
uuid2 type: 4
uuid2 variant: 1
uuid3 time is: (1606657906, 776082): Sun Nov 29 21:51:46 2020
网友评论