CMake、转发声明、信号和 'No such file or directory'

CMake, forward declarations, signals and 'No such file or directory'

我正在尝试在 Qt 中开发一个与某些逻辑相关的用户界面。我已经成功地将信号从逻辑发送到用户界面,但现在我也需要反过来做。我在尝试编译项目时遇到 QMainWindow: No such file or directory 错误。

我确定这是 class 相互包含的问题,因此我尝试执行一些前向声明。但是我一直无法正确地做到这一点。试了很多方法好像都做不到。

我试图在客户端的头文件 class QTGUI; 中创建一个空的 class 并仅在 client.cpp 中包含 qtgui.hpp,以及与 QTGUI 文件。我做错了什么?

非常感谢您。


更新了 CMakeLists 和源代码。原来的东西在下面。

主要 CMakeLists

cmake_minimum_required(VERSION 2.6)
project(backendCode)

find_package(Boost COMPONENTS system)
include_directories(${Boost_INCLUDE_DIR})

SET(THREADLIBS "-lpthread -lboost_thread")

SET(FSLIBS "-lboost_filesystem -lboost_system")

add_subdirectory(gui)
add_subdirectory(server)

GUI CMakeLists: 与原始代码相比,我在此处包含了客户端内容。

cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(QtGui)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)

include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${QT_DEFINITIONS})


add_executable(main main.cpp qtgui.cpp ../common/inotify.cpp 
../client/client.cpp ../common/message.cpp)
target_link_libraries(main Qt5::Widgets Qt5::Network ${Boost_LIBRARIES} 
${THREADLIBS})

QTGUI.h

#ifndef QTGUI_H
#define QTGUI_H

#include <QMainWindow>
#include <QFileSystemWatcher>

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>

#include "../common/inotify.hpp"

class Client;

namespace Ui {
class QTGUI;
}

class QTGUI : public QMainWindow
{
    Q_OBJECT

public:
...
private:
    boost::shared_ptr<Client> client_;
    Ui::QTGUI* ui;
};
#endif // QTGUI_H

QTGUI.cpp

#include "QTGUI.h"
#include "ui_qtgui.h"

#include <iostream>

#include <QHostAddress>
#include <QRegExp>
#include <QIntValidator>
#include <QFileDialog>

#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

#include "../client/client.hpp"
...

CLIENT.HPP

#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>

#include <boost/signals2.hpp>

#include "../common/message.hpp"

class QTGUI;

class Client : public boost::enable_shared_from_this<Client>
{
    {
        public:
        ...
        private:
        boost::shared_ptr<QTGUI> ui_;
        };

        #endif // CLIENT_H

CLIENT.CPP

#include <boost/bind.hpp>

#include <cstdio> /* sprintf */

#include "client.hpp"
#include "../gui/QTGUI.h"

#include <unistd.h>

Client::Client():
...

原码

我的 main CMakeLists 中有以下内容:

cmake_minimum_required(VERSION 2.6)
project(backendCode)

find_package(Boost COMPONENTS system)
include_directories(${Boost_INCLUDE_DIR})

SET(BOOSTHREAD "-lpthread -lboost_thread")

SET(FSLIBS "-lboost_filesystem -lboost_system")

add_subdirectory(gui)
add_subdirectory(client)
add_subdirectory(server)

以下,在gui CMakeLists:

cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(QtGUI)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)

include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${QT_DEFINITIONS})


add_executable(main main.cpp qtgui.cpp ../common/inotify.cpp)
target_link_libraries(main client Qt5::Widgets Qt5::Network ${Boost_LIBRARIES} 
${BOOSTHREAD})

在 qtgui.h 头文件中,我有以下代码:

#ifndef QTGUI_H
#define QTGUI_H

#include <QMainWindow>

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/signals2.hpp>

#include "../client/client.hpp"

namespace Ui {
class QTGUI;
}
class QTGUI    : public QMainWindow
{
    Q_OBJECT
...
    private:
        boost::shared_ptr<Client> client_;
};
#endif

最后,在 client.hpp 文件中我有以下代码:

#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/thread.hpp>

#include <boost/signals2.hpp>

#include "../gui/qtgui.h"

class QTGUI;

class Client : public boost::enable_shared_from_this<Client>
{
    ...
private:
    boost::shared_ptr<QTGUI> ui_;
};
#endif

编辑:namespace 添加到 qtgui.h

您的客户端代码在没有 Qt 支持的情况下编译,包含一个包含 Qt header 的文件。这永远行不通。 client.hpp 中的 #include "../gui/qtgui.h" 行是问题的根源。

client.hpp 中的前向声明将毫无意义,因为您已经包含了 qtgui.h。选择一个或另一个。很可能,您不想qtgui.h包含在client.hpp中。那应该可以解决您的问题。