0

I started trying to develop a game engine. As a result, I cannot compile! Please tell me what should be fixed or added so that I can compile the DLL? I note that I'm a beginner in C++, and this is my first attempt to create a DLL library in C++.

So. The project has the following CPP files: dllmain.cpp (generate by VS), Engine.cpp, pch.cpp, World Game Engine.cpp. The project has header file Engine.h.

Code of 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;
}

So, I get a lot of errors.

I can’t create a screenshot because I have a Russian version of VS. But if necessary, I can switch to the English version and take a screenshot. SFML has been linked.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Studio
  • 221
  • 1
  • 4
  • You first have to explain why you can't. – drescherjm Apr 29 '20 at 16:39
  • Your compiler should use `stdafx.h` or `pch.h` not both. And whatever one you use it must be the first include. Modern versions of Visual Studio use `pch.h` for standard `c++` projects. 10+ year old versions used `stdafx.h` for all standard `c++` and MFC. – drescherjm Apr 29 '20 at 16:40
  • Related to the precompiled header naming: [https://stackoverflow.com/questions/51928685/can-i-use-include-pch-h-instead-of-include-stdafx-h-as-my-precompile-heade](https://stackoverflow.com/questions/51928685/can-i-use-include-pch-h-instead-of-include-stdafx-h-as-my-precompile-heade) – drescherjm Apr 29 '20 at 16:44
  • The project was generated by VS 2019. I can assume that everything is correct. – John Studio Apr 29 '20 at 16:51
  • ***I can assume that everything is correct.*** The code is not correct in the usage of precompiled headers. Most likely you want to get rid of `#include "stdafx.h"` and put `#include "pch.h"` at the top of each .cpp file. – drescherjm Apr 29 '20 at 16:55

1 Answers1

0

Both stdafx.h and pch.h are precompiled headers, the ones that Visual Studio creates for you, when you start a project. The only difference as far as I know (but here I might be wrong) is that, depending on the version of the VS, it either creates stdafx.h (older version), or pch.h (newer ones). You should only #include the header files that exist in your Project tree. Check it again, and delete one of the #include, depending on which header file you have.

Avoid spaces in the names of the header (.h) and implementation (.cpp) files. Your World Game Engine.cpp should be World_Game_Engine.cpp

After that, your include list in dllmain.cpp (main .cpp file) should look like:

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

Good luck!

RockOGOlic
  • 150
  • 1
  • 7
  • Yes. You were right. There was no stdafx.h in the headers. I deleted this from all the files, but the errors remained. – John Studio Apr 29 '20 at 16:56
  • #include "pch.h" must be before any code in each `.cpp` file. The compiler completly ignores all code before that line. This is how precompiled headers works. – drescherjm Apr 29 '20 at 16:59
  • try adding all the .cpp files you use in the #include of the dllmain.cpp – RockOGOlic Apr 29 '20 at 17:00
  • Errors are not related to this. C2653. Engine: not a class name or namespace C4430. missing type specifier - int is assumed. Note. C ++ does not support int by default ETC. – John Studio Apr 29 '20 at 17:04
  • No! Its not a good idea to include a `.cpp` file. As a professional developer I would never ever do that. [https://stackoverflow.com/questions/19547091/including-cpp-files](https://stackoverflow.com/questions/19547091/including-cpp-files) Instead the `.cpp` files should be added to your Visual Studio project as c++ source files. – drescherjm Apr 29 '20 at 17:04
  • ***Engine: not a class name or namespace*** That error is usually caused by a missing header or a circular include (where two or more headers include each other forming a loop of headers). With that said if you have `#include "Engine.h"` before `#include "pch.h"` that will cause this however I mentioned that multiple times already I assume the order of these is fixed. – drescherjm Apr 29 '20 at 17:10
  • @drescherjm, thanks for writing that! Solved my current issue too :) Will always keep in mind – RockOGOlic Apr 29 '20 at 17:23