Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link an unknown library

Tags:

c++

For example I am trying to use GLFW3 and it throws the following error at me

x11_init.c:-1: error: undefined reference to `XIQueryVersion'.

I only know that it has something to do with X11 but how do I know which library I have to link? How would you tackle this problem?

like image 590
Maik Klein Avatar asked May 16 '13 12:05

Maik Klein


3 Answers

You could use nm to list the symbols defined in the system libraries in order to find which one contains your missing symbol :

find /usr/lib/ -type f -name \*.a \
  -exec nm -gAC --defined-only {} 2> /dev/null \; \
    | grep \ XIQueryVersion

Which outputs :

/usr/lib/x86_64-linux-gnu/libXi.a:XIQueryVersion.o:00000110 T XIQueryVersion

Then you know you have to link libXi.a, of course you can adjust the library path and symbol name, and that would only work if you already have the right library in your system.

like image 132
zakinster Avatar answered Oct 04 '22 02:10

zakinster


I would proceed in this order:

  1. look in the X11 doc to see if they tell you which lib to link to obtain this function
  2. look on the web if you can find a reference to this lib
  3. use nm on the X11 libs to see which one contains this reference

(shhhhhh... it's libxi)

like image 34
Gui13 Avatar answered Oct 04 '22 03:10

Gui13


I was getting similar linking errors. My error was saying:

/usr/local/lib/libglfw3.a(x11_init.c.o): In function `initExtensions':
x11_init.c:(.text+0x16d3): undefined reference to `XIQueryVersion'
/usr/local/lib/libglfw3.a(x11_window.c.o): In function `createWindow':
x11_window.c:(.text+0x6de): undefined reference to `XISelectEvents'

But since I wanted Cmake linking flags for the required linking libs, it took me a while to do so. Required external libs, to compile glfw based program, are:

Requires.private:  x11 xrandr xi xxf86vm gl

This thread shows how to find these libs needed to compile glfw based program.

Since, I spent 2-3 hours looking for Cmake linking flags for the above additional linking libs. I think it's worth mentioning here to help others.

Mainly for the error I have mentioned above, I just needed a cmake linking flag for xi only, but for completeness sake I am mentioning for all of them, i.e. x11 xrandr xi xxf86vm. Here is the snippet from my CMakeLists.txt file:

include_directories(
    ./src
    ${X11_xf86vmode_INCLUDE_PATH}
    ${X11_Xrandr_INCLUDE_PATH}
    ${X11_Xinput_INCLUDE_PATH}
)

    target_link_libraries(
        ${GLFW_LIBRARIES}
        ${X11_LIBRARIES}
        ${X11_Xxf86vm_LIB}
        ${X11_Xrandr_LIB}
        ${X11_Xinput_LIB}
    )

I extracted these flags from this link. Hope, it will save time for others. Enjoy!

like image 25
sinner Avatar answered Oct 04 '22 04:10

sinner