简体   繁体   English

C++ SDL2 - 无法理解的链接错误

[英]C++ SDL2 - Incomprehensible link error

I setup SDL2 under C++ CDT Eclipse.我在 C++ CDT Eclipse 下设置了 SDL2。 I added the include path "........SDL2-2.0.3\i686-w64-mingw32\include", the library path "........SDL2-2.0.3\i686-w64-mingw32\lib" and added the libraries 1."mingw32" 2."SDL2main" 3."SDL2".我添加了包含路径“........SDL2-2.0.3\i686-w64-mingw32\include”,库路径“........SDL2-2.0.3\i686-w64 -mingw32\lib" 并添加了库 1."mingw32" 2."SDL2main" 3."SDL2"。 So now, if I add a main.cpp with this content:所以现在,如果我添加一个包含以下内容的 main.cpp:

#include <SDL.h>

int main(int argc, char* args[])
{

    return 0;
}

I can build the project fine, but if I use this:我可以很好地构建项目,但是如果我使用它:

#include <SDL.h>

int main()
{

    return 0;
}

The project can't build and I get this error:该项目无法构建,我收到此错误:

Info: Internal Builder is used for build g++ "-IO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\include\SDL2" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\main.cpp" g++ "-LO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib" -o "SDL OpenGL Lab.exe" main.o -lmingw32 -lSDL2main -lSDL2 O:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib/libSDL2main.a(SDL_windows_main.o): In function console_main': /Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference to SDL_main' collect2.exe: error: ld returned 1 exit status信息:内部构建器用于构建 g++ "-IO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\include\SDL2" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o " ..\main.cpp" g++ "-LO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib" -o "SDL OpenGL Lab.exe" main.o -lmingw32 -lSDL2main -lSDL2 O:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib/libSDL2main.a(SDL_windows_main.o):在函数console_main': /Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference to SDL_main'collect2.exe 的引用:错误:ld 返回 1 退出状态

I simply wonder me why this error is depending on my main method, I want to use the main method I want.我只是想知道为什么这个错误取决于我的主要方法,我想使用我想要的主要方法。 Why this is so, how I can fix this ?为什么会这样,我该如何解决?

Sorry, you're stuck with it.对不起,你被它困住了。 I also preferred a simpler main, but SDL doesn't support it.我还喜欢更简单的 main,但 SDL 不支持它。

Here's a little more on SDL and main in the Windows world.这里有更多关于 SDL 和 Windows 世界中的主要内容。 It doesn't say why you need their version of main -- but you do.它没有说明为什么你需要他们的 main 版本——但你确实需要。

I get "Undefined reference to 'SDL_main'" ...我得到“对'SDL_main'的未定义引用”......

Make sure that you are declaring main() as:确保您将 main() 声明为:

#include "SDL.h" #include "SDL.h"

int main(int argc, char *argv[]) int main(int argc, char *argv[])

https://wiki.libsdl.org/FAQWindows https://wiki.libsdl.org/FAQWindows

I had the same problem and had to go through a lot of manipulations.我有同样的问题,不得不经历很多操作。 What I have found is the simple line #define SDL_MAIN_HANDLED before calling SDL.h.我发现的是在调用 SDL.h 之前的简单行#define SDL_MAIN_HANDLED。 SDL has his own main that's why, I think.我认为,SDL 有自己的主线,这就是为什么。 For example, in my .h files, I write everytime this :例如,在我的 .h 文件中,我每次都这样写:

#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED 
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM