【问题标题】:can't compile C++/SFML DLL. How to fix?无法编译 C++/SFML DLL。怎么修?
【发布时间】:2020-04-29 16:36:19
【问题描述】:

我开始尝试开发游戏引擎。结果,我无法编译!请告诉我应该修复或添加什么以便我可以编译 DLL?我注意到我是 C++ 的初学者,这是我第一次尝试在 C++ 中创建 DLL 库。

所以。该项目有以下 CPP 文件:dllmain.cpp(由 VS 生成)、Engine.cpp、pch.cpp、World Game Engine.cpp。该项目有头文件Engine.h。

Engine.h 代码:

#pragma once
#include <SFML/Graphics.hpp>

using namespace sf;

class Engine
{
private:

    RenderWindow m_Window;

    void input();
    void update(float dtAsSeconds);
    void draw();

public:
    Engine();

    void start();

};
Code of Engine.cpp:
#include "stdafx.h"
#include "Engine.h"
#include "pch.h"

Engine::Engine()
{
    Vector2f resolution;
    resolution.x = VideoMode::getDesktopMode().width;
    resolution.y = VideoMode::getDesktopMode().height;

    m_Window.create(VideoMode(resolution.x, resolution.y),
        "World Game Engine",
        Style::Fullscreen);

}

void Engine::start()
{
    Clock clock;

    while (m_Window.isOpen())
    {
        Time dt = clock.restart();
        float dtAsSeconds = dt.asSeconds();
        input();
        update(dtAsSeconds);
        draw();
    }
}
Code of World Game Engine.cpp:
#include "stdafx.h"
#include "Engine.h"
#include "pch.h"


int init()
{
    Engine engine;

    engine.start();

    return 0;
}

所以,我得到了很多错误。

我无法创建屏幕截图,因为我有俄罗斯版的 VS。但是如果有必要,我可以切换到英文版并截图。 SFML 已关联。

【问题讨论】:

  • 你首先要解释为什么你不能。
  • 你的编译器应该使用stdafx.h 或pch.h 不能同时使用。无论您使用什么,它都必须是第一个包含。现代版本的 Visual Studio 将 pch.h 用于标准 c++ 项目。 10 年以上的旧版本使用 stdafx.h 用于所有标准 c++ 和 MFC。
  • 项目由VS 2019生成,我可以假设一切都是正确的。
  • 我可以假设一切都是正确的。代码在使用预编译头时不正确。您很可能希望摆脱 #include "stdafx.h" 并将 #include "pch.h" 放在每个 .cpp 文件的顶部。

标签: c++ sfml


【解决方案1】:

stdafx.h 和 pch.h 都是预编译的头文件,它们是 Visual Studio 在您启动项目时为您创建的头文件。据我所知(但在这里我可能错了)唯一的区别是,根据 VS 的版本,它要么创建 stdafx.h(旧版本),要么创建 pch.h(新版本)。您应该只#include 项目树中存在的头文件。再次检查它,并删除#include 之一,具体取决于您拥有的头文件。

在头文件 (.h) 和实现 (.cpp) 文件的名称中避免使用空格。 你的 World Game Engine.cpp 应该是 World_Game_Engine.cpp

之后,您在 dllmain.cpp(主 .cpp 文件)中的包含列表应如下所示:

        #include "pch.h"
        #include "Engine.h"
        #include ... any other .h file you use

祝你好运!

【讨论】:

  • 是的。你是对的。标头中没有 stdafx.h。我从所有文件中删除了这个,但错误仍然存​​在。
  • #include "pch.h" 必须在每个 .cpp 文件中的任何代码之前。编译器完全忽略该行之前的所有代码。这就是预编译头文件的工作原理。
  • 尝试在 dllmain.cpp 的#include 中添加您使用的所有 .cpp 文件
  • 错误与此无关。 C2653。引擎:不是类名或命名空间 C4430。缺少类型说明符 - 假定为 int。笔记。 C++默认不支持int ETC。
  • 不!包含.cpp 文件不是一个好主意。作为一名专业开发人员,我永远不会这样做。 https://stackoverflow.com/questions/19547091/including-cpp-files 而应将 .cpp 文件作为 c++ 源文件添加到您的 Visual Studio 项目中。
猜你喜欢
  • 2018-10-28
  • 1970-01-01
  • 2015-08-13
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
相关资源
最近更新 更多