Search code examples
c++compilationlinkerarchive-file

Linking external library to project still gives undefined reference


I have a librdma.a which I need to link to my current project. When I run a nm on librdma.a, this is what I get:-

0000000000000000 T set_attribute

Which means that function is available in the code.

Now when I try to link this .a file to my current project and create a .a file for that using this Makefile.am:-

noinst_LTLIBRARIES=     libsrc.la

libsrc_la_SOURCES=      One.cpp \
                        Two.cpp \

libsrc_la_CPPFLAGS =    -I$(top_srcdir)/inc -I$(rdma_dir)/include

libsrc_la_LIBADD =      -L$(rdma_dir)/lib -lrdma

This creates a libsrc.a file, which should technically have the definition of set_attribute(), the function that I am trying to use. But when I run a nm on this archive file, I get an undefined reference.

                 U _Z8set_attributePP5attrlPcS2_

Also, when I try to make the complete project, that in turn uses this libsrc.a file, I get errors like these:-

../libfinal/.libs/libfinal.so: undefined reference to `set_attribute()'

Solution

  • It looks like you are mixing a C library with a C++ library, and the headers for the C library aren't wrapped in a extern C block. Hence the unmangled name, set_attribute, in the first library, and in the library it is referencing, you are getting a mangled name of _Z8set_attrPP5attrlPcS2_.