【问题标题】:Compiling: undefined reference "clock_gettime and memcpy" for Qt project编译:Qt 项目的未定义参考“clock_gettime 和 memcpy”
【发布时间】:2018-02-25 22:58:52
【问题描述】:

我尝试用 CentOS 编译 Qt 项目。 This 问题详细描述了我所做的事情和 我想通过引用this 来处理另一个glibc 库/users/my/lib64/(我无法更新/lib64/)。

这是编译输出:

g++ ./main.o ./moc_widget.o ./widget.o \
  -o ./test -Wl,--rpath=/users/my/lib64 \
  -Wl,--rpath=/users/my/Qt/5.9.1/gcc_64/lib \
  -Wl,--dynamic-linker=/users/my/lib64/libc.so.6 \
  -Wl,--dynamic-linker=/users/my/lib64/libz.so.1 \
  -L/users/my/Qt/5.9.1/gcc_64/lib -lQt5Widgets \
  -lQt5Gui -lQt5Core -lGL -lpthread  -lglib-2.0 -lrt -lX11 \
  -I/users/my/test/2 \
  -I/users/my/Qt/5.9.1/gcc_64/include \
  -I/users/my/Qt/5.9.1/gcc_64/include/QtWidgets \
  -I/users/my/Qt/5.9.1/gcc_64/include/QtCore \
  -I/users/my/Qt/5.9.1/gcc_64/include/QtGui

.pro 文件:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

CONFIG += qt

SOURCES += \
        main.cpp \
        widget.cpp 

HEADERS += \
        widget.h 

FORMS += \
        widget.ui 

gcc 版本:6.1.0

但是错误:

    /users/my/Qt/5.9.1/gcc_64/lib/libQt5Core.so: undefined reference to `clock_gettime@GLIBC_2.17'
    /users/my/Qt/5.9.1/gcc_64/lib/libQt5Widgets.so: undefined reference to `memcpy@GLIBC_2.14'
    collect2 ld returned exit 1 status

如何解决?

【问题讨论】:

  • 将您的.pro 文件添加到问题中,似乎是链接错误并包含您的 gcc 版本
  • 我很困惑你是想在本地机器上编译你的项目并将其部署到目标机器上还是你想在目标机器上编译项目
  • 谢谢。我修改我的问题。很抱歉,前者是理想的,但我无法在目标机器上运行应用程序,所以我尝试后者。
  • 创建一个简单的项目并构建。我想检查您是否可以只构建一个简单的应用程序?
  • 我做了一个简单的项目,只有关闭按钮,但是我使用Qtcreator并拿起发布项目,所以我对用cui构建不太了解。

标签: qt centos linker-errors glibc undefined-reference


【解决方案1】:
g++ ./main.o ./moc_widget.o ./widget.o \
 -o ./test -Wl,--rpath=/users/my/lib64 \
 -Wl,--rpath=/users/my/Qt/5.9.1/gcc_64/lib \
 -Wl,--dynamic-linker=/users/my/lib64/libc.so.6 \
 -Wl,--dynamic-linker=/users/my/lib64/libz.so.1 \
 -L/users/my/Qt/5.9.1/gcc_64/lib -lQt5Widgets \
 -lQt5Gui -lQt5Core -lGL -lpthread  -lglib-2.0 -lrt -lX11 \
 -I...

这个命令行完全是假的(你没看懂previous answer):动态链接器只能有一个,应该是/users/my/lib64/ld-linux-x86-64.so.2,而不是libz.so.1。通过使用多个--dynamic-linker=... 标志,您只是替换以前的(不正确的)设置为新的(也是不正确的)设置。

这也是假的,因为在 link 行上指定 -I... 标志而没有任何来源是没有意义的。

如果此命令成功,您最终会得到一个会立即崩溃的可执行文件,因为libz.so.1 不是动态链接器。

现在,您的链接失败了,因为您在错误系统上执行链接。您需要在 original 系统上进行链接(您之前成功链接二进制文件的系统,以及具有 GLIBC 2.17 或更高版本的系统)。然后将链接的可执行文件移动到您的目标系统。

在原始系统上,您的链接命令应如下所示:

g++ main.o moc_widget.o widget.o -o test \
   -Wl,-rpath=/users/my/lib64 \
   -Wl,--dynamic-linker=/users/my/lib64/ld-linux-x86-64.so.2 \
 -L...

我上面缩进的两行应该是唯一从你原来的成功链接命令的变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2022-10-15
    • 2023-03-08
    • 2023-03-13
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多