user1461001
user1461001

Reputation: 703

gcc undefined reference error on Ubuntu

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

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

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'

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

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

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.

My GCC version is

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

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

Upvotes: 4

Views: 4295

Answers (1)

user1461001
user1461001

Reputation: 703

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

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 "

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.

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

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

Upvotes: 7

Related Questions