我今天开始使用SDL并且之前遇到了一些问题,现在我已经开始运行了但它不会让我初始化它。
这是我的代码:
#include <iostream>
#include "SDL.h"
#undef main
using namespace std;
int main(){
if(SDL_Init(SDL_INIT_EVERYTHING)<0){
cout << "error starting sdl" << endl;
}
return 0;
}
这是构建日志:
-------------- Build: Debug in Graphics (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -std=c++11 -IC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\include\SDL2 -I"C:\Users\73638G75MA\Documents\C++ projects\Graphics" -c "C:\Users\73638G75MA\Documents\C++ projects\Graphics\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -LC:\Users\73638G75MA\Documents\SDL2-2.0.3\x86_64-w64-mingw32\lib -o bin\Debug\Graphics.exe obj\Debug\main.o -lmingw32 -lSDL2main -lSDL2
obj\Debug\main.o: In function `main':
C:/Users/73638G75MA/Documents/C++ projects/Graphics/main.cpp:8: undefined reference to `SDL_Init'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
我会在这方面提供所有可能的帮助,#undef main起初是因为它不会让我运行它。如果它不存在,它会给我一个未定义的引用winmain @ 16&#34;我正在创建一个控制台应用程序。
答案 0 :(得分:5)
根据包含和库搜索路径...\SDL2-2.0.3\x86_64-w64-mingw32\...
,您尝试使用64位SDL2进行构建。 从编译器名称根据Code::Blocks download page和我对codeblocks-13.12mingw内容的检查-setup.exe,包含的工具链仅为32位,不能创建64位二进制文件,也不能使用64位库。mingw32-g++
判断,我说你正在使用mingw.org工具链
如果您想使用预先构建的SDL2,您需要下载匹配的工具链(64位mingw-w64)并使用它,或者更改构建参数以使用32位版本的SDL2(它是出现在i686-w64-mingw32
目录中的development libraries archive。
答案 1 :(得分:2)
这里有几个错误。
<强> 1 强>
不要仅仅因为你不知道为什么会这样!当您使用SDL时,main()
(几乎)总是必须看起来像int main(int, char **) {}
。
因此,请删除#undef main
并将int main()
更改为int main(int, char **)
。
<强> 2 强>
构建x32可执行文件时,必须使用i686-w64-mingw32
而不是x86_64-w64-mingw32
的内容。因此,请正确调整编译标记。
x86_64-w64-mingw32
可能适用于x64应用程序,但我认为你正在构建x32应用程序。
答案 2 :(得分:0)
g++ -std=c++17 Test.cpp -I"include" -L"lib" -Wall -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -o Test
一些您可能尚未意识到的重要事情:
int main(int argc, char* argv[]) {
,因为SDL需要找到它来接手。我的文件结构如下:
Test/
include/
SDL.h
SDL_main.h
SDL_image.h
...
lib/
libSDL2.a
libSDL2.dll.a
libSDL2_image.a
...
licenses/
Test.exe
SDL2.dll
SDL2_image.dll
zlib1.dll
Test.cpp
sdl2-config
请注意,我的命令行位于Test的当前目录。
还要注意,我在环境变量路径上有mingw-w64 g ++可执行文件的文件夹。
还应注意的是,上面结构中的... s意味着里面应该还有更多内容,它们来自SDL给您的x86_64文件夹。
答案 3 :(得分:0)
这对我有用....转到 设置->编译器->链接器设置->在其他链接器选项框中,粘贴以下内容 -lmingw32 -lSDL2main -lSDL2