对"uuid_generate"的未定义引用,即使我使用 -luuid

undefined reference to 'uuid_generate' even though I use -luuid

本文关键字:-luuid 未定义 uuid generate 引用      更新时间:2023-10-16

My test.cpp

#include <uuid/uuid.h>
#include <iostream>
int main(int argc, char *argv[])
{
  uuid_t id;
  uuid_generate(id);
  char *string = new char[100];
  uuid_unparse(id, string);
  std::cout << string << std::endl;
  return 0;
}

我使用的是Ubuntu 14

我正在运行test.cpp作为…

g++ -luuid test.cpp

和输出

test.cpp:(.text+0x26): undefined reference to `uuid_generate'
test.cpp:(.text+0x47): undefined reference to `uuid_unparse'
collect2: error: ld returned 1 exit status

我的c++版本:

Target: x86_64-linux-gnu
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

和我已经安装了uid-dev。

sudo apt-get install uuid uuid-dev
uuid is already the newest version.
uuid-dev is already the newest version.

链接库的顺序很重要,您需要在引用的模块后添加-luuid:

g++ test.cpp -luuid

除非您正在使用分组选项(-Wl,--start-group, -Wl,--end-group)。