Main.Obj中已经定义的主要

main already defined in main.obj

本文关键字:定义 Obj Main      更新时间:2023-10-16

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

https://www.wikihow.com/set-up-sdl-with-visual-studio

但是编译器正在为我抛出错误的一个或多个乘法定义的符号'和
'_ 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安装中有一个混乱的文件结构。无论如何,我认为说明如何从main.cpp文件中删除SDL依赖性可能会很有趣,以避免将来此类问题。

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

主要想法是隐藏与您的main.cpp和标题与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。更好的版本

但是,您通常需要比在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接口。

int main(int, char* [])
{
    MySDL sdl;
    sdl.doSmthWithYourWindow();
    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;
}