Is it possible to shorten the path of files . h included by file . pri?

Asked

Viewed 84 times

1

I’m trying to separate my application from a small open lib I created, so I put the files on .pri and includes in the .pro, the structure of the folder was like this:

c:/projetos/
├── minhalib
│   ├── minhalib.pri
│   ├── foo
│   |   ├── foo.cpp
│   |   └── foo.h
│   └── bar
│       ├── bar.cpp
│       └── bar.h
|
└── aplicativo
    ├── aplicativo.pro
    ├── main.cpp
    ├── mainwindow.cpp
    └── mainwindow.h

In the app . pro I have this:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = aplicacao
TEMPLATE = app

SOURCES += main.cpp\
           mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

include($PWD/../../minhalib/minhalib.pri)

And in the minhalib/foo.pri I have this:

SOURCES  += $$PWD/foo/foo.cpp \
            $$PWD/bar/bar.cpp

HEADERS  += $$PWD/foo/foo.h \
            $$PWD/bar/bar.h

In the main.cpp I called it so:

#include "mainwindow.h"
#include "../minhalib/bar/bar.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Bar bar;
    bar.test();

    MainWindow w;
    w.show();

    return a.exec();
}

Note that I include so #include "../minhalib/bar/bar.h", but I would like to include in a simpler way, something like:

#include <bar/bar>
#include <foo/foo>

Or:

#include "bar/bar.h"
#include "foo/foo.h"

How can I do this by setting up .pri

1 answer

3


To solve just use the INCLUDEPATH +=, in the specific case just point the way with $PWD

INCLUDEPATH += $$PWD

SOURCES  += $$PWD/foo/foo.cpp \
            $$PWD/bar/bar.cpp

HEADERS  += $$PWD/foo/foo.h \
            $$PWD/bar/bar.h

Thus the compiler recognizes the paths thus:

#include "bar/bar.h"
#include "foo/foo.h"

A hint from colleague @Bacco to shorten further is to use set the full path directly on INCLUDEPATH +=, so for example:

INCLUDEPATH += $$PWD/foo \
               $$PWD/bar \

SOURCES  += $$PWD/foo/foo.cpp \
            $$PWD/bar/bar.cpp

HEADERS  += $$PWD/foo/foo.h \
            $$PWD/bar/bar.h

So don’t just include it like this:

#include "bar.h"
#include "foo.h"

You need to be extra careful to check that there are no files of the same name in the given directories

Browser other questions tagged

You are not signed in. Login or sign up in order to post.