Main.Obj中已经定义的主要
main already defined in main.obj
我正在尝试使用本文使用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;
}
- float* 已在 Gameobject.obj 中定义
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- C++ LNK 2005 错误"已在 .obj 中定义
- "... already defined in *.obj"但我没有在同一范围内两次定义任何变量
- 已经在 .obj 中定义,即使有 #ifndef #define #endif
- 需要的具体示例.错误:已经定义的.OBJ 一个或多个多个定义的符号
- Main.Obj中已经定义的主要
- 变量已在 .obj 中定义;这是怎么回事?
- 已在 ConsoleApplication1.obj 中定义
- DllMain 已在 dllmain.obj 中定义
- 已在main.obj中定义了双精度
- 错误LNK2005结构已在.obj文件中定义
- 已经在main.obj中定义了
- 变量已在 .obj 中定义,但没有循环包含
- 成员函数已在.obj(visual studio)中定义
- 链接器错误: /* 项实例 */ 已在项.obj 中定义
- 错误 11 错误 LNK2005:已在 MyForm.obj 中定义"class cv::Mat imageOutput" (?imageOutput@@3VMat@cv@@A)
- 错误 LNK2005:"class Player m_player"已在 Game.obj 中定义
- 已经定义了 Obj?链接错误
- LNK2005错误,已在main.obj中定义