C++ 对“SDL_main”的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10803918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to 'SDL_main'
提问by Prismriver
I've recently decided to try working with SDL with CodeBlocks 10.05. I started with the tutorial on http://www.sdltutorials.com/sdl-tutorial-basicsand did my best to follow it. Unfortunately, I'm encountering:
我最近决定尝试在 CodeBlocks 10.05 中使用 SDL。我从http://www.sdltutorials.com/sdl-tutorial-basics上的教程开始,并尽力遵循它。不幸的是,我遇到了:
..\..\..\..\..\..\SDL\SDL-1.2.15\lib\libSDLmain.a(SDL_win32_main.o):SDL_win32_main.c|| undefined reference to `SDL_main'|
when I try to compile it.
当我尝试编译它时。
I've searched through many of the questions on this website and other tutorials (mainly the tutorial on LazyFoo and the CodeBlocks wiki) and can't seem to find a solution.
我在本网站和其他教程(主要是 LazyFoo 和 CodeBlocks wiki 上的教程)上搜索了许多问题,但似乎找不到解决方案。
- C:\SDL\SDL-1.2.15\include has been added in the Compiler tab (Search Directories)
- C:\SDL\SDL-1.2.15\lib has been added in the Linker tab
- The libraries libmingw32.a, libSDLmain.a, libSDL.dll.a are linked in that order
- libmingw32.a from the MinGW\lib folder in the CodeBlocks installation directory
- SDL.dll is in both the System32 folder and in the project folder
- C:\SDL\SDL-1.2.15\include 已添加到编译器选项卡(搜索目录)中
- C:\SDL\SDL-1.2.15\lib 已添加到链接器选项卡中
- 库 libmingw32.a、libSDLmain.a、libSDL.dll.a 按该顺序链接
- CodeBlocks 安装目录下 MinGW\lib 文件夹中的 libmingw32.a
- SDL.dll 位于 System32 文件夹和项目文件夹中
When attempting to follow the tutorial on the CodeBlocks wiki, I was told that SDL.h could not be found in the given directory (when making a new SDL project).
在尝试遵循 CodeBlocks wiki 上的教程时,我被告知在给定目录中找不到 SDL.h(在创建新的 SDL 项目时)。
CApp.cpp
应用程序
#include "CApp.h"
#include "SDL\SDL.h"
CApp::CApp(){
Surf_Display=NULL;
Running=true;
}
int CApp::OnExecute(){
if (OnInit()==false){
return -1;
}
SDL_Event Event;
while (Running){
while (SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main(int argc, char* argv[]){
CApp theApp;
return theApp.OnExecute();
}
CApp.h
应用程序
#ifndef CAPP_H_INCLUDED
#define CAPP_H_INCLUDED
#include "SDL\SDL.h"
class CApp{
private:
bool Running;
SDL_Surface* Surf_Display;
public:
CApp();
int OnExecute();
public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};
#endif // CAPP_H_INCLUDED
采纳答案by jrok
The only plausible reason for your problem I can think of is that when you created the file with main
in it, you forgot to add it to build targets.
我能想到的问题的唯一合理原因是,当您在其中创建文件main
时,您忘记将其添加到构建目标中。
You should see CApp.cpp in the list where my main.cpp is. Right click on it and click Properties. Click on Build tab in the window that pops up. You should see this:
您应该在我的 main.cpp 所在的列表中看到 CApp.cpp。右键单击它,然后单击属性。在弹出的窗口中单击 Build 选项卡。你应该看到这个:
Click OK, hit Ctrl+F11 (Rebuild).
单击确定,按 Ctrl+F11(重建)。
Good luck.
祝你好运。
回答by Ellias Mustellar
put these arguments to the main function. I had this problem too, and I fixed it few seconds ago.
将这些参数放到 main 函数中。我也有这个问题,几秒钟前我修复了它。
int main(int argv, char** args) { }
int main(int argv, char** args) { }
回答by Marcin Kaczmarek
Try #undef main
after all SDL related headers.
尝试#undef main
所有与 SDL 相关的标头。
Update. This is not a valid solution!
更新。这不是一个有效的解决方案!
As pointed out by HolyBlackCat, this is a pretty sloppy fix. SDL replaces the main function in order to perform some initialization and/or cleanup that is otherwise not possible, and then calls back to the actual user function.
正如 HolyBlackCat 所指出的,这是一个非常草率的修复。SDL 替换 main 函数以执行一些否则不可能的初始化和/或清理,然后回调到实际的用户函数。
The interception works by replacing the name of user's main function to SDL_main
, with a simple macro
拦截的工作原理是将用户的主函数的名称替换为SDL_main
,使用一个简单的宏
#define main SDL_main
The user's function then ceases to be the entry point for the application, and an entry point provided by SDL is used. The proposed #undef
disables the interception recklessly and one should argue that it is not supposed to work at all. For those who successfully compiled and ran an SDL application after this "fix", it must have simply been a platform-dependent coincidence.
然后,用户的函数不再是应用程序的入口点,而是使用 SDL 提供的入口点。该提议#undef
鲁莽地禁用了拦截,人们应该争辩说它根本不应该起作用。对于在此“修复”之后成功编译和运行 SDL 应用程序的人来说,这一定只是一个平台相关的巧合。
The proper solution to the OP's error is making sure that the file containing main
gets compiled and linked, and that the function has correct signature. As already posted by others.
OP 错误的正确解决方案是确保包含的文件main
被编译和链接,并且函数具有正确的签名。正如其他人已经发布的那样。
回答by BugHunterUK
Define SDL_MAIN_HANDLED
before you include the SDL libs:
SDL_MAIN_HANDLED
在包含 SDL 库之前定义:
#define SDL_MAIN_HANDLED
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
More info: I'm using the SDL functions without the SDL_main be defined. Is that fine?