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

Compile C Plugin on macOS #13163

Open
tsl0922 opened this issue Dec 26, 2023 · 4 comments
Open

Compile C Plugin on macOS #13163

tsl0922 opened this issue Dec 26, 2023 · 4 comments

Comments

@tsl0922
Copy link
Contributor

tsl0922 commented Dec 26, 2023

I'm trying to build mpv-debug-plugin for macOS, but it failed to compile without a linking target (works on Linux).

[build] [ 92%] Linking CXX shared library debug.dylib
[build] Undefined symbols for architecture x86_64:
[build]   "_mpv_command_string", referenced from:
[build]       Debug::Console::ExecCommand(char const*) in debug.cpp.o

Also tried MPV_CPLUGIN_DYNAMIC_SYM, but got:

[build] [100%] Linking CXX shared library debug.dylib
[build] duplicate symbol '_pfn_mpv_set_property' in:
[build]     CMakeFiles/debug.dir/src/debug.cpp.o
[build]     CMakeFiles/debug.dir/src/main.cpp.o

It seems selectany only work on Windows. ping @kasper93

@tsl0922 tsl0922 added the os:mac label Dec 26, 2023
@Akemi
Copy link
Member

Akemi commented Dec 26, 2023

cplugins are not supported on macOS as far as i know.

also if you have problems building the plugin this is the wrong issue tracker to ask about.

@tsl0922
Copy link
Contributor Author

tsl0922 commented Dec 26, 2023

well, mpv on macOS does report it has cplugins feature enabled:

[cplayer] List of enabled features: av-channel-layout avif-muxer bsd-fstatfs build-date cocoa coreaudio cplugins darwin ffmpeg gl gl-cocoa glob glob-posix gpl html-build iconv javascript jpeg jpegxl lavu-uuid lcms2 libarchive libass libavdevice libbluray libdl libm libplacebo luajit macos-cocoa-cb macos-media-player macos-touchbar manpage-build osx-thread-name posix posix-shm rubberband rubberband-3 shaderc swift threads uchardet vapoursynth vector videotoolbox-gl videotoolbox-pl vk-khr-display vulkan zimg zimg-st428 zlib

BTW: I'm the plugin author, trying to make it support macOS

@tsl0922
Copy link
Contributor Author

tsl0922 commented Dec 26, 2023

It sould be possible to make it work with MPV_CPLUGIN_DYNAMIC_SYM, if the duplicate symbol issue can be resolved.

EDIT: Tested this simple program on macOS, it works (on plugin with single compile uint).

gcc plugin.c -DMPV_CPLUGIN_DYNAMIC_SYM -I/usr/local/opt/mpv/include -shared -o plugin.so

#include <stdio.h>

#include <mpv/client.h>

MPV_EXPORT
int mpv_open_cplugin(mpv_handle *handle)
{
    printf("mpv version: %lu\n", mpv_client_api_version());
    return 0;
}

so I get an ugly solution to make my plugin compile: cat all the source files to one and compile it.

@kasper93
Copy link
Contributor

kasper93 commented Dec 26, 2023

I'm trying to build mpv-debug-plugin for macOS, but it failed to compile without a linking target (works on Linux).

[build] [ 92%] Linking CXX shared library debug.dylib
[build] Undefined symbols for architecture x86_64:
[build]   "_mpv_command_string", referenced from:
[build]       Debug::Console::ExecCommand(char const*) in debug.cpp.o

I don't know much about macOS dynamic loading, it could work same as on Linux, but apparently it doesn't.

Also tried MPV_CPLUGIN_DYNAMIC_SYM, but got:

[build] [100%] Linking CXX shared library debug.dylib
[build] duplicate symbol '_pfn_mpv_set_property' in:
[build]     CMakeFiles/debug.dir/src/debug.cpp.o
[build]     CMakeFiles/debug.dir/src/main.cpp.o

It seems selectany only work on Windows. ping @kasper93

Indeed, selectany is Microsoft invention and intended to be used only for Windows targets. The whole mechanism of initializing pointer table MPV_CPLUGIN_DYNAMIC_SYM was made for Windows (as I though it is the only platform that needs it), which as you noticed works fine for other targets, but without selectany it has to be only in one translation unit.

For convenience and transparent use, we just define symbols in header and let selectany do the job of deduplicating things during linking.

We can make it more robust for macOS, but I'm not sure what is the best solution, as I don't use this platform. The first question is it really expected that symbols are not loaded to global lookup, same as on Linux?

maybe weak attribute could be use instead.

EDIT: Tested this simple program on macOS, it works (on plugin with single compile uint).

Idea was to make it transparent, so you don't even have to worry about it. If more targets needs this workaround, it might be better to introduce new API for function pointer query. But likely we can fix it somehow without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants