Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

sdl - Static-linking of SDL2 libraries

I am using Windows 7, Code::Blocks and MinGW. I have little to no experience when it comes to compiling/building anything, especially when Code::Blocks doesn't use makefiles.

I downloaded SDL2-devel-2.0.0-mingw.tar.gz (SDL Development Libraries) from http://www.libsdl.org/tmp/download-2.0.php, and I'd like to create a standalone executable using SDL2 libraries, but so far I've always had to bundle the SDL2.dll file with the executable to make it work.

I've heard that I can not static-link dynamic libraries, so my only option seems to be doing something with the source files using the file SDL2-2.0.0.tar.gz (Source Code) from the link I mentioned above. However, I do not know what I should do with those.

What I managed to try with the source files is importing the Visual Studio project to Code::Blocks and building it, but it tells me "sdl-config No such file or directory" (I do not know what triggered that). I'm also not sure if building merely gives me an executable, with which I do not know what I can do to link it to my own executable.

A fool proof idiot's step by step guide would be the best bet to solve this case.

EDIT:

I managed to compile the SDL libraries with the guide Jonas provided, and got a libSDL2.a file.

At first I only added the path of libSDL2.a to "Link libraries:" -section of Code::Blocks, but I got a bunch of errors such as "SDL_Init() not declared in this scope".

In addition to the libSDL2.a path, I also added the path of SDL2-2.0.0include to the Compiler's search directory as well as the path of SDL2-2.0.0uild.libs to the Linker's search directory. I also wrote this to my test file: #include "SDL.h". My test file now looks like this:

#include "SDL.h"

int main( int argc, char* args[] ) {
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
    //Quit SDL
    SDL_Quit();
    return 0;
}

It appears it did fix the declaration problem, but now Code::Blocks opened a SDL_mmjoystick.c file and gave me even more errors: "undefined reference to 'waveInClose@4'", "undefined reference to 'waveOutClose@4'", "undefined reference to 'joyGetNumDevs@0'" and tons of other ones.

Here's a screenshot of what's happening, note the different color of #include texts, I'm not sure why that happens: http://gyazo.com/00656a9c1e57a2bd0db1414fa7d68ced.png

I am not sure how to correctly take this library into use. Any help in this case, or should I make another question for it?

EDIT:

I added -lSDL2 to the linker options and deleted the other parameters. Now it builds fine:

mingw32-g++.exe -Wall -fexceptions  -g    -IC:UsersUserDesktopSDL2-2.0.0include  -c "C:UsersUserDesktopCppProjectsSDL projectmain.cpp" -o objDebugmain.o
mingw32-g++.exe -Wall -fexceptions  -g    -IC:UsersUserDesktopSDL2-2.0.0include  -c "C:UsersUserDesktopCppProjectsSDL projecthetestfile.cpp" -o objDebughetestfile.o
mingw32-g++.exe -LC:UsersUserDesktopSDL2-2.0.0uild.libs  -o "binDebugSDL project.exe" objDebugmain.o objDebughetestfile.o   -lSDL2  ....SDL2-2.0.0uild.libslibSDL2.a C:UsersUserDesktopSDL2-2.0.0uild.libslibSDL2.a  -mwindows
Output size is 945.80 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings (0 minutes, 1 seconds)

But when I try to run it, it says my computer lacks SDL2.dll, while the whole point was to static-link.

So currently I have the path to build/.libs in my Link libraries -settings, -lSDL2 in the Other linker options, and for search directories I have the path to SDL2-2.0.0/include for the compiler and SDL2-2.0.0/build/.libs for the linker.

In the build/.libs directory I can also see libSDL2.a, libSDL2.dll.a, libSDL2.la and libSDL2.lai files, which I don't know what they are.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not necessary to recompile the library, SDL2 is given with static-link library named "libSDL2.a" on the folder "SDL2-2.0.0i686-w64-mingw32lib".

Just be sure to add these options to the linker :

"-lmingw32 -lSDL2main -lSDL2 -mwindows -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -static-libgcc"

on Code:Blocks at "Project / Build Options... / Linket settings / Other linker options"

These options allow you to link with what SDL2.dll was using.

You can retreive them on the file "SDL2-2.0.0i686-w64-mingw32insdl2-config"

The magical trick is to delete or rename the file "libSDL2.dll.a" on the folder "SDL2-2.0.0i686-w64-mingw32lib". I added a "-" before to keep it in case I need it.

I don't know why this librairy overcomes the other and a clue would be appreciated.

I tried with Code::Blocks 12.11 MinGW32 and it worked.

If you run with some projects that use dynamic-link and some other which use static-link, you will have to keep your librairies in two different folders knowing that "libSDL2main.a" will be in those two.

Sorry for my writing, I'm not used to write in english.

Mike


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...