No such file on library header files after converting calling classes to library themselves

370 Views Asked by At

I have a project where ClientLauncher is the executing subdir. I also have two libraries GUI and Metrics.

Initially ClientLauncher and GUI were "one", and that used the Metrics library and all was good. But I now get "filename.h": No such file or directory when compiling the program on every place where GUI includes something from Metrics.

The reason why I am changing this is because I want to be able to test all my code in another library UnitTests. For that reason I want to have everything in a library so that UnitTests can use that code.

So GUI is now a library, but it used to be the start of the program. To convert it to a library, I added TEMPLATE = lib to the .pro as you can see below.

ClientLauncher "launches" GUI. It only has main.cpp and includes "mainwindow.h" which is a header file in the GUI library.

ClientLauncher/main.cpp

#include "mainwindow.h"

int main(int argc, char *argv[]) {
     return 0; //Actual code doesn't cause error
}

GUI/mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "metricslib.h" //ERROR: metricslib.h: No such file or directory

#endif

Metrics/metricslib.h

#ifndef METRICSLIB_H
#define METRICSLIB_H

//Nothing that causes error

#endif

I'm using a subdirs project as a root:

TEMPLATE = subdirs

QT += widgets

CONFIG += qt

SUBDIRS += \
ClientLauncher \
Metrics \
GUI

ClientLauncher.depends = GUI
GUI.depends = Metrics

ClientLauncher.pro

QT += core gui charts network sql widgets

CONFIG += c++11 console gui c++14
CONFIG -= app_bundle

SOURCES += \
        main.cpp

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

unix|win32: LIBS += -L$$OUT_PWD/../GUI/ -lGUI

INCLUDEPATH += $$PWD/../GUI
DEPENDPATH += $$PWD/../GUI

GUI.pro

QT += core gui charts network sql widgets

TEMPLATE = lib

#DEFINES += GUI_LIBRARY

CONFIG += c++11 gui c++14

# Set icon
RC_ICONS = icon.ico

SOURCES += \
    mainwindow.cpp \

HEADERS += \
    mainwindow.h \

FORMS += \
    mainwindow.ui \

RESOURCES += \
    resources.qrc

## Metrics dependency ##
unix|win32: LIBS += -L$$OUT_PWD/../Metrics/ -lMetrics

INCLUDEPATH += $$PWD/../Metrics
DEPENDPATH += $$PWD/../Metrics

win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../Metrics/Metrics.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../Metrics/libMetrics.a

Metrics.pro

QT -= gui

TEMPLATE = lib

CONFIG += c++11 gui staticlib

HEADERS += \
    metricslib.h
2

There are 2 best solutions below

3
TheCloaker On

I am not sure I am reading this right I am a little tired but if I am right this is a simple fix.

so when include a existing file into your Project you have it in your project but... that is not how you include it you include the header file by the file path for example lets say the main file is desktop/project/main.cpp and one header is desktop/project/GUI/header.h and another is desktop/header2.h here is how you get to them from main.cpp,

   #include <GUI/header.h>

   #include <../header2.h>

I know it is a ruff example but I hope it helps have a good day!

0
The Coding Wombat On

So I found a solution, but I don't think this is what I am supposed to do.

By also making my root (ClientLauncher) depend on everything that GUI depends, and adding those libraries to the .pro of the ClientLauncher, I can use the libraries such as Metrics in GUI.

Now this seems like a weird "fix", since ClientLauncher doesn't use Metrics itself, so I'm still looking for a real solution.

So I changed ClientLauncher.depends = GUI to ClientLauncher.depends = GUI Metrics, and added the following to ClientLauncher.pro

unix|win32: LIBS += -L$$OUT_PWD/../Metrics/ -lMetrics

INCLUDEPATH += $$PWD/../Metrics
DEPENDPATH += $$PWD/../Metrics

win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../Metrics/Metrics.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../Metrics/libMetrics.a