"平台";是一个包含SDL2的C++DLL项目。该项目配置有SDL2包含和库目录"样品";是一个控制台应用程序;平台";,但没有提及SDL2;除了我隐含的包含一个包含那个库的东西之外"样品";无法在"中的cpp文件上进行编译;平台";错误是找不到SDL2的头文件。
我可以将SDL2 include目录添加到";样品";项目,但这创造了一种耦合,似乎击败了我分离的原因;平台";从";样品";首先。我研究了预编译的头文件,但这似乎解决了另一个问题;我不知道如何获得";样品";包括";平台";无论如何,PCH。
错误:
Severity Code Description Project File Line Suppression State
Error C1083 Cannot open include file: 'SDL.h': No such file or directory Sample C:UsersdanieSourceSingleScreenPlatformdisplay.h 6
sample.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
platform.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;
}
display.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
display.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;DR:
- 将include从标头移动到源
- 前向声明
SDL_Window
和SDL_Surface
根据您的个人资料判断,您最近决定学习编程。别担心,这很酷,你会解决的。
为什么我这么认为?我花了一点时间才理解你的问题,因为你似乎找不到合适的词。
您要问的是如何在C++中正确隐藏实现细节。据我所知,您正在开发一个在后台使用SDL的库。您希望在不直接使用SDL的应用程序中使用此库。
别担心,你可以做到的。你只需要解决两件事:
- 从任何公共标头中删除
#include "SDL.h"
行,并将该行放入需要它的cpp文件中。如果标头要由另一个目标(如您的示例(使用,则将其视为公共标头 - 正向声明SDL的类型
转发瓦特
编译者的本性是,他们什么都想知道。如果他们不明白这一点,你就会被编译错误所困扰。
当涉及到像SDL_Window
这样的类型时,编译器想知道:
- 它存在吗
- 这个型号有多大
- 它具有哪些属性
- 它提供了哪些方法
幸运的是,我们可以通过使用所谓的";转发声明";。我转发声明如下:
// Example for your Display class.
class Display;
// Example for SDL
struct SDL_Window;
struct SDL_Surface;
使用前向声明,我们承诺类型Display
、SDL_Window
和SDL_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;
};
为了使类型再次可用,我们需要包含定义类型的标头。
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;
};
我知道这一次可能会有很多收获。请耐心等待我,让我们看看最终结果会是什么样子:
display.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;
}