main 已在 main.obj 中定义

main already defined in main.obj

我正在尝试使用这篇文章 Visual Studio 2019 建立一个 SDL 项目:

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

但编译器向我抛出错误 'one or more multiply defined symbols found' 和
'_main 已在 main.obj 中定义'。

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

我读过 c++ 不能有一个以上的主要功能,但我无法打开 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 并使用此处链接的不同教程完全重新启动:

https://www.youtube.com/watch?v=QQzAHcojEKg

不太确定有什么区别,但确实有效。不管怎样,谢谢你的帮助,我会把新代码放在这里。

#include "SDL.h"

int main(int argc, char* argv[])
{
    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);

    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 接口 MySDL.h

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

    return 0;
}

2。更好的版本

但是,您通常想要比 window 更复杂的东西,它会在 3 秒内消失。因此,您可能希望存储依赖于 SDL 的 class 成员。但是,您将不得不在 MySDL.h header 文件中添加 #include "SDL.h",这会给您带来与您的问题和评论中所述相同的问题。要删除这种依赖性,我们可以使用 pimpl 惯用语。

header 文件现在包含一个指向我们的 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 接口。

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