如何在c++中生成UUID,而不使用boost库?

时间:2021-02-20 18:23:48

I want to generate UUID for my application to distinguish each installation of my application. I want to generate this UUID using C++ without boost library support. How can I generate UUID using some other opensource library?

我想为我的应用程序生成UUID,以区分应用程序的每个安装。我想使用c++生成这个UUID,但不支持boost库。如何使用其他的开源库生成UUID ?

Note: My platform is windows

注意:我的平台是windows

3 个解决方案

#1


10  

As mentioned in the comments, you can use UuidCreate

正如在评论中提到的,您可以使用UuidCreate

#pragma comment(lib, "rpcrt4.lib")  // UuidCreate - Minimum supported OS Win 2000
#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
    UUID uuid;
    UuidCreate(&uuid);
    char *str;
    UuidToStringA(&uuid, (RPC_CSTR*)&str);
    cout<<str<<endl;
    RpcStringFreeA((RPC_CSTR*)&str);
    return 0;
}

#2


1  

The ossp-uuid library can generate UUIDs and has C++ bindings.

ossp- uuuid库可以生成uid并具有c++绑定。

It seems extremely simple to use:

使用起来似乎极其简单:

#include <uuid++.hh>
#include <iostream>

using namespace std;

int main() {
        uuid id;
        id.make(UUID_MAKE_V1);
        const char* myId = id.string();
        cout << myId << endl;
        delete myId;
}

Note that it allocates and returns a C-style string, which the calling code must deallocate to avoid a leak.

注意,它分配并返回c样式的字符串,调用代码必须释放该字符串以避免泄漏。

Another possibility is libuuid, which is part of the util-linux package, available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/. Any Linux machine will have it installed already. It does not have a C++ API but is still callable from C++ using the C API.

另一种可能是libuuid,它是util-linux包的一部分,可以从ftp://ftp.kernel.org/pub/linux/utils/util-linux/获得。任何Linux机器都已经安装了。它没有c++ API,但是仍然可以使用C API从c++调用。

#3


1  

Traditionally UUID's are simply the machine's MAC address concatenated with with the number of 100-nanosecond intervals since the adoption of the Gregorian calendar in the West. So it's not too difficult to write a C++ class that does this for you.

传统上,UUID只是把机器的MAC地址与西方采用公历后的100纳秒间隔连接在一起。所以写一个c++类并不难。

#1


10  

As mentioned in the comments, you can use UuidCreate

正如在评论中提到的,您可以使用UuidCreate

#pragma comment(lib, "rpcrt4.lib")  // UuidCreate - Minimum supported OS Win 2000
#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
    UUID uuid;
    UuidCreate(&uuid);
    char *str;
    UuidToStringA(&uuid, (RPC_CSTR*)&str);
    cout<<str<<endl;
    RpcStringFreeA((RPC_CSTR*)&str);
    return 0;
}

#2


1  

The ossp-uuid library can generate UUIDs and has C++ bindings.

ossp- uuuid库可以生成uid并具有c++绑定。

It seems extremely simple to use:

使用起来似乎极其简单:

#include <uuid++.hh>
#include <iostream>

using namespace std;

int main() {
        uuid id;
        id.make(UUID_MAKE_V1);
        const char* myId = id.string();
        cout << myId << endl;
        delete myId;
}

Note that it allocates and returns a C-style string, which the calling code must deallocate to avoid a leak.

注意,它分配并返回c样式的字符串,调用代码必须释放该字符串以避免泄漏。

Another possibility is libuuid, which is part of the util-linux package, available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/. Any Linux machine will have it installed already. It does not have a C++ API but is still callable from C++ using the C API.

另一种可能是libuuid,它是util-linux包的一部分,可以从ftp://ftp.kernel.org/pub/linux/utils/util-linux/获得。任何Linux机器都已经安装了。它没有c++ API,但是仍然可以使用C API从c++调用。

#3


1  

Traditionally UUID's are simply the machine's MAC address concatenated with with the number of 100-nanosecond intervals since the adoption of the Gregorian calendar in the West. So it's not too difficult to write a C++ class that does this for you.

传统上,UUID只是把机器的MAC地址与西方采用公历后的100纳秒间隔连接在一起。所以写一个c++类并不难。