Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDL2_ttf] Missing C++ symbols statically linked with C #20756

Closed
5 of 7 tasks
aidenfoxx opened this issue Apr 27, 2024 · 16 comments
Closed
5 of 7 tasks

[SDL2_ttf] Missing C++ symbols statically linked with C #20756

aidenfoxx opened this issue Apr 27, 2024 · 16 comments
Labels

Comments

@aidenfoxx
Copy link

aidenfoxx commented Apr 27, 2024

Description / Steps to reproduce the issue

When statically linking "SDL2_ttf" in a C project, the linker throws errors for missing symbols. This can be worked around by including "stdlibc++", but this shouldn't be required?

Error

"cc"  -o example.exe example.exe.p/main.c.obj "-Wl,--start-group" "C:/Development/MSYS2/clang64/bin/../lib/libmingw32.a" "-mwindows" "C:/Development/MSYS2/clang64/bin/../lib/libSDL2main.a" "C:/Development/MSYS2/clang64/bin/../lib/libSDL2.a" "-mwindows" "-lm" "C:/Development/MSYS2/clang64/bin/../lib/libkernel32.a" "C:/Development/MSYS2/clang64/bin/../lib/libuser32.a" "C:/Development/MSYS2/clang64/bin/../lib/libgdi32.a" "C:/Development/MSYS2/clang64/bin/../lib/libwinmm.a" "C:/Development/MSYS2/clang64/bin/../lib/libimm32.a" "C:/Development/MSYS2/clang64/bin/../lib/libole32.a" "C:/Development/MSYS2/clang64/bin/../lib/liboleaut32.a" "C:/Development/MSYS2/clang64/bin/../lib/libversion.a" "C:/Development/MSYS2/clang64/bin/../lib/libuuid.a" "C:/Development/MSYS2/clang64/bin/../lib/libadvapi32.a" "C:/Development/MSYS2/clang64/bin/../lib/libsetupapi.a" "C:/Development/MSYS2/clang64/bin/../lib/libshell32.a" "C:/Development/MSYS2/clang64/bin/../lib/libdinput8.a" "C:/Development/MSYS2/clang64/bin/../lib/libSDL2_ttf.a" "-mwindows" "-mwindows" "C:/Development/MSYS2/clang64/bin/../lib/libbrotlicommon.a" "C:/Development/MSYS2/clang64/bin/../lib/libpng16.a" "C:/Development/MSYS2/clang64/bin/../lib/libz.a" "C:/Development/MSYS2/clang64/bin/../lib/libharfbuzz.a" "C:/Development/MSYS2/clang64/bin/../lib/libusp10.a" "C:/Development/MSYS2/clang64/bin/../lib/librpcrt4.a" "C:/Development/MSYS2/clang64/bin/../lib/libfreetype.a" "C:/Development/MSYS2/clang64/bin/../lib/libbz2.a" "C:/Development/MSYS2/clang64/bin/../lib/libglib-2.0.a" "C:/Development/MSYS2/clang64/bin/../lib/libintl.a" "C:/Development/MSYS2/clang64/bin/../lib/libws2_32.a" "C:/Development/MSYS2/clang64/bin/../lib/libshlwapi.a" "C:/Development/MSYS2/clang64/bin/../lib/libpcre2-8.a" "C:/Development/MSYS2/clang64/bin/../lib/libgraphite2.a" "C:/Development/MSYS2/clang64/bin/../lib/libbrotlidec.a" "-Wl,--subsystem,console" "-lkernel32" "-luser32" "-lgdi32" "-lwinspool" "-lshell32" "-lole32" "-loleaut32" "-luuid" "-lcomdlg32" "-ladvapi32" "-Wl,--end-group"
ld.lld: error: undefined symbol: operator new(unsigned long long)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shaper_face_data_create)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shaper_face_data_create)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shaper_face_data_create)
>>> referenced 2 more times

ld.lld: error: undefined symbol: operator delete(void*)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shaper_face_data_destroy)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(DWriteFontFileStream::~DWriteFontFileStream())
>>> referenced 1 more times

ld.lld: error: undefined symbol: operator new[](unsigned long long)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced 3 more times

ld.lld: error: undefined symbol: operator delete[](void*)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced by libharfbuzz.a(hb-directwrite.cc.obj):(_hb_directwrite_shape)
>>> referenced 5 more times

ld.lld: error: undefined symbol: __cxa_call_unexpected
>>> referenced by libgraphite2.a(gr_face.cpp.obj):(gr_make_face_with_ops)
>>> referenced by libgraphite2.a(gr_face.cpp.obj):(gr_make_face_with_ops)
>>> referenced by libgraphite2.a(Face.cpp.obj):(graphite2::Face::nameTable() const)
>>> referenced 41 more times

ld.lld: error: undefined symbol: __gxx_personality_seh0
>>> referenced by libgraphite2.a(gr_face.cpp.obj):(.xdata)
>>> referenced by libgraphite2.a(gr_face.cpp.obj):(GCC_except_table0)
>>> referenced by libgraphite2.a(gr_features.cpp.obj):(.xdata)
>>> referenced 30 more times

ld.lld: error: undefined symbol: vtable for __cxxabiv1::__class_type_info
>>> referenced by libgraphite2.a(Face.cpp.obj):(typeinfo for graphite2::Face)
>>> referenced by libgraphite2.a(CmapCache.cpp.obj):(typeinfo for graphite2::Cmap)

