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

Adding declspec export attribute to header functions #220

Merged
merged 3 commits into from
Sep 9, 2024

Conversation

Honeybunch
Copy link
Contributor

@Honeybunch Honeybunch commented Apr 29, 2024

Hi there! I'm trying to get libmysofa added to vcpkg so it will be available to more users. Also steam-audio uses libmysofa and I'd like to use steam-audio from vcpkg 😄

See: microsoft/vcpkg#38368

For my vcpkg port I ran into an interesting problem that this patch addresses. Without this any downstream project that depends on libmysofa will fail to properly link to the library on windows. Not sure if this is the way you'd like to fix this. We could also do a .def file for the linker to mark these functions as part of the export .lib.

@umlaeute
Copy link
Collaborator

or build the library with gcc (rather than msvc) which has an option to export all the symbols.
(the library should then be usable even in an MSVC project)

@umlaeute
Copy link
Collaborator

sidenote: i'm not at all opposed against adding a MYSOFA_EXPORT.
however, i think this should not be limited to Windows.

in general i'm a big fan of using hidden symbols by default for libraries (only symbols explicitly exported via MYSOFA_EXPORT are made public), which is well supported by CMake (which even has functions to generate a header file with a proper MYSOFA_EXPORT definition).

so yes, but please do not stop at MSVC.

src/hrtf/mysofa.h Outdated Show resolved Hide resolved
@Honeybunch
Copy link
Contributor Author

or build the library with gcc (rather than msvc) which has an option to export all the symbols. (the library should then be usable even in an MSVC project)

In vcpkg the default compiler triplet for windows uses msvc so getting it to compile with msvc would be preferred

@Honeybunch
Copy link
Contributor Author

in general i'm a big fan of using hidden symbols by default for libraries (only symbols explicitly exported via MYSOFA_EXPORT are made public), which is well supported by CMake (which even has functions to generate a header file with a proper MYSOFA_EXPORT definition).

What the library currently does is generate a mysofa_export.h header from a config as you suggested. The problem is that this header is not installed so it can't be safely just included in mysofa.h. Which is only part of the problem, each function still needs to be marked up with MYSOFA_EXPORT define so that when a downstream project includes the header it knows that the functions have the declspec(import) property.

@umlaeute
Copy link
Collaborator

umlaeute commented May 2, 2024

The problem is that this header is not installed

so the solution is to install the header, no?

this of course would complicate the includes from a simple single mysofa.h to two dependent headers - which might not be desired.

@Honeybunch
Copy link
Contributor Author

I tried the vcpkg x64-mingw-dynamic and x64-mingw-static triplets and I can't find any issues with them. So it seems like this change is compatible with non-msvc compilers on Windows

@Honeybunch
Copy link
Contributor Author

this of course would complicate the includes from a simple single mysofa.h to two dependent headers - which might not be desired.

This is why I didn't do that. I wasn't sure what the preference was so I did it the way that I thought would be the least invasive

@umlaeute
Copy link
Collaborator

umlaeute commented May 2, 2024

So it seems like this change is compatible with non-msvc compilers on Windows

my comment was not about introducing incompatibilities, but about doing it properly rather than halfway: other systems (e.g. gcc on Linux) can benefit from the change as well.
but then MYSOFA_EXPORT needs to have a value on these systems.

also note the inconsistent behaviour mentioned in #220 (review)

@ColdenCullen
Copy link

It sounds like a solution to this is to define MYSOFA_EXPORT to __attribute__((visibility(default))) and compile with -fvisibility=hidden on non-Windows platforms (per this StackOverflow). @umlaeute I think that satisfies what you're asking for?

@Honeybunch
Copy link
Contributor Author

So TIL about cmake's generate_export_header

Looks like the way cmake expects you to use this is, yeah, to make sure the generated header is installed so downstream projects can include it.

@Honeybunch
Copy link
Contributor Author

I updated my port to use this and then made sure a downstream project could consume it and it looks to work

@umlaeute
Copy link
Collaborator

umlaeute commented May 3, 2024

@ColdenCullen said

It sounds like a solution to this is to define MYSOFA_EXPORT to __attribute__((visibility(default))) and

Yes, that's what I've been trying to say.

@Honeybunch
Copy link
Contributor Author

Honeybunch commented May 7, 2024

Any feedback for the latest version of this? Seems like I was wrong and the right answer was to include and install the header that Cmake generated

@Honeybunch
Copy link
Contributor Author

Hate to necro an old PR but wanted to swing back to this. What's the current feedback for this change as it currently exists?

@hoene hoene self-requested a review September 9, 2024 23:07
Copy link
Owner

@hoene hoene left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks reasonable for me.

@hoene hoene merged commit 1f9c8df into hoene:main Sep 9, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants