在linux上,可以借助libuuid1实现生成唯一标识的功能。安装命令如下:
sudo apt install uuid-dev参考代码如下:
/* For malloc() */ #include <stdlib.h> /* For puts()/printf() */ #include <stdio.h> /* For uuid_generate() and uuid_unparse() */ #include <uuid/uuid.h> /* Uncomment to always generate capital UUIDs. */ //#define capitaluuid true /* Uncomment to always generate lower-case UUIDs. */ //#define lowercaseuuid true /* * Don't uncomment either if you don't care (the case of the letters * in the 'unparsed' UUID will depend on your system's locale). */ int main(void) { uuid_t binuuid; /* * Generate a UUID. We're not done yet, though, * for the UUID generated is in binary format * (hence the variable name). We must 'unparse' * binuuid to get a usable 36-character string. */ uuid_generate_random(binuuid); /* * uuid_unparse() doesn't allocate memory for itself, so do that with * malloc(). 37 is the length of a UUID (36 characters), plus '\0'. */ char *uuid = malloc(37); #ifdef capitaluuid /* Produces a UUID string at uuid consisting of capital letters. */ uuid_unparse_upper(binuuid, uuid); #elif lowercaseuuid /* Produces a UUID string at uuid consisting of lower-case letters. */ uuid_unparse_lower(binuuid, uuid); #else /* * Produces a UUID string at uuid consisting of letters * whose case depends on the system's locale. */ uuid_unparse(binuuid, uuid); #endif // Equivalent of printf("%s\n", uuid); - just my personal preference puts(uuid); return 0; } 附:参考链接