GLFW - UBuntu 14.04 LTS上的链接器错误 [英] GLFW - linker errors on UBuntu 14.04 LTS

查看:418
本文介绍了GLFW - UBuntu 14.04 LTS上的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从此网站(位于底部)编制示例代码)。我下载了GLFW 3.0.4源代码,我在一个自定义位置构建它。我使用默认设置,GLFW被构建为静态库。我的CMakeLists.txt如下所示:

I want to compile an example code from this site (at the bottom). I downloaded GLFW 3.0.4 source code and I built it in a custom location. I used a default settings and GLFW was built as a static library. My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 2.8)
project(glfw_test_project)

SET(CMAKE_CXX_FLAGS "-Wall -Werror -g -std=c++11")

find_package(OpenGL REQUIRED)

include_directories(/home/user/MyLibs/OpenGL/glfw-3.0.4/include)
link_directories(/home/user/MyLibs/OpenGL/glfw-3.0.4/build/src)

add_executable(glfw_test main.cpp)

target_link_libraries(glfw_test ${OPENGL_gl_LIBRARY})
target_link_libraries(glfw_test glfw3)


b $ b

当我运行 make 时,我会得到以下信息:

When I run make I get following info:

Linking CXX executable glfw_test
/usr/bin/ld: /home/user/MyLibs/OpenGL/glfw-3.0.4/build/src/libglfw3.a(x11_clipboard.c.o): undefined reference to symbol 'XConvertSelection'
//usr/lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [glfw_test] Error 1
make[1]: *** [CMakeFiles/glfw_test.dir/all] Error 2
make: *** [all] Error 2

出了什么问题?


当我将GLFW构建为动态库时,一切正常。然后我的CMakeLists.txt中的最后一行必须替换为:

Edit1 When I build GLFW as a dynamic library everything works fine. Then the last line in my CMakeLists.txt have to be replaced with:

target_link_libraries(glfw_test glfw)

使用静态库时出现什么问题?

What is wrong when using a static library ?

推荐答案

我在Ubuntu 14.04 LTS上遇到了相同的问题。

I experienced the same problems on Ubuntu 14.04 LTS.

第一 :命令行缺少DSO 显示错误的命令链接器命令行参数。此帖子让我意识到。你需要调用 $ make VERBOSE = 1 来查看链接器调用,并用你自己的眼睛检查它。记住: TARGET_LINK_LIBRARIES()应该是 CMakeLists.txt 中的最后一个命令,并查看此订单说明

First: error adding symbols: DSO missing from command line shows a wrong order of linker command line parameters. This post made me realise that. You need to call $ make VERBOSE=1 to see the linker call and check it with your own eyes. Remember: TARGET_LINK_LIBRARIES() should be the last command in your CMakeLists.txt And have a look at this order explanation.

第二:如果你使用静态GLFW3库构建,所有的X11库都不会自动添加到你的链接器调用中,如上面的atsui所示。在 GLFW-文档中,您可以找到一个适合您需求的cmake变量: $ {GLFW_STATIC_LIBRARIES} 。要使用它并启用一个更自动的构建过程,让 pkg-config 找到X11 libs的所有依赖:

Second: if you build against static GLFW3 libraries all the X11 libraries are not automatically added to your linker call, as atsui already showed above. In the GLFW-Documentation you can find a cmake variable which should fit your needs: ${GLFW_STATIC_LIBRARIES}. To make use of it and enable a more automatic build process, let pkg-config find all dependencies for X11 libs:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(myProject)

FIND_PACKAGE( PkgConfig REQUIRED )
pkg_search_module( GLFW3 REQUIRED glfw3 ) # sets GLFW3 as prefix for glfw vars

# now ${GLFW3_INCLUDE_DIR}, ${GLFW3_LIBRARIES} and ${GLFW3_STATIC_LIBRARIES} 
# are set

INCLUDE_DIRECTORIES( ${GLFW3_INCLUDE_DIR} )
ADD_EXECUTABLE( myProject mySource.cpp )

TARGET_LINK_LIBRARIES( myProject ${GLFW_STATIC_LIBRARIES} )
# they include X11 libs

现在用 $ make VERBOSE = 1 再次检查链接器库参数,你应该找到更多的X11库像:
-lXrandr -lXi -lXrender -ldrm -lXdamage -lXxf86vm -lXext -lX11 -lpthread

Now check the linker lib parameters again with $ make VERBOSE=1 and you should find many more X11 libs like: -lXrandr -lXi -lXrender -ldrm -lXdamage -lXxf86vm -lXext -lX11 and -lpthread.

第三:链接到共享库时,X11等的依赖关系会动态解析。这就是为什么你不需要显式地添加X11 lib标志到链接器。在这种情况下,您只需要:
TARGET_LINK_LIBRARIES(myProject $ {GLFW_LIBRARIES})

Third: when linking against the shared library, the dependencies to X11 etc. are resolved dynamically. That is why you don't need to explicitly add X11 lib flags to the linker. In that case you only need: TARGET_LINK_LIBRARIES( myProject ${GLFW_LIBRARIES} )

第四:发生在我身上,有一些glfwCommands有未定义的引用 - 链接器错误。我发现,我在 / usr / local / lib 中安装了GLFW3,但链接器只给出了 LD_LIBRARY_PATH 环境变量只设置为 / usr / lib32 。添加参数 -L / usr / local / lib 解决了引用问题。为了避免在将来我设置 LD_LIBRARY_PATH = / usr / lib32; / usr / local / lib;

Fourth: it happened to me, that some glfwCommands had undefined reference to - linker errors. I found out, that I installed GLFW3 in /usr/local/lib but the linker was given only the LD_LIBRARY_PATH environment variable which was only set to /usr/lib32. Adding the parameter -L/usr/local/lib solved the reference problem. To avoid it in future I've set LD_LIBRARY_PATH=/usr/lib32;/usr/local/lib;.

提示:检查GLFW cmake变量中的值,并在生成期间打印:

Hint: to check what values are in your GLFW cmake variables, print them during build:

FOREACH(item ${GLFW3_STATIC_LIBRARIES})
    MESSAGE(STATUS "  using lib: " ${item})
ENDFOREACH()

这篇关于GLFW - UBuntu 14.04 LTS上的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