在Linux下编写生成GUID的程序,如下 guid.c 文件:
#include <stdio.h>
#include <uuid/uuid.h>
void uuid2string(const uuid_t uu, char* const str) // 把uuid转成字符串。
{
int i;
for(i=0;i<16;i++)
sprintf(str+i*2, "%02X",uu[i]);
return;
}
void main()
{
int i;
char buf[200];
uuid_t uu;
uuid_generate(uu);
printf("{");
for(i=0;i<15;i++)
printf("%02X-",uu[i]);
printf("%02X}\n",uu[15]);
uuid2string(uu, buf);
printf("%s\n",buf);
return;
}
编译guid.c文件:
# gcc -o guid guid.c -luuid
如果在编译时出现错误“uuid/uuid.h: No such file or directory”,解决办法:安装 libuuid-devel 软件包。
如下直接用yum命令安装:
[root@localhost]# yum install libuuid-devel
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* epel: mirrors.yun-idc.com
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package libuuid-devel.x86_64 0:2.17.2-12.14.el6 will be installed
--> Processing Dependency: libuuid = 2.17.2-12.14.el6 for package: libuuid-devel-2.17.2-12.14.el6.x86_64
--> Running transaction check
---> Package libuuid.i686 0:2.17.2-12.7.el6_3 will be updated
--> Processing Dependency: libuuid = 2.17.2-12.7.el6_3 for package: libblkid-2.17.2-12.7.el6_3.x86_64
--> Processing Dependency: libuuid = 2.17.2-12.7.el6_3 for package: util-linux-ng-2.17.2-12.7.el6_3.x86_64
---> Package libuuid.x86_64 0:2.17.2-12.7.el6_3 will be updated
---> Package libuuid.i686 0:2.17.2-12.14.el6 will be an update
---> Package libuuid.x86_64 0:2.17.2-12.14.el6 will be an update
--> Running transaction check
---> Package libblkid.x86_64 0:2.17.2-12.7.el6_3 will be updated
---> Package libblkid.x86_64 0:2.17.2-12.14.el6 will be an update
---> Package util-linux-ng.x86_64 0:2.17.2-12.7.el6_3 will be updated
---> Package util-linux-ng.x86_64 0:2.17.2-12.14.el6 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
libuuid-devel x86_64 2.17.2-12.14.el6 base 84 k
Updating for dependencies:
libblkid x86_64 2.17.2-12.14.el6 base 115 k
libuuid i686 2.17.2-12.14.el6 base 68 k
libuuid x86_64 2.17.2-12.14.el6 base 68 k
util-linux-ng x86_64 2.17.2-12.14.el6 base 1.5 M
Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 4 Package(s)
Total download size: 1.9 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): libblkid-2.17.2-12.14.el6.x86_64.rpm | 115 kB 00:00
(2/5): libuuid-2.17.2-12.14.el6.i686.rpm | 68 kB 00:00
(3/5): libuuid-2.17.2-12.14.el6.x86_64.rpm | 68 kB 00:00
(4/5): libuuid-devel-2.17.2-12.14.el6.x86_64.rpm | 84 kB 00:00
(5/5): util-linux-ng-2.17.2-12.14.el6.x86_64.rpm | 1.5 MB 00:04
--------------------------------------------------------------------------------
Total 332 kB/s | 1.9 MB 00:05
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
Updating : libuuid-2.17.2-12.14.el6.x86_64 1/9
Updating : libblkid-2.17.2-12.14.el6.x86_64 2/9
Updating : util-linux-ng-2.17.2-12.14.el6.x86_64 3/9
Installing : libuuid-devel-2.17.2-12.14.el6.x86_64 4/9
Updating : libuuid-2.17.2-12.14.el6.i686 5/9
Cleanup : util-linux-ng-2.17.2-12.7.el6_3.x86_64 6/9
Cleanup : libuuid-2.17.2-12.7.el6_3 7/9
Cleanup : libblkid-2.17.2-12.7.el6_3.x86_64 8/9
Cleanup : libuuid-2.17.2-12.7.el6_3 9/9
Verifying : libblkid-2.17.2-12.14.el6.x86_64 1/9
Verifying : util-linux-ng-2.17.2-12.14.el6.x86_64 2/9
Verifying : libuuid-2.17.2-12.14.el6.i686 3/9
Verifying : libuuid-2.17.2-12.14.el6.x86_64 4/9
Verifying : libuuid-devel-2.17.2-12.14.el6.x86_64 5/9
Verifying : libuuid-2.17.2-12.7.el6_3.x86_64 6/9
Verifying : libuuid-2.17.2-12.7.el6_3.i686 7/9
Verifying : util-linux-ng-2.17.2-12.7.el6_3.x86_64 8/9
Verifying : libblkid-2.17.2-12.7.el6_3.x86_64 9/9
Installed:
libuuid-devel.x86_64 0:2.17.2-12.14.el6
Dependency Updated:
libblkid.x86_64 0:2.17.2-12.14.el6 libuuid.i686 0:2.17.2-12.14.el6
libuuid.x86_64 0:2.17.2-12.14.el6 util-linux-ng.x86_64 0:2.17.2-12.14.el6
Complete!
[root@localhost]#