We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4875

Summary: CMakeLists.txt is missing some target link libraries
Product: sdl12-compat Reporter: Lyle <lyle.tafoya>
Component: everythingAssignee: Ryan C. Gordon <icculus>
Status: NEW --- QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: mkalte
Version: unspecified   
Hardware: x86_64   
OS: Linux   
Attachments: Give CMakeLists.txt a chance to find libs and add missing libs
Give CMakeLists.txt a chance to find libs and add missing libs

Description Lyle 2019-11-24 02:22:52 UTC
When I run ldd on my locally installed SDL 1.2 shared object file I get the following:

    linux-vdso.so.1 (0x00007ffc557f7000)
    libm.so.6 => /usr/lib/libm.so.6 (0x00007f2f0c219000)
    libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f2f0c214000)
    libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f2f0c1f2000)
    libc.so.6 => /usr/lib/libc.so.6 (0x00007f2f0c02b000)
    /usr/lib64/ld-linux-x86-64.so.2 (0x00007f2f0c44d000)

However, when using cmake to generate a Makefile, sdl12-compat does not get linked against all of these. This results in differences in regards to compiling applications which depend on them. A perfect example of this is the build of testsprite which occurs automatically and fails linking with undefined references to symbols which are found in libm and libdl. It looks like this:

    testsprite.c:(.text+0x35b): undefined reference to `sin'
    /usr/bin/ld: libSDL-1.2.so.1.2.50: undefined reference to `dlopen'
    /usr/bin/ld: libSDL-1.2.so.1.2.50: undefined reference to `dlclose'
    /usr/bin/ld: libSDL-1.2.so.1.2.50: undefined reference to `dlsym'

The same thing can be observed with the legacy Lazy Foo tutorials (which have require libdl). You can fix this by linking testsprite directly against libm and libdl, but this is not necessary when using vanilla SDL 1.2. That step is only necessary when using sdl12-compat. Therefor, these libraries should be linked against sdl12-compat to match the behavior. This can be accomplished by adding the following line immediately after line 4 in CMakeLists.txt:

    target_link_libraries(SDL dl m)

This still does not include libpthread as can be seen in the original SDL shared object file as I have not yet seen evidence of misbehavior due to that and I am not yet familiar enough with the project to say for certain that it won't work correctly without it.
Comment 1 Malte Kießling 2020-12-22 21:47:19 UTC
Created attachment 4596 [details]
Give CMakeLists.txt a chance to find libs and add missing libs

Afaik, you should not need pthread, as threading should be handled by SDL. 

Assuming that this should keep working in weird environments where Find_SDL, Find_X11 and the like are not available for whatever reason (cross compilation with borked roots come to mind), i have added a bit of code to try to find the include paths independent of that. This works for me, but YMMV.  

If anyone knows a file that is exclusive to sdl1 (as SDL_haptic.h is for sdl2), this would make this a better try at finding the header. A generally better way than this would also be preferable.  

~mkalte
Comment 2 Malte Kießling 2020-12-22 21:52:18 UTC
Created attachment 4597 [details]
Give CMakeLists.txt a chance to find libs and add missing libs

Oops, and of course, OP of this is right, linking should happen with the wrapper, not the test program. Sorry for the extra spam x.X