[CMake] Incorrect link order MSYS + SDL from target_link_libraries

Doug douglas.linder at gmail.com
Tue Sep 25 05:15:23 EDT 2012


Hm, I'm getting an incorrect link order when I try to link an SDL
application in MSYS.

The application linked via:

target_link_libraries(demo ${TEST_DEPS})

Where test deps is:

foreach(ITEM ${TEST_DEPS})
  message(": ${ITEM}")
endforeach()

-->

: gdi32
: opengl32
: glu32
: winmm
: E:/projects/tmp/libni/build/libni.dll
: zlib
: png
: nshared
: E:/projects/tmp/libni/build/deps/n/libn.dll
: lua
: m
: SDL2
: SDL2main
: stb
: ax
: sqlite3

This results in a link order:

cd /E/projects/tmp/libni/build/tests/demo/opengl &&
/E/MinGW/bin/gcc.exe  -Wall -pedantic -Wimplicit-function-declaration
-Wreturn-type -Wstrict-prototypes -Wextra -std=c99 -g
-Wl,--whole-archive CMakeFiles/test-ni-demo-opengl.dir/objects.a
-Wl,--no-whole-archive  -o test-ni-demo-opengl.exe
-Wl,--out-implib,libtest-ni-demo-opengl.dll.a
-Wl,--major-image-version,0,--minor-image-version,0  -lgdi32
-lopengl32 -lglu32 -lwinmm
../../../libni.dll ../../../deps/zlib/libzlib.dll.a
../../../deps/png/libpng.dll.a ../../../deps/n/libn.dll.a
../../../deps/n/libn.dll ../../../deps/lua/liblua.dll.a -lm
../../../deps/sdl/SDL/libSDL2.dll.a
../../../deps/sdl/SDL/libSDL2main.a ../../../deps/stb/libstb.dll.a
../../../deps/ax/libax.dll.a ../../../deps/sqlite3/libsqlite3.a
../../../deps/zlib/libzlib.dll.a -lm -luser32 -lgdi32 -lwinmm -limm32
-lole32 -loleaut32 -lversion -luuid -lmingw32 -mwindows
../../../deps/n/libn.dll -lpthread -lkernel32 -luser32 -lgdi32
-lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

Which fails to link; the library mingw32 requires the WinMain symbol
that is part of SDL2Main.a; linking with that as the final item works
(notice that libSDL2Main.a is now after -mingw32):

cd /E/projects/tmp/libni/build/tests/demo/opengl &&
/E/MinGW/bin/gcc.exe  -Wall -pedantic -Wimplicit-function-declaration
-Wreturn-type -Wstrict-prototypes -Wextra -std=c99 -g
-Wl,--whole-archive CMakeFiles/test-ni-demo-opengl.dir/objects.a
-Wl,--no-whole-archive  -o test-ni-demo-opengl.exe
-Wl,--out-implib,libtest-ni-demo-opengl.dll.a
-Wl,--major-image-version,0,--minor-image-version,0  -lgdi32
-lopengl32 -lglu32 -lwinmm
../../../libni.dll ../../../deps/zlib/libzlib.dll.a
../../../deps/png/libpng.dll.a ../../../deps/n/libn.dll.a
../../../deps/n/libn.dll ../../../deps/lua/liblua.dll.a -lm
../../../deps/sdl/SDL/libSDL2.dll.a ../../../deps/stb/libstb.dll.a
../../../deps/ax/libax.dll.a ../../../deps/sqlite3/libsqlite3.a
../../../deps/zlib/libzlib.dll.a -lm -luser32 -lgdi32 -lwinmm -limm32
-lole32 -loleaut32 -lversion -luuid -lmingw32 -mwindows
../../../deps/n/libn.dll -lpthread -lkernel32 -luser32 -lgdi32
-lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
../../../deps/sdl/SDL/libSDL2main.a

Any idea how I can trigger a dependency between a library that is not
locally build (mingw32) and a local static library in such a way as to
force the link order to be correct?

It's quite possibly an error in the SDL cmake config, but in general
to fix it I need a way to force a link order where mingw32 depends on
SDL2Main.

Currently (as far as I can tell) the link dependencies are basically:

target_link_libraries(SDL2 ${STUFF}) where STUFF contains both
SDL2Main and mingw32

Any ideas?

Cheers,
Doug.


More information about the CMake mailing list