dvanaria
dvanaria

Reputation: 6783

Issues when Statically Compiling SDL2 program

I'm working on a simple Hello World type program using C and SDL2 on a Windows platform (gcc compiler on Cygwin, running on Windows 7).

My program compiles fine and runs fine as long as I'm compiling with the dynamically linked libraries, and that the SDL2.dll file is in the same directory as my running program.

However, when I add the '-static' compiler flag to gcc line when compiling, I get a ton of "undefined reference" errors such as this:

undefined reference to `_imp__waveInAddBuffer@12'

Can anyone point me in the right direction of what the problem is here?

EDIT: I forgot to specify that I'm using MinGW as my compiler toolchain.

EDIT: I'm including the gcc compiler flag that worked (taken from the accepted answer below, plus a few additions from myself):

-Wl,-Bstatic -lmingw32 -lSDL2main -lSDL2 -Wl,-Bdynamic -lws2_32 -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinm m -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -lcomdlg32 -lhid -lsetupapi

Upvotes: 0

Views: 582

Answers (1)

keltar
keltar

Reputation: 18399

Your question appears to be windows-specific, things may (or not) be different on other systems, although some things are very much alike.

I don't think you can ever produce executable with cygwin without cygwin1.dll dependency.

-static prevents linker from ever considering any dynamic library, not just SDL. There are many dlls that are part of windows installation (so you can expect them to be present on target system) but they don't have static version, and if any of your program's code uses them (be it your code or, in that case, SDL), you can't produce executable with -static here. You can use static versions of some libraries though, e.g. SDL - that will require having corresponding .a (or .lib for MSVC), and specifying all dependencies said library have (as .a don't have dependency information built into it, while dynamic libraries do), e.g. for SDL2 it could be something like

-Wl,-Bstatic SDL2main SDL2 -Wl,-Bdynamic -lws2_32 -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid -lcomdlg32

(-Bstatic asks linker to use static verions of libraries that comes after that; -Bdynamic is reverse; -Wl,... passes option to linker)

If using mingw, you may also want --static-libgcc and --static-libstdc++ (and not depending on cygwin dll as an added bonus).

Upvotes: 2

Related Questions