Thread: Linking problem : can't find symbols in a library

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    7

    Linking problem : can't find symbols in a library

    Hello,

    I'm trying to compile a code using functions located in a library (libxenctrl), but ld cannot find the symbols :
    Code:
    gntring3_read_async : gntring3_read_async.o
    	gcc gntring3_read_async.o -lxenctrl /home/fremals/GVirtus9/modules/gntring/libgntring4.o -o ring3_read_async -lm
    Code:
    /home/fremals/GVirtus9/modules/gntring/libgntring4.o: in function  connect_ring :
    libgntring4.c:(.text+0x328): undefined reference to  xc_interface_open(xentoollog_logger*, xentoollog_logger*, unsigned int) 
    libgntring4.c:(.text+0x348): undefined reference to  xc_map_foreign_pages(xc_interface_core*, unsigned int, int, unsigned long const*, int) 
    libgntring4.c:(.text+0x365): undefined reference to  xc_interface_close(xc_interface_core*) 
    collect2: error: ld returned 1 exit status
    Code:
    fremals@fremals-System-Product-Name:~$ nm /usr/local/lib/libxenctrl.so | grep xc_interface_open
    0000000000016c39 T xc_interface_open
    00000000000169a3 t xc_interface_open_common
    fremals@fremals-System-Product-Name:~$ nm /usr/local/lib/libxenctrl.so | grep xc_interface_close
    0000000000016d26 T xc_interface_close
    0000000000016c8e t xc_interface_close_common
    fremals@fremals-System-Product-Name:~$ nm /usr/local/lib/libxenctrl.so | grep xc_map_foreign_pages
    000000000001e4a6 T xc_map_foreign_pages
    Does soemone know why ld cannot find the symbols please ?

    Thank you !!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Given that the linker is providing prototypes (i.e. it's including parameter types), I'm inclined to think you're building with a C++ compiler, which encodes that information. Had you built with a C compiler, the linker would likely just give you the function name, since that's all it has access to.

    If you are building with a C++ compiler, either switch to C, or when you include the header that declares these functions, tell the compiler they have C linkage:
    Code:
    extern "C"
    {
    #include <foobar.h>
    }
    I see that you're linking with gcc, not g++, but you didn't include how you built libgntring4.o, which is the offending object. If you are linking in C++ objects, you should link with your C++ compiler, not a C compiler. It will ensure that the proper standard libraries are linked in (plus any other extra stuff your C++ environment might require).

  3. #3
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Also, adding to what Cas said, I found sometimes the order of the libraries matter. For example, trying something like this might fix your problem:
    Code:
    gcc gntring3_read_async.o /home/fremals/GVirtus9/modules/gntring/libgntring4.o -o ring3_read_async -lm -lxenctrl

  4. #4
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    My interpretation is that the functions are located in that .so file that you searched. It says this about .so files: "The advantage of .so (shared object) over .a library is that they are linked during the runtime i.e. after creation of your .o file -o option in gcc. So, if there's any change in .so file, you don't need to recompile your main program."

    I found more information. It's called Runtime Linking. There are additional flags: LDADD= -Llibpath -Wl, -Rlibpath

    The -L adds to a library search path, where the libraries to resolve symbols are located. The -R is the runtime search path that is being linked to and which the linker uses to embed information. The -Wl flag passes flags from gcc to the linker.
    Last edited by FourAngels; 09-16-2015 at 10:08 PM.

  5. #5
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    Thank you for your answers, cas found the problem. libgntring is effectively compiled with g++. I don't understand why it was working before I reinstall linux, but it's working now, that's what matters. A big thank, I was blocked on this for a week ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking library problem
    By seibold in forum C Programming
    Replies: 6
    Last Post: 06-28-2010, 10:48 AM
  2. Symbols classing during linking
    By Angus in forum Linux Programming
    Replies: 8
    Last Post: 05-14-2009, 11:14 AM
  3. unresolved external symbols...linking errors in VC++
    By rammohan2b in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2009, 02:19 AM
  4. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  5. Library Linking problem
    By Lynchie in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:49 AM