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 5174

Summary: CMake build fails to link a shared library on OpenBSD
Product: SDL Reporter: Vitaly Novichkov <admin>
Component: buildAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: brad, sezeroz
Version: 2.0.12   
Hardware: x86_64   
OS: OpenBSD   
Attachments: A build output
My CMake cache to inspect

Description Vitaly Novichkov 2020-06-05 08:40:34 UTC
Trying to compile the latest hg SDL2 state, and I getting a failure at the final step:

```
[  0%] Linking C shared library libSDL2-2.0.so
CMakeFiles/SDL2.dir/src/SDL.c.o: In function `SDL_ExitProcess':
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL.c:(.text+0xf): undefined reference to `_Exit'
CMakeFiles/SDL2.dir/src/SDL_assert.c.o: In function `SDL_PromptAssertion':
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x36a): undefined reference to `memcpy'
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x67e): undefined reference to `__sF'
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x695): undefined reference to `fprintf'
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x69c): undefined reference to `__sF'
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x6b1): undefined reference to `fflush'
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x6bd): undefined reference to `__sF'
/home/admin/repos/TheXTech/build/external/SDLMixerX/src/SDLMixerX_Local-build/external/AudioCodecs/src/AudioCodecs-build/external/SDL2/src/SDL2HG/src/SDL_assert.c:(.text+0x6cc): undefined reference to `fgets'
...
```
Comment 1 Sam Lantinga 2020-06-05 18:09:09 UTC
It looks like somehow it's not linking with the standard C library... ?
Comment 2 Vitaly Novichkov 2020-06-05 19:48:49 UTC
Yes, libc didn't link, was that connected to the "enable libc" flag? Just now I reminded that I had a similar mess when I tried to build one of my projects on Windows with MSVC (in most I use MinGW, MSVC very rare), or something not that... don't remind accurate...
Comment 3 Sam Lantinga 2020-06-05 22:14:50 UTC
Yes, did you disable the "enable libc" flag? We don't expect that on UNIX, and that may not work correctly.
Comment 4 Vitaly Novichkov 2020-06-05 22:22:04 UTC
No, in my build I didn't set any libc flags at cmake build and I built it with most commmon case.
I had to pass these cmake flags only:
https://github.com/WohlSoft/AudioCodecs/blob/master/download_sdl2_hg.cmake#L57
No failure other platforms than OpenBSD. Didn't tested FreeBSD and NetBSD yet.
Comment 5 Vitaly Novichkov 2020-06-06 12:39:23 UTC
Created attachment 4372 [details]
A build output

For a test, I did the pure test:
- cloned the SDL hg repo
ran next:
```
sudo pkg_add ninja cmake
mkdir build
cd build
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release ..
ninja
```
and then I got this failure.
Comment 6 Vitaly Novichkov 2020-06-06 12:43:56 UTC
Created attachment 4373 [details]
My CMake cache to inspect

I see that libc flag is enabled, but the shared library fails to be built on a link step because of libc...
Comment 7 Brad Smith 2020-06-20 19:14:57 UTC
Looks like the CMake infrastructure has a bug. The autoconf build knows to not utilize --no-undefined on OpenBSD since that is not usable with shared libs. I was wondering why we had not seen this with our official packages but I haven't moved to CMake yet.
Comment 8 Brad Smith 2020-07-31 04:44:04 UTC
AC_MSG_CHECKING(for linker option --no-undefined)
have_no_undefined=no
case "$host" in
    dnl Skip this on platforms where it is just simply busted.
    *-*-openbsd*)
        ;;

    *)
        save_LDFLAGS="$LDFLAGS"
        LDFLAGS="$LDFLAGS -Wl,--no-undefined"
        AC_TRY_LINK([
        ],[
        ],[
        have_no_undefined=yes
        EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,--no-undefined"
        ])
        LDFLAGS="$save_LDFLAGS"
        ;;
esac
AC_MSG_RESULT($have_no_undefined)



  if(APPLE)
    list(APPEND EXTRA_LDFLAGS "-Wl,-undefined,error")
    list(APPEND EXTRA_LDFLAGS "-Wl,-compatibility_version,${DYLIB_COMPATIBILITY_VERSION}")
    list(APPEND EXTRA_LDFLAGS "-Wl,-current_version,${DYLIB_CURRENT_VERSION}")
  else()
    set(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined")
    check_c_compiler_flag("" HAVE_NO_UNDEFINED)
    set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
    if(HAVE_NO_UNDEFINED)
      list(APPEND EXTRA_LDFLAGS "-Wl,--no-undefined")
    endif()
  endif()

Not knowing much about CMake... I would think you could change the "else()" to something like  "elseif (NOT OPENBSD)"
Comment 9 Ozkan Sezer 2020-12-27 02:20:22 UTC
(In reply to Brad Smith from comment #8)
> Not knowing much about CMake... I would think you could change the "else()"
> to something like  "elseif (NOT OPENBSD)"

That change is now https://hg.libsdl.org/SDL/rev/c4ef403dbc59
Closing as fixed.