c++ - main 已经在 main.obj 中定义

标签 c++ sdl

我正在尝试使用这篇文章使用 Visual Studio 2019 设置 SDL 项目:

https://www.wikihow.com/Set-Up-SDL-with-Visual-Studio

但编译器向我抛出错误“找到一个或多个多重定义的符号”和
'_main 已在 main.obj 中定义'。

main.obj 是我项目调试文件夹中的一个文件,但当我尝试删除它或整个调试文件夹时,VS 在我运行项目时重新创建它。

我读过 c++ 不能有一个以上的 main 函数,但我无法打开 main.obj 文件,我真的不想删除 main.cpp 中的那个

这是我正在运行的代码,感谢您的帮助!

#include "SDL.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window* window = SDL_CreateWindow
    ("An SDL2 window", // window's title
        10, 25, // coordinates on the screen, in pixels, of the window's upper left corner
        640, 480, // window's length and height in pixels  
        SDL_WINDOW_OPENGL);

    SDL_Delay(3000); // window lasts 3 seconds
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

最佳答案

很高兴知道它现在可以工作了。也许您之前安装的 SDL 文件结构困惑。无论如何,我认为展示如何从 main.cpp 文件中删除 SDL 依赖项以避免将来出现此类问题可能会很有趣。

首先,让我们考虑一下您问题中的示例。这个例子在实践中不是很有用,但我会在之后展示一个更好的版本。

主要思想是从 main.cpp 和 header 中隐藏所有与 SDL 相关的内容。

<强>1。简单示例

// MySDL.h - NO SDL STUFF
class MySDL
{
public:
    MySDL() = default; // or whatever you need
    void runtest() const; // here we'll run the 3sec window
};

现在,我们可以将所有 SDL 内容放入 cpp 文件中:

// MySDL.cpp
#include "MySDL.h"
#include "SDL.h" // HERE WE INCLUDE SDL

void MySDL::runtest()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window* window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);

    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    SDL_Delay(3000);
}

main.cpp 中没有包含 SDL,我们只包含我们的 SDL 接口(interface) MySDL.h

// Now you can use your SDL interface like this
int main(int, char* [])
{
    MySDL sdl;
    sdl.runtest();

    return 0;
}

<强>2。更好的版本

但是,您通常需要比 3 秒后消失的窗口更复杂的东西。因此,您可能希望存储依赖于 SDL 的类成员。但是,你必须在你的 MySDL.h 头文件中 #include "SDL.h" ,这会给你带来与你的问题和评论中描述的相同的问题.要消除这种依赖性,我们可以使用 pimpl 习惯用法。

头文件现在包含指向我们的 SDL 实现的指针。此 SDL 实现将在 cpp 文件中定义,以删除 SDL 依赖项。

// MySDL.h
class MySDL
{
public:
    MySDL() = default; // or whatever you need
    ~MySDL();

    void doSmthWithYourWindow(/*args*/);

private:
    // pointer to our SDLImplementation (defined in cpp file)
    class SDLImplementation;
    std::unique_ptr<SDLImplementation> _sdl;
};

在我们的 cpp 文件中,我们定义了 SDLImplementation,MySDL 可以通过 _sdl 指针访问该实现。

// MySDL.cpp
#include "MySDL.h"
#include "SDL.h"

// here we can store class members which depend on SDL
struct MySDL::SDLImplementation
{
    SDLImplementation()
    {
        SDL_Init(SDL_INIT_EVERYTHING);
        _window = SDL_CreateWindow("yee haw", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
        _renderer = SDL_CreateRenderer(_window, -1, 0);
        SDL_SetRenderDrawColor(_renderer, 0, 255, 0, 255);

        SDL_RenderClear(_renderer);
        SDL_RenderPresent(_renderer);
    }

    // functionality of your SDL implementation
    void turnWindowUpsideDown() { /* _window->turnUpsideDown(); */ }

    // members depending on SDL
    SDL_Window* _window;
    SDL_Renderer* _renderer;
};

MySDL::~MySDL() = default;

void MySDL::doSmthWithYourWindow(/*args*/)
{
    // here we have access to our SDL implementation
    _sdl->turnWindowUpsideDown();
}

和以前一样,我们只在 main.cpp 文件中包含我们的 MySDL.h 接口(interface)。

int main(int, char* [])
{
    MySDL sdl;
    sdl.doSmthWithYourWindow();
    return 0;
}

关于c++ - main 已经在 main.obj 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56860861/

相关文章:

c++ - 在 C++ 中是否可以定义一个纯虚函数?

c++ - 我的逻辑在哪里失败?

c - 确定 SDL_surface 的宽度/高度的 SDL_function?

c - C 中的 .p 文件是什么?

c - SDL 窗口是透明的

c++ - 为什么我的 C++ 程序在特定输入时崩溃?

c++ - 一个空类和一个空结构是如何编译的?

c - SDL2不显示图像

c++ - Linux 中的 SDL 库

.net - 这段代码是线程安全的吗?