Skip to content

Fix: Avoid symbol export when building statically with MSVC#1020

Merged
EmmanuelP merged 3 commits into
AravisProject:mainfrom
BrugolaOvoidale:fix/msvc-static-export
Oct 31, 2025
Merged

Fix: Avoid symbol export when building statically with MSVC#1020
EmmanuelP merged 3 commits into
AravisProject:mainfrom
BrugolaOvoidale:fix/msvc-static-export

Conversation

@BrugolaOvoidale

Copy link
Copy Markdown

🛠️ Fix: Avoid unnecessary symbol export with static MSVC builds

Summary

This patch ensures that __declspec(dllexport) is not applied when Aravis is built statically with MSVC. It updates the logic in meson.build to conditionally apply the ARV_API macro only for shared builds, improving compatibility and cleanliness of static builds on Windows.


🔍 Problem

When building Aravis statically with MSVC (e.g., via vcpkg with x64-windows-static triplet), the current build system still applies __declspec(dllexport) globally. This leads to:

  • Generation of .lib and .exp files next to user executables.
  • Unnecessary export symbols in static libraries.
  • Confusion in build setups expecting pure static linking behavior.

This behavior is not aligned with standard MSVC conventions, where symbol export macros are only needed in shared (DLL) builds.


✅ Solution

This patch modifies the logic in meson.build:

if cc.get_id() == 'msvc'
  if build_shared
    cc_export_define = '__declspec(dllexport) extern'
  else
    cc_export_define = 'extern'  # static build: no export
  endif
elif cc.has_argument('-fvisibility=hidden')
  if build_shared
    add_project_arguments('-fvisibility=hidden', language: 'c')
    cc_export_define = 'extern __attribute__ ((visibility ("default")))'
  else
    cc_export_define = 'extern'
  endif
else
  cc_export_define = 'extern'
endif

Now, when default_library is 'static', no __declspec(dllexport) is added — restoring correct static build behavior.

Also, this patch explicitly adds:

default_options: ['default_library=shared']

to the project() declaration in meson.build. This ensures that unless otherwise specified, builds will default to shared libraries, preserving existing workflows and expectations.

Users who want static builds can still explicitly override this with:

meson setup builddir --default-library=static

💡 Justification

This change:

  • Respects standard MSVC static linking conventions.
  • Avoids unintended .exp/.lib files when building user executables.
  • Improves compatibility with tools like vcpkg and Visual Studio.
  • Has no impact on non-MSVC or shared builds.
  • Does not alter the existing ARV_API macro or ABI.

🧠 Historical Context

This aligns with an earlier discussion in PR #550 (2021), where initial MSVC support was proposed. Back then:

  • ARV_API was added to manage symbol visibility.
  • The concern was raised about leaking symbols from static libraries.
  • Author noted that ARV_API should be conditional and not clutter headers unnecessarily.
  • This patch implements that feedback in a non-invasive, backwards-compatible way.

🧪 Testing

Confirmed working with:

  • Visual Studio 2022
  • MSVC toolchain (x64, /MTd)
  • Aravis built via vcpkg with x64-windows-static
  • No .exp/.lib artifacts appear at app link time.
  • Verified behavior is unchanged for shared builds.

🔗 Related

@EmmanuelP EmmanuelP requested a review from Copilot September 2, 2025 10:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes MSVC symbol export behavior when building Aravis statically by ensuring __declspec(dllexport) is only applied for shared builds, not static ones.

  • Adds conditional logic to only apply ARV_API export macros for shared MSVC builds
  • Sets default library type to shared to preserve existing build behavior
  • Improves GCC visibility handling consistency

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@AravisProject AravisProject deleted a comment from Copilot AI Sep 2, 2025
@EmmanuelP

Copy link
Copy Markdown
Contributor

Hi,

Please don't take offense, but I'd like to know if there is a real human behind this merge request that seems mostly AI generated. For example, there is a mention of a concern about symbol leaking in PR #550, but I did not find anything about that there.

@EmmanuelP EmmanuelP marked this pull request as draft October 29, 2025 15:03
@BrugolaOvoidale

Copy link
Copy Markdown
Author

Hi,

Please don't take offense, but I'd like to know if there is a real human behind this merge request that seems mostly AI generated. For example, there is a mention of a concern about symbol leaking in PR #550, but I did not find anything about that there.

Hi, no worries at all!.
Yes, I'm a real person and I wrote and tested the patch myself. I have to admit that I used AI only to help polish the wording and the visual of the PR description (it made it sound a bit fancier than I'd normally write). Sorry if that worried you.
About the "leaking" comment, yes PR #550 doesn't use that exact words, but there is a part in the discussion where you mentioned avoiding "leaking private API" from headers when ARV_API was added, and that's what I was referring to (#550 (comment)).
The main goal of this change is just to stop __declspec(dllexport) from being applied when doing static MSVC builds. It fixes the .exp/.lib artifacts that show up when linking user executables.
Again, sorry for the confusion!

@EmmanuelP EmmanuelP marked this pull request as ready for review October 30, 2025 16:13
@EmmanuelP

Copy link
Copy Markdown
Contributor

Please don't take offense, but I'd like to know if there is a real human behind this merge request that seems mostly AI generated. For example, there is a mention of a concern about symbol leaking in PR #550, but I did not find anything about that there.

Hi, no worries at all!. Yes, I'm a real person and I wrote and tested the patch myself. I have to admit that I used AI only to help polish the wording and the visual of the PR description (it made it sound a bit fancier than I'd normally write). Sorry if that worried you.

I'm not against AI use. It's just that compilation on Windows is not in my area of expertise, and I don't want to blindly accept related changes.

A minor issue about the default_options change: is this really needed ? It seems to set a default value which is already the default.

@BrugolaOvoidale

Copy link
Copy Markdown
Author

A minor issue about the default_options change: is this really needed ? It seems to set a default value which is already the default.

I wasn't sure about Meson's default for default_library and added it to be explicit, but since it's already shared by default, it's redundant, and it's better to remove it.

@EmmanuelP EmmanuelP merged commit b4b8407 into AravisProject:main Oct 31, 2025
5 of 7 checks passed
@EmmanuelP

Copy link
Copy Markdown
Contributor

Thanks a lot, Leonardo.

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.

3 participants