c++ - 在 QtCreator 中使用 oscpack 时出现 undefined reference 错误

标签 c++ linux qt qml qt-creator

我看过几个类似的帖子,但无法解决我的具体问题。

我想使用此处描述的 oscpack,
http://www.rossbencina.com/code/oscpack

并在此处下载,https://github.com/hecomi/qml-osc
然后单击“克隆或下载”按钮,然后单击“下载 ZIP”链接。

注意:我之前的链接,'https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/oscpack/oscpack_1_1_0.zip '是不正确的。

注2:我还在我的 Ubuntu 18.04 系统上安装了 oscpack:

$ sudo apt-get -y install liboscpack-dev liboscpack1

在我的项目中包含这个库之后——我添加头文件的那一刻,并且——我得到了多个 undefined reference 错误;例如:
undefined reference to `UdpSocket::Connect(IpEndpointName const&)'

无论我是否使用,我都会收到错误:
LIBS += -L"/developer/3rdPartyLibs/osc/lib" -loscpack # from the downloaded ZIP file

或者
LIBS += -L"/usr/lib/x86_64-linux-gnu" -loscpack  # from the linux package

在我的 SampleProject.pro 文件中。

我在这里看到了一个非常相似的帖子/问题:
Linking a static library to my project on Visual Studio 2010

而且我相信我有同样的错误,并且解决方案是相似的(只是我在 Ubuntu 18.04 而不是 Windows 上开发)。我只是还没弄清楚如何给链接器它需要的东西。我希望链接器找不到与“sys/sockets.h”关联的库,但我也不知道在哪里可以找到它。我正在使用带有 Qt_5_13_2_GCC_64bit-Debug 的 Qt Creator 4.10.2。

如果您不熟悉 QtCreator 但知道需要将哪些命令行参数传递给 ld(和/或 g++),那仍然会有很大帮助 - 我可以尝试找出在 IDE 中添加的位置。

我怀疑这个问题不需要额外的信息,但以防万一,代码如下。

谢谢!

最小项目(main.qml):
import QtQuick 2.13
import QtQuick.Controls 2.5
import QtQml 2.13
import OSC 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")


    Rectangle {
        width: 360
        height: 360
        Text {
            id: message
            anchors.centerIn: parent
        }
        OSCReceiver {
            port: 3333
            onMessage: {
                message.text = address + ' : ' + msg;
            }
        }
        OSCSender {
            id: osc
            ip: '127.0.0.1'
            port: 3333
        }
        Timer {
            property int cnt: 0
            interval: 1000/60
            running: true
            repeat: true
            onTriggered: {
                osc.send('/Hoge/cnt', ++cnt);
            }
        }
    }
}

项目文件(SampleProject.pro):
QT += quick

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

LIBS += -L"/developer/3rdPartyLibs/osc/lib" -loscpack   # add for oscpack

INCLUDEPATH += /developer/3rdPartyLibs/osc              # add for oscpack
include(/developer/3rdPartyLibs/osc/osc.pri)            # add for oscpack

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

和 main.cpp 文件:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

#include <osc_receiver.h> // add for oscpack
#include <osc_sender.h>   // add for oscpack

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    qmlRegisterType<OSCReceiver>("OSC", 1, 0, "OSCReceiver"); // add for oscpack
    qmlRegisterType<OSCSender>("OSC", 1, 0, "OSCSender");     // add for oscpack

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

这是输出:

14:27:49: Running steps for project SampleProject... 14:27:49: Configuration unchanged, skipping qmake step. 14:27:50: Starting: "/usr/bin/make" -j8 g++ -Wl,-rpath,/opt/Qt/5.13.2/gcc_64/lib -o SampleProject main.o osc_sender.o osc_receiver.o qrc_qml.o moc_osc_sender.o moc_osc_receiver.o -L/developer/3rdPartyLibs/osc/lib -loscpack /opt/Qt/5.13.2/gcc_64/lib/libQt5Quick.so /opt/Qt/5.13.2/gcc_64/lib/libQt5Gui.so /opt/Qt/5.13.2/gcc_64/lib/libQt5Qml.so /opt/Qt/5.13.2/gcc_64/lib/libQt5Network.so /opt/Qt/5.13.2/gcc_64/lib/libQt5Core.so -lGL -lpthread -loscpack osc_sender.o: In function OSCSender::setIp(QString const&)': /developer/builds/qt/build-SampleProject-Desktop_Qt_5_13_2_GCC_64bit-Debug/../../../3rdPartyLibs/osc/osc_sender.cpp:21: undefined reference toUdpSocket::Connect(IpEndpointName const&)' osc_sender.o: In function OSCSender::setPort(int)': /developer/builds/qt/build-SampleProject-Desktop_Qt_5_13_2_GCC_64bit-Debug/Makefile:255: recipe for target 'SampleProject' failed ../../../3rdPartyLibs/osc/osc_sender.cpp:38: undefined reference toUdpSocket::Connect(IpEndpointName const&)' osc_sender.o: In function OSCSender::send(QString const&, QString const&)': /developer/builds/qt/build-SampleProject-Desktop_Qt_5_13_2_GCC_64bit-Debug/../../../3rdPartyLibs/osc/osc_sender.cpp:55 ... ...many similar lines... ... undefined reference toSocketReceiveMultiplexer::~SocketReceiveMultiplexer()' /developer/builds/qt/build-SampleProject-Desktop_Qt_5_13_2_GCC_64bit-Debug/../../../3rdPartyLibs/osc/include/ip/UdpSocket.h:166: undefined reference to UdpSocket::~UdpSocket()' osc_receiver.o: In functionUdpListeningReceiveSocket::Run()': /developer/builds/qt/build-SampleProject-Desktop_Qt_5_13_2_GCC_64bit-Debug/../../../3rdPartyLibs/osc/include/ip/UdpSocket.h:169: undefined reference to SocketReceiveMultiplexer::Run()' osc_receiver.o:(.data.rel.ro._ZTI25UdpListeningReceiveSocket[_ZTI25UdpListeningReceiveSocket]+0x10): undefined reference totypeinfo for UdpSocket' collect2: error: ld returned 1 exit status make: *** [SampleProject] Error 1 14:27:51: The process "/usr/bin/make" exited with code 2.

最佳答案

解释

问题在于对库在 C++ 中如何链接的误解。集成 oscpack 的 qml-osc 项目使用静态链接(因此没有必要动态链接),但不幸的是 liboscpack.a 非常旧,会产生您观察到的问题。

解决方案

考虑到以上我做了fork of qml-osc消除了对静态库的依赖,所以必须使用动态链接。

考虑到上述情况,您必须遵循以下步骤:

  • 安装 liboscpack-dev
  • sudo apt-get install liboscpack-dev
    
  • 清理您的项目,使其具有以下结构(您可以从 here 下载文件):
  • ├── 59331063.pro
    ├── main.cpp
    ├── main.qml
    └── qml.qrc
    

    其中 .pro 如下:
    QT += quick
    CONFIG += c++11
    DEFINES += QT_DEPRECATED_WARNINGS
    SOURCES += main.cpp
    RESOURCES += qml.qrc
    
    INCLUDEPATH += /usr/include/oscpack
    LIBS += -loscpack 
    include($$PWD/3rdPartyLibs/qml-osc/osc.pri)
    
  • 然后创建一个名为 3rdPartyLibs 的文件夹并克隆我的存储库:
  • mkdir 3rdPartyLibs && cd 3rdPartyLibs && git clone https://github.com/eyllanesc/qml-osc.git
    

    最后,项目必须具有以下结构:
    ├── 3rdPartyLibs
    │   └── qml-osc
    │       ├── LICENSE
    │       ├── osc.pri
    │       ├── osc_receiver.cpp
    │       ├── osc_receiver.h
    │       ├── osc_sender.cpp
    │       ├── osc_sender.h
    │       └── README.md
    ├── 59331063.pro
    ├── main.cpp
    ├── main.qml
    └── qml.qrc
    


    目前我无法使用 ubuntu 访问机器,因此为了测试我之前的程序,我使用了以下 Dockerfile
    FROM ubuntu:18.04
    
    MAINTAINER eyllanesc <e.yllanescucho@gmail.com>
    
    RUN apt-get update && \
        apt-get autoclean
    
    RUN apt-get update && apt-get install \
      -y --no-install-recommends \
      git \
      ca-certificates \
      build-essential \ 
      qt5-default \
      qtdeclarative5-dev \
      qml-module-qtquick-controls \
      qml-module-qtquick-controls2 \
      qml-module-qtquick-window2  \
      liboscpack-dev
    
    COPY app app
    
    RUN mkdir app/3rdPartyLibs && \
        cd app/3rdPartyLibs && \
        git clone https://github.com/eyllanesc/qml-osc.git
    
    RUN mkdir app/build && \
        cd app/build && \
        qmake .. && \
        make
    
    CMD app/build/59331063
    

    关于c++ - 在 QtCreator 中使用 oscpack 时出现 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59331063/

    相关文章:

    c++ - QGraphicsScene 上的静态 QGraphicsItem

    C++ Lambda 函数闭包 - 内存问题

    c# - 何时在托管模式和非托管模式下使用 C++

    c++ - 打开 Unix,Arduino Uno 的 Linux 系统调用权限被拒绝

    c++ - 检查是否至少有一个 Wayland session 正在运行

    Qt - 弹出菜单

    ios - Xcode 错误 NSException

    c++ - 使用 32 位 Mersenne Twister 生成 64 位值

    c - C 代码与机器/编译器相关的段错误

    linux - 生成具有相同结构的多个文件