在Ubuntu上没有定义的参考错误。

时间:2022-11-23 20:36:01

I have a strange problem and i have gone through all similar questions but can't find an answer.

我有一个奇怪的问题,我经历了所有类似的问题,却找不到答案。

I am trying to compile some code which keeps throwing up undefined reference error even though the library is specified using -l and is in the LD_LIBRARY_PATH too. I can't figure out the reason. Here is an example

我正在尝试编译一些代码,这些代码不断抛出未定义的引用错误,尽管库是使用-l指定的,也在LD_LIBRARY_PATH中。我搞不懂原因。这是一个例子

gcc -L/home/sam/gmdb/lib -L/home/sam/db/add-ons/lib -L/home/sam/convert/lib -L/home/sam/rtana/lib -L/home/sam/rtana/add-ons/lib -o /home/sam/gmdb/bin/server /home/sam/db/obj/tools/server/server.o /home/sam/db/obj/tools/common/tool_data_parse.o /home/sam/db/obj/tools/common/tool_param.o /home/sam/gmdb/obj/tools/common/tool_public.o -lgmcommon -L/home/sam/db/add-ons/vpp/lib/suse -lipsi_crypto -lipsi_osal -lipsi_pse -lipsi_ssl -lgmmd5 -lgmkernel -lgmpl -lgmrep -lgmsqlserver -lgmsqlclient -lconvert -lrtana -lglog -lgflags -lprotobuf -lre2 -lboost_timer -lnuma -lpthread -lm -lrt

gcc -L/home/sam/gmdb/lib -L/home/sam/db/add-on /lib -L/home/sam/ home/sam/rtana/lib -L/home/sam/rtana/add-on /add-ons/lib -o / home/sam/db/server/server/server/server/。o /home/sam/db/obj/tools/common/tool_data_parse。o /home/sam/db/obj/tools/common/tool_param。o /home/sam/gmdb/obj/tools/common/tool_public。l /home/sam/db/add-ons/vpp/ vpp/lib/suse -lipsi_crypto -lipsi_ssl -lgmr -lgmr -lgmr -lgmr -lgmsqlserver -lgmsqlserver -lgmsqlserver -lgmsqlserver -lgmsqlserver - lrgmsqlserver - lrms -lre2 -lre2 - lpro_timer -lpthread -lpthread -lm -lrt。

The list of undefined errors is long but the first one is

未定义错误的列表很长,但是第一个错误是。

/home/usama/convert/lib/libconvert.so: undefined reference to `numa_num_configured_cpus'

/home/usama/convert/lib/libconvert。所以:未定义的引用“numa_num_configured_cpus”

So as you can see it's complaining about a method in libnuma. libnuma is provided a -lnuma as you can see, and is present in /usr/lib

你可以看到它在抱怨libnuma中的方法。libnuma提供了一个-lnuma,您可以看到,并出现在/usr/lib中。

The error mentions libconvert which is just a shared library that has a call to numa_num_configured_cpus but is not linked with libnuma, and it shouldn't matter since libconvert is just an so file. I am providing the -lnuma while generating the executable as you can see above. Here is the nm out on libconvert

这个错误提到了libconvert,它只是一个共享库,它有一个对numa_num_configured_cpus的调用,但是与libnuma没有关联,并且它不应该很重要,因为libconvert只是一个so文件。在生成可执行文件时,我提供了-lnuma,您可以在上面看到。这是在libconvert上的nm。

nm -C -u convert/lib/libconvert.so | grep numa*
                 U google::protobuf::internal::NameOfEnum(google::protobuf::EnumDescriptor const*, int)
                 U numa_num_configured_cpus
                 U numa_num_configured_nodes

I did try to compile with --unresolved-symbols, which results in successful compilation but the binary doesn't run again complaining about undefined symbols.

我确实尝试过用-unresolved-符号来编译,这将导致编译成功,但是二进制代码不会再次运行对未定义符号的抱怨。

My GCC version is

我的GCC版本是

gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

And Ubuntu is 64 bit Ubuntu 12.10

Ubuntu是64位Ubuntu 12.10。

Does anyone have a clue if i am hitting a bug or what is going on?

有没有人知道我是在打窃听器还是发生了什么?

1 个解决方案

#1


4  

Guys thank you for your comments. The problem as i found that is because the default behavior of gcc is changed in Ubuntu (at-least the version i am using) The hint is on this wiki page of debian https://wiki.debian.org/ToolChain/DSOLinking

谢谢你的评论。我发现的问题是,因为gcc的默认行为在Ubuntu中发生了变化(至少我正在使用的版本),提示在debian https://wiki.debian.org/ToolChain/DSOLinking的wiki页面上。

According to this the gcc is changed to add --as-needed to the linker. The down side of this behavior is that and i quote the wiki " Binaries, which are using symbols from an indirectly linked shared library will fail to link "

根据这一点,gcc被更改为添加——需要链接器。这种行为的反面是,我引用了wiki "二进制文件,它使用的是间接链接共享库中的符号无法链接"

This was exactly the problem with me, as libconvert was using libnuma, but not linked to it, and the binary i was building tried to link everything including both libconvert and libnuma to it. The default gcc would work because it uses no-as-needed behavior which is changed in Debian and made it to Ubuntu as well.

这正是我的问题,因为libconvert使用libnuma,但没有链接到它,我构建的二进制文件试图将所有内容链接到它,包括libconvert和libnuma。默认的gcc会起作用,因为它使用了在Debian中更改的不需要的行为,并将其应用到Ubuntu。

So in short the linking works if i add -Wl,--no-as-needed.

因此,如果我添加-Wl,那么在短时间内链接就会起作用——不需要。

gcc -Wl,--no-as-needed -L/home/sam/gmdb/lib -L/home/sam/db/add-ons/lib -L/home/sam/convert/lib -L/home/sam/rtana/lib -L/home/sam/rtana/add-ons/lib -o /home/sam/gmdb/bin/server /home/sam/db/obj/tools/server/server.o /home/sam/db/obj/tools/common/tool_data_parse.o /home/sam/db/obj/tools/common/tool_param.o /home/sam/gmdb/obj/tools/common/tool_public.o -lgmcommon -L/home/sam/db/add-ons/vpp/lib/suse -lipsi_crypto -lipsi_osal -lipsi_pse -lipsi_ssl -lgmmd5 -lgmkernel -lgmpl -lgmrep -lgmsqlserver -lgmsqlclient -lconvert -lrtana -lglog -lgflags -lprotobuf -lre2 -lboost_timer -lnuma -lpthread -lm -lrt

gcc -Wl,-不需要-L/home/sam/gmdb/lib -L/home/sam/db/add-ons/lib -L/home/sam/ home/sam/rtana/lib -L/home/sam/rtana/add-on /add-ons/lib -o / home/sam/db/server/server/server/server/。o /home/sam/db/obj/tools/common/tool_data_parse。o /home/sam/db/obj/tools/common/tool_param。o /home/sam/gmdb/obj/tools/common/tool_public。l /home/sam/db/add-ons/vpp/ vpp/lib/suse -lipsi_crypto -lipsi_ssl -lgmr -lgmr -lgmr -lgmr -lgmsqlserver -lgmsqlserver -lgmsqlserver -lgmsqlserver -lgmsqlserver - lrgmsqlserver - lrms -lre2 -lre2 - lpro_timer -lpthread -lpthread -lm -lrt。

#1


4  

Guys thank you for your comments. The problem as i found that is because the default behavior of gcc is changed in Ubuntu (at-least the version i am using) The hint is on this wiki page of debian https://wiki.debian.org/ToolChain/DSOLinking

谢谢你的评论。我发现的问题是,因为gcc的默认行为在Ubuntu中发生了变化(至少我正在使用的版本),提示在debian https://wiki.debian.org/ToolChain/DSOLinking的wiki页面上。

According to this the gcc is changed to add --as-needed to the linker. The down side of this behavior is that and i quote the wiki " Binaries, which are using symbols from an indirectly linked shared library will fail to link "

根据这一点,gcc被更改为添加——需要链接器。这种行为的反面是,我引用了wiki "二进制文件,它使用的是间接链接共享库中的符号无法链接"

This was exactly the problem with me, as libconvert was using libnuma, but not linked to it, and the binary i was building tried to link everything including both libconvert and libnuma to it. The default gcc would work because it uses no-as-needed behavior which is changed in Debian and made it to Ubuntu as well.

这正是我的问题,因为libconvert使用libnuma,但没有链接到它,我构建的二进制文件试图将所有内容链接到它,包括libconvert和libnuma。默认的gcc会起作用,因为它使用了在Debian中更改的不需要的行为,并将其应用到Ubuntu。

So in short the linking works if i add -Wl,--no-as-needed.

因此,如果我添加-Wl,那么在短时间内链接就会起作用——不需要。

gcc -Wl,--no-as-needed -L/home/sam/gmdb/lib -L/home/sam/db/add-ons/lib -L/home/sam/convert/lib -L/home/sam/rtana/lib -L/home/sam/rtana/add-ons/lib -o /home/sam/gmdb/bin/server /home/sam/db/obj/tools/server/server.o /home/sam/db/obj/tools/common/tool_data_parse.o /home/sam/db/obj/tools/common/tool_param.o /home/sam/gmdb/obj/tools/common/tool_public.o -lgmcommon -L/home/sam/db/add-ons/vpp/lib/suse -lipsi_crypto -lipsi_osal -lipsi_pse -lipsi_ssl -lgmmd5 -lgmkernel -lgmpl -lgmrep -lgmsqlserver -lgmsqlclient -lconvert -lrtana -lglog -lgflags -lprotobuf -lre2 -lboost_timer -lnuma -lpthread -lm -lrt

gcc -Wl,-不需要-L/home/sam/gmdb/lib -L/home/sam/db/add-ons/lib -L/home/sam/ home/sam/rtana/lib -L/home/sam/rtana/add-on /add-ons/lib -o / home/sam/db/server/server/server/server/。o /home/sam/db/obj/tools/common/tool_data_parse。o /home/sam/db/obj/tools/common/tool_param。o /home/sam/gmdb/obj/tools/common/tool_public。l /home/sam/db/add-ons/vpp/ vpp/lib/suse -lipsi_crypto -lipsi_ssl -lgmr -lgmr -lgmr -lgmr -lgmsqlserver -lgmsqlserver -lgmsqlserver -lgmsqlserver -lgmsqlserver - lrgmsqlserver - lrms -lre2 -lre2 - lpro_timer -lpthread -lpthread -lm -lrt。