在 QtCreator 中使用 oscpack 时出现未定义引用错误

undefined reference errors when using oscpack in QtCreator

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

我想使用 oscpack,这里有描述, http://www.rossbencina.com/code/oscpack

并在此处下载,https://github.com/hecomi/qml-osc 然后点击按钮 'clone or download',然后点击 'Download ZIP' link.

注意:link 我之前的“https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/oscpack/oscpack_1_1_0.zip”不正确。

注2:我还在我的Ubuntu18.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 文件中。

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

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

如果您不熟悉 QtCreator 但知道需要将哪些命令行参数传递给 ld (and/or 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++ 中的 link 编辑方式的误解。集成 oscpack 的 qml-osc 项目使用静态 link(因此没有必要动态 link 它)但不幸的是 liboscpack.a 已经很老了,会产生您观察到的问题。

解决方案

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

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

  1. 安装 liboscpack-dev
sudo apt-get install liboscpack-dev
  1. 清理您的项目,使其具有以下结构(您可以从 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)
  1. 然后创建一个名为 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