I have a project that contains two others (I use subdirs ). Calling the necessary functions from the subprojects is handled by the MainWindow class, which is, as it were, above them (that is, it is not a file from the subprojects).
The problem occurs when I try to access the subproject classes from the "control" file.
Errors:
- : -1: error: mainwindow.o: in function `MainWindow :: slot_showTable () ':
- undefined reference to `Crutch :: Crutch () '
- : -1: error: collect2: error: ld returned 1 exit status
Control file
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> // Здесь подключаются только GUI библиотеки, вроде QPushButton и др. //namespace Ui { //class MainWindow; //} class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void slot_showTable(); // В этом слоте происходит обращение к подпроекту private: // Тут GUI переменные, вроде QPushButton, QLabel и т.д. // Ui::MainWindow *ui; }; #endif // MAINWINDOW_H mainwindow.cpp
#include "mainwindow.h" //#include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) // ui(new Ui::MainWindow) { // ui->setupUi(this); createConnections(); } void MainWindow::createConnections() { // Нужный нам слот connect(btnShowBoardTable, &QPushButton::clicked, this, &MainWindow::slot_showTable); ... } #include "subproject/crutch.h" void MainWindow::slot_showTable() { Crutch *s = new Crutch(); // Здесь и появляется ошибка. Если попытаться обратиться к любому методу класса, то ошибка появляется и на этот метод тоже } File located in the subproject
crutch.h
// Этот файл находится в подпроекте #ifndef CRUTCH_H #define CRUTCH_H #include "mainwindow.h" // Этот файл находится в том же подпроекте, что и crutch.h, именно в нём располагается класс TableMainWindow #include <QObject> class TableMainWindow; class Crutch : public QObject { Q_OBJECT public: Crutch(); ~Crutch(); void showMainWindow(); private: TableMainWindow* mainWindow; }; #endif // CRUTCH_H crutch.cpp
#include "crutch.h" Crutch::Crutch() { mainWindow = new TableMainWindow;; } void Crutch::showMainWindow() { mainWindow->show(); } Crutch::~Crutch() { delete mainWindow; } As it seems to me, the problem lies in the line #include "subproject/crutch.h" but how to fix it I have not the slightest assumption.
PS In the above code, the mainwindow file is found twice, but these are two different files. Just one is in the subproject (it is found in crutch.h ), and the second is in the project’s header.
MergeAllProjects.pro
TEMPLATE = subdirs SUBDIRS = tablewindow SUBDIRS += build build.file = build.pro SUBDIRS += cfg_browser_sh build.pro
TEMPLATE = app QT += widgets SOURCES += main.cpp\ mainwindow.cpp HEADERS += mainwindow.h FORMS += mainwindow.ui Pro-file of the first (interaction with it) of the subproject
tablewindow.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = TableWindow CONFIG += c++11 #TEMPLATE = app TEMPLATE = lib # каталог, в котором будет располагаться результирующий исполняемый файл DESTDIR = $$OUT_PWD/bin MOC_DIR = moc OBJECTS_DIR = obj RCC_DIR = rcc UI_DIR = uic DEFINES += QT_DEPRECATED_WARNINGS include(cross/cross.pri) include(table_window/table_window.pri) INCLUDEPATH += $$PWD SOURCES += \ crutch.cpp \ mainwindow.cpp HEADERS += \ crutch.h \ mainwindow.h FORMS += \ mainwindow.ui RESOURCES += \ $$PWD/tablewindow.qrc There is also a .pri file, but it only contains a set of files in the same subfolder (headers, sources, forms)
Because there is no interaction with the second subproject (at the moment), I decided not to upload its .pro files (if necessary, I will provide them)