ld.lld: error: undefined symbol: vtable for __cxxabiv1::__si_class_type_info
>>> referenced by libgraphite2.a(CmapCache.cpp.obj):(typeinfo for graphite2::CachedCmap)
>>> referenced by libgraphite2.a(CmapCache.cpp.obj):(typeinfo for graphite2::DirectCmap)
cc: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Minimal reproduction

main.c;

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

int main(int argc, char *argv[]) {
    TTF_Font* font = TTF_OpenFontRW(SDL_RWFromConstMem(NULL, 0), 1, 16);
    TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
    return 0;
}

meson.build:

project('example', 'c', version: '1.0.0')
executable(
    'example', ['main.c'],
    dependencies: [
        dependency('sdl2', static: true),
        dependency('sdl2_ttf', static: true)
    ]
)

Expected behavior

The project is successfully linked.

Actual behavior

The linker throws an error for missing "stdlibc++" symbols.

Verification

Windows Version

MSYS_NT-10.0-19045

MINGW environments affected

  • MINGW64
  • MINGW32
  • UCRT64
  • CLANG64
  • CLANG32
  • CLANGARM64

Are you willing to submit a PR?

Probably not :(

@aidenfoxx aidenfoxx added the bug label Apr 27, 2024
@aidenfoxx aidenfoxx changed the title [SDL2_ttf] Missing C++ symbols when compiling with C [SDL2_ttf] Missing C++ symbols statically linked with C Apr 27, 2024
@aidenfoxx
Copy link
Author

Seems like there have been related issues with harfbuzz in the past: #2635

@Biswa96
Copy link
Member

Biswa96 commented Apr 27, 2024

For static linking you have to add all the required libraries. See the output of pkgconf -static -libs SDL2_ttf command.

@aidenfoxx
Copy link
Author

aidenfoxx commented Apr 27, 2024

@Biswa96 I believe meson automagically pulls in the pkgconfig dependencies. Comparing the first line of the above logs with the output of pkgconf, everything appears to be there.

@Biswa96
Copy link
Member

Biswa96 commented Apr 27, 2024

You are probably missing -static-libstdc++ flag.

@aidenfoxx
Copy link
Author

aidenfoxx commented Apr 27, 2024

Adding -static-libstdc++ doesn't help. I can add link_args: ['-lstdc++'] to my config to build successfully, but I don't understand why I should need to manually link against stdc++ when I meet the pkgconf dependencies for the package, and am building a purely C app.

@Biswa96
Copy link
Member

Biswa96 commented Apr 27, 2024

Those libraries depends on stdc++

@aidenfoxx
Copy link
Author

Is there documentation surrounding that? I didn't find anything on the SDL website. I would have thought the library should be compiled with static stdc++, rather than passing the buck to the user. Though maybe that's not possible.

@lazka
Copy link
Member

lazka commented Apr 28, 2024

Could be that meson fails to detect the c++ dep(?). Since adding link_language: 'cpp' to the meson file also seems to make it work here.

project('example',  ['c', 'cpp'], version: '1.0.0')
executable(
    'example', ['main.c'],
    link_language: 'cpp',
    dependencies: [
        dependency('sdl2', static: true),
        dependency('sdl2_ttf', static: true)
    ]
)

@aidenfoxx
Copy link
Author

pkgconf (which I believe meson uses under the hood) doesn't list C++ as a dependency for any of the related packages ("sdl2", "sdl2_ttf", "harfbuzz" or "graphite2").

It is of course not a big issue to link against C++, but it feels unintuitive for an end user building a C app, and wanting to use a C library.

@lazka
Copy link
Member

lazka commented Apr 28, 2024

I agree. It's not clear to me where this could/should be improved though.

@oscarfv
Copy link
Contributor

oscarfv commented May 3, 2024

pkgconf (which I believe meson uses under the hood) doesn't list C++ as a dependency for any of the related packages ("sdl2", "sdl2_ttf", "harfbuzz" or "graphite2").

It is of course not a big issue to link against C++, but it feels unintuitive for an end user building a C app, and wanting to use a C library.

You need to satisfy the library's dependencies, and the dependencies of the dependencies... pkgconf is failing to do that for you, but it's the same basic, messy stuff programmers are dealing with since the dawn of times.

@aidenfoxx
Copy link
Author

it's the same basic, messy stuff programmers are dealing with since the dawn of times

That shouldn't be to say we shouldn't fix it when we can! 😄

@Peter0x44
Copy link

Which dependency of SDL2_ttf needs C++/libstdc++?

@Peter0x44
Copy link

Or it's just a purely a build system misconfiguration?

@aidenfoxx
Copy link
Author

Both harfbuzz and graphite2 both depend on C++ symbols, but in this case graphite2 is a dependency of harfbuzz. Sdl2_ttf is not directly dependent on C++.

Interestingly, when sdl2_ttf is built using its bundled harfbuzz version, it does make sure to enable linking against c++: https://github.com/libsdl-org/SDL_ttf/blob/SDL2/CMakeLists.txt#L192

@Biswa96
Copy link
Member

Biswa96 commented May 19, 2024

This is by-design and not actionable from msys2 packaging perspective.

@Biswa96 Biswa96 closed this as completed May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants