c++ - 如何解耦 C++ 项目 header 依赖项?

标签 c++

“平台”是一个包含 SDL2 的 C++ DLL 项目。该项目配置有 SDL2 包含和库目录。 “Sample”是一个控制台应用程序,包含并链接到“Platform”,但不涉及 SDL2;除了我隐含地包含一个包含该库的东西之外。 “Sample”无法在“Platform”中的 cpp 文件上编译,错误是找不到 SDL2 的头文件。
我可以将 SDL2 包含目录添加到“Sample”项目中,但这会产生一种耦合,这似乎违背了我首先将“Platform”与“Sample”分开的原因。我查看了预编译的头文件,但这似乎解决了另一个问题;而且我无论如何也不知道如何让“样本”包含“平台”PCH。
错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C1083   Cannot open include file: 'SDL.h': No such file or directory    Sample  C:\Users\danie\Source\SingleScreen\Platform\display.h   6   
示例.cpp:
#include "platform.h"

int main(int argc, char** argv)
{

    Display_Configuration* display_configuration = new Display_Configuration();
    display_configuration->window_title = "Sandbox";
    display_configuration->dimension_x = 1280;
    display_configuration->dimension_y = 720;
    display_configuration->color_depth = 24;
    display_configuration->is_fullscreen = false;

    Platform* platform = new Platform(display_configuration);

    return 0;
}
平台.h:
#ifndef SINGLESCREEN_PLATFORM_PLATFORM_H
#define SINGLESCREEN_PLATFORM_PLATFORM_H

#ifdef  PLATFORM_EXPORTS 
#define PLATFORM_API __declspec(dllexport)  
#else
#define PLATFORM_API __declspec(dllimport)  
#endif

#include "display.h"
#include "controller.h"
#include "synthesizer.h"
#include "sampler.h"

extern PLATFORM_API class Platform
{
private:
    Display* _display;
    Controller* _controller;
    Synthesizer* _synthesizer;
    Sampler* _sampler;

public:
    Platform(Display_Configuration* display_configuration);
    ~Platform();
};

#endif //SINGLESCREEN_PLATFORM_PLATFORM_H
平台.cpp:
#include "pch.h"
#include "platform.h"

Platform::Platform(Display_Configuration* display_configuration)
{
    _display = new Display(display_configuration);
    _controller = new Controller();
    _synthesizer = new Synthesizer();
    _sampler = new Sampler();
}

Platform::~Platform()
{
    delete _display;
    delete _controller;
    delete _synthesizer;
    delete _sampler;
}
显示.h:
#ifndef SINGLESCREEN_PLATFORM_DISPLAY_H
#define SINGLESCREEN_PLATFORM_DISPLAY_H

#include <vector>
#include <string>
#include "SDL.h"

struct Display_Configuration
{
    std::string window_title;
    bool is_fullscreen;
    int dimension_x;
    int dimension_y;
    int color_depth;
};

class Display
{
private:
    std::string _window_title;
    bool _is_fullscreen;
    int _dimension_x;
    int _dimension_y;
    int _color_depth;
    SDL_Window* _window;
    SDL_Surface* _surface;

public:
    Display(Display_Configuration* display_configuration);
    ~Display();
    bool initialize();

};

#endif //SINGLESCREEN_PLATFORM_DISPLAY_H
显示.cpp:
#include "pch.h"
#include <iostream>
#include "display.h"


Display::Display(Display_Configuration* display_configuration)
{
    _window_title = display_configuration->window_title;
    _is_fullscreen = display_configuration->is_fullscreen;
    _dimension_x = display_configuration->dimension_x;
    _dimension_y = display_configuration->dimension_y;
    _color_depth = display_configuration->color_depth;
}

Display::~Display()
{
}

bool Display::initialize()
{
    _window = NULL;
    _surface = NULL;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "Failed to initialize the video subsystem. The error was:" << std::endl << SDL_GetError() << std::endl;
        return false;
    }
    
    _window = SDL_CreateWindow(
        _window_title.c_str(),
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        _dimension_x, _dimension_y,
        SDL_WINDOW_SHOWN | ( _is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : NULL )
    );

    if (_window == NULL) {
        std::cout << "Failed to create a rendering window. The error was:" << std::endl << SDL_GetError() << std:: endl;
        return false;
    }

    _surface = SDL_GetWindowSurface(_window);

    if (_surface == NULL) {
        std::cout << "Failed to create a rendering surface. The error was:" << std::endl << SDL_GetError() << std::endl;
        return false;
    }

    return true;
}

最佳答案

TL;博士:

  • 将包含从 header 移到源
  • 转发声明SDL_WindowSDL_Surface

  • 从您的个人资料来看,您最近决定学习编程。别担心,这很酷,你会弄清楚的。
    为什么我会这么认为?我花了一点时间才理解你的问题,因为 [Y] 你似乎对正确的词不知所措。
    您要问的是如何在 C++ 中正确隐藏实现细节。据我了解,您正在使用 SDL 开发库。并且您希望在不直接使用 SDL 的应用程序中使用此库。
    别担心,你可以做到的。你只需要解决两件事:
  • 删除行 #include "SDL.h"从任何公共(public)标题并将该行放入需要它的 cpp 文件中。如果它们打算由另一个目标(例如您的示例)使用,则 header 被认为是公开的。
  • 前向声明 SDL 的类型。

  • 向前看?
    编译器具有这样的性质,他们想知道一切。如果他们不明白这一点,您将被编译错误所困扰。
    当涉及到 SDL_Window 等类型时,编译器想知道:
  • 它存在吗?
  • 类型有多大?
  • 它有什么属性?
  • 它提供了哪些方法?

  • 幸运的是,我们可以通过使用所谓的“前向声明”来告诉爱管闲事的编译器不要管它自己的事情。我转发声明如下所示:
    // Example for your Display class.
    class Display;
    
    // Example for SDL
    struct SDL_Window;
    struct SDL_Surface;
    
    使用前向声明,我们做出 promise ,类型 Display , SDL_WindowSDL_Surface存在,但不包括它们的标题,它们将在其他地方定义。
    这允许我们存储指向这些类型实例的指针(或引用)。这样包含 #include "SDL.h"可以从标题 display.h 中移动到源display.cpp .您的样本不需要知道 SDL 的下落。
    重要的!没有定义就不能使用前向声明的类型。
    假设你转发声明了你自己的类 display在一个文件中;即像这样:
    class display;
    
    int main() {
        auto size = sizeof(display); // Nope!
        auto ptr = new display{};    // Nope!
        ptr->initialize();           // Nope!
    
        display object_from_somewhere_else;
        display* ptr2 = &object_from_somewhere_else; // Yes!
    
        return 0;
    };
    
    为了使类型再次可用,我们需要包含定义类型的 header 。
    class display;
    #include "display.h"
    
    或者
    #include "display.h"
    class display;
    
    
    int main() {
        auto size = sizeof(display); // Yes!
        auto ptr = new display{};    // Yes!
        ptr->initialize();           // Yes!
    
        display object_from_somewhere_else;
        display* ptr2 = &object_from_somewhere_else; // Yes!
    
        return 0;
    };
    

    我知道这一次可能很多。再忍耐一下,让我们看看最终结果会是什么样子:
    显示.h
    #ifndef SINGLESCREEN_PLATFORM_DISPLAY_H
    #define SINGLESCREEN_PLATFORM_DISPLAY_H
    
    #include <vector>
    #include <string>
    
    struct SDL_Window;
    struct SDL_Surface;
    
    struct Display_Configuration
    {
        std::string window_title;
        bool is_fullscreen;
        int dimension_x;
        int dimension_y;
        int color_depth;
    };
    
    class Display
    {
    private:
        std::string _window_title;
        bool _is_fullscreen;
        int _dimension_x;
        int _dimension_y;
        int _color_depth;
        SDL_Window* _window;
        SDL_Surface* _surface;
    
    public:
        Display(Display_Configuration* display_configuration);
        ~Display();
        bool initialize();
    
    };
    
    #endif //SINGLESCREEN_PLATFORM_DISPLAY_H
    
    显示.cpp
    #include "pch.h"
    #include <iostream>
    #include "display.h"
    
    #include "SDL.h"
    
    Display::Display(Display_Configuration* display_configuration)
    {
        _window_title = display_configuration->window_title;
        _is_fullscreen = display_configuration->is_fullscreen;
        _dimension_x = display_configuration->dimension_x;
        _dimension_y = display_configuration->dimension_y;
        _color_depth = display_configuration->color_depth;
    }
    
    Display::~Display()
    {
    }
    
    bool Display::initialize()
    {
        _window = NULL;
        _surface = NULL;
    
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            std::cout << "Failed to initialize the video subsystem. The error was:" << std::endl << SDL_GetError() << std::endl;
            return false;
        }
        
        _window = SDL_CreateWindow(
            _window_title.c_str(),
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
            _dimension_x, _dimension_y,
            SDL_WINDOW_SHOWN | ( _is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : NULL )
        );
    
        if (_window == NULL) {
            std::cout << "Failed to create a rendering window. The error was:" << std::endl << SDL_GetError() << std:: endl;
            return false;
        }
    
        _surface = SDL_GetWindowSurface(_window);
    
        if (_surface == NULL) {
            std::cout << "Failed to create a rendering surface. The error was:" << std::endl << SDL_GetError() << std::endl;
            return false;
        }
    
        return true;
    }
    

    关于c++ - 如何解耦 C++ 项目 header 依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63856394/

    相关文章:

    c++ - 数组的物理存储方式(特别是维度大于 2 的数组)?

    c++ - "unregistered void cast"使用派生到基类的 boost 序列化

    c++ - 具有操作数据重置功能的字符数组

    c++ - 创建一个计时器以在 X 秒内调用一个函数

    c++ - 类内部和外部的 static 关键字

    c++ - 使用托管和非托管 C++ 将 VS2008 升级到 VS2010 时出现问题

    C++ 主表达式 - 它是不是主表达式?

    c++ - 重复相同计算的优化

    c++ - Cmake 中的 Find_path 不起作用

    c++ - 如果成员具有非平凡的 noexcept 赋值运算符,则默认 move 赋值不能显式地为 noexcept