2

I need to use OpenGL as a library in my project on my Ubuntu 15.04 64bit PC, which is built by CMake 3.0.2. I install packages: mesa-common-dev mesa-utils-extra libgl1-mesa-dev libglu1-mesa-dev libglapi-mesa libx11-dev libxi-dev libxinerama-dev libxcursor-dev libxrandr-dev

After run cmake and Makefile, I got these link error:

/usr/bin/ld: /home/user/CMU462/DrawSVG/asst1_drawsvg/lib/libglfw.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libX11.so: error adding symbols: DSO missing from command line

I checked /usr/lib/x86_64-linux-gnu/libX11.so, it does exist.

I found a explanation, it seems I failed to link my project with X11 library. The answer says that add -lX11 option may fix this.

Alternatively I link X11 library in CMakeLists.txt according to FindX11.cmake:

find_package(X11 REQUIRED)

message(STATUS "X11_FOUND = ${X11_FOUND}")
message(STATUS "X11_INCLUDE_DIR = ${X11_INCLUDE_DIR}")
message(STATUS "X11_LIBRARIES = ${X11_LIBRARIES}")

include_directories(${X11_INCLUDE_DIR})

link_directories(${X11_LIBRARIES})

target_link_libraries(MyProj ... ${X11_LIBRARIES})

Run cmake I got these output:

-- X11_FOUND = 1
-- X11_INCLUDE_DIR = /usr/include
-- X11_LIBRARIES = /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so

But I still got the error aforementioned.

QUESTION: The X11 library can be linked by

  1. using target_link_libraries in CMakeLists.txt, or
  2. adding -lX11 option directly to compiling command

What's the difference between them? Does the link in CMakeLists.txt directly lead to a -lX11 option in Makefile generated?

If so, did I do something wrong in CMakeLists.txt?


UPDATE

Let's take this project as example, following is my build procedure.

Install required libraries: Install the OpenGL and other relative libraries (the GLEW and GLFW library is provided in this project): mesa-common-dev mesa-utils-extra libgl1-mesa-dev libglu1-mesa-dev libglapi-mesa libxi-dev libxinerama-dev libxcursor-dev libxrandr-dev

Run CMake: Then use the provided CMakeLists.txt, everything goes right.

Make: When make the project, this error occurred:

/usr/bin/ld: /home/user/CMU462/DrawSVG/asst1_drawsvg/lib/libglfw.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libX11.so: error adding symbols: DSO missing from command line

I have searched for many times, all of the answers say that the incorrect link of glfw3 and x11 lead to the error:

  1. missing the -X11 option. However in line 26 of CMakeLists.txt exists this option
  2. the libraries show arranged in order. I check the dependencies with pkg-config --libs command, all libraries are arranged in order in line 17 to 29 of CMakeLists.txt.

The only potential reason for this is the order of the compile options of libraries (line 17 to 29) and the target_link_libraries (line 116) of CMakeLists.txt.

Otherwise, there must be another error omitted by me during my procedure.

Community
  • 1
  • 1
stanleyerror
  • 670
  • 1
  • 9
  • 23
  • 3
    You should rely on `target_link_libraries` as this is the official and generic way. You know the name of X11, but this might theoretically vary for a different platform. Not in this case, but in general. Try removing `link_directories(${X11_LIBRARIES})`, you should only link by target_link_libraries as you do in the next line. – usr1234567 Dec 01 '15 at 07:46
  • 2
    Remove `link_directories(${X11_LIBRARIES})`, that variable contains file paths, not directory names. And you do not need to set `link_directories` because `X11_LIBRARIES` contains the full path. The error that you get is *after* ones add `-lX11` (or `target_link_libraries`). My guess is that you have also to link to opengl. https://cmake.org/cmake/help/v3.0/module/FindOpenGL.html – Antonio Dec 01 '15 at 11:02
  • Possibly related http://stackoverflow.com/questions/19901934/strange-linking-error-dso-missing-from-command-line and http://stackoverflow.com/q/28531014/2436175 – Antonio Dec 01 '15 at 13:06
  • @Antonio sorry to mislead you, I linked OpenGL in `CMakeLists.txt`, without posting the code here. As I see, based on the explanation in the question and the related answer in your comment, it is the incorrect link to the X11 library that lead to the error. I am seeking a proper way to do the link. – stanleyerror Dec 01 '15 at 14:07

0 Answers0