Zpalmtree
Zpalmtree

Reputation: 1369

How to include boost header only library in Qt Creator

I'm trying to include a boost header only library, specifically, <boost/variant>, using a Qt Creator project.

This is what I have added to my .pro file:

INCLUDEPATH += /usr/include/boost \

Now, if I go into a .cpp file and try including..

#include <variant.hpp>

works, but

#include <boost/variant.hpp>

does not.

This makes sense, since I'm adding /usr/include/boost/, not /usr/include. However, if I instead do

INCLUDEPATH += /usr/include

I get a ton of cmath related errors, which seem to be related to adding /usr/include to the include path - https://github.com/android-ndk/ndk/issues/452

If I just do #include <variant.hpp> in my code, then the boost code complains - 'boost/variant/variant.hpp' file not found.

How do I correctly allow <boost/variant.hpp> style imports to work without including the whole of /usr/include/?

Minimal example code:

# Example.pro
QT += quick
CONFIG += c++17

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

# 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 <boost/variant.hpp>

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

    QGuiApplication app(argc, argv);

    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();
}

qml.qrc

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
        <file>main.qml</file>
    </qresource>
</RCC>
// main.qml
import QtQuick 2.12
import QtQuick.Controls 2.5

ApplicationWindow {
}

Thanks.

Upvotes: 1

Views: 962

Answers (1)

selbie
selbie

Reputation: 104589

This statement:

#include <variant>

Is pulling in the std C++ header file for variant, and gives your code access to std::variant, not the Boost version.

This statement:

#include <boost/variant>

Will likely not compile - because /usr/include/boost/variant is a directory itself, not a file that can be included.

If you want the Boost version of the variant header, I think you want this in your .cpp file:

#include <boost/variant.hpp>

And you shouldn't have to mess with your INCLUDEPATH assuming the /usr/include/boost folder exists.

Update Assuming /usr/include is the source of conflicting header files, one thing you could try is doing a symlink to boost from a different folder.

cd                              # cd to home directory
mkdir include                   # create a local "include" folder
cd include                      # cd to this folder
ln -s /usr/include/boost boost  # create a symlink to boost

Then update your include path to point to this directory

INCLUDEPATH += /home/yourname/include

Where /home/yourname is your home directory.

That will allow you to pull in <boost/variant.hpp> without potential conflicts elsewhere in /usr/include

Upvotes: 3

Related Questions