[c++] hide symbols that aren't in the C API #7496

Open
jameslamb opened 11:43pm on October 13, 2026 wants to merge 309 Ξ” into microsoft/lightgbm main from
fix/symbol-visibility
Reviewing
112 fewer changed lines
(29% less) vs GitHub
This saves about 50.8 hours per year vs conventional diff tools
jameslamb authored
of work between September 12 and October 22
Diff Delta:
309
About 32 Diff Delta/hour
Classified as:  Bug Fix, General

jameslamb's Description of Work

Notes for Reviewers

Contributes to https://github.com/lightgbm-org/LightGBM/issues/7327

By convention, we consider the LGBM_* functions in the C API to be the full public API of lib_lightgbm.{dll,dylib,so}. Everything else is considered an internal implementation detail and not intended to be accessed directly.

I say "by convention" because today there are 1,000+ symbols exported from the shared library on macOS / Linux. On my Mac using clang, I saw:


  • (991) various LightGBM internals, including tons of C++ types and methods

  • (98) intentionally-public LGBM_ functions

  • (59) various symbols from the vendored json11 code

  • (38) _Arrow* symbols from nanoarrow

  • (6) fmt:: symbols from fmt

  • (1) global locking variable from OpenMP (_.gomp_critical_user_.reduction.var)

code to see them (click me)

```shell
cmake -B build -S .
cmake --build build --target _lightgbm -j4
nm -gU ./lib_lightgbm.* | c++filt
```

This proposes limiting the visibility of all those unintentionally-exported symbols.

Benefits of these changes


  • reduced risk of conflicts with other libraries (which can cause segfaults, correctness problems, and other runtime issues)



  • reduced memory needed to load the library

  • faster library loading

  • faster compiled code (e.g. because the compiler can be more aggressive handling inlines because it knows they won't need to be used outside the library)

  • reduced binary size

See "Why is the new C++ visibility support so useful?" at https://gcc.gnu.org/wiki/Visibility for more.

Notes for Reviewers

I have been wanting to do this for a looooong time 😊

Is this safe?

I think so. I had an AI agent check (and I manually spot-checked) all the projects listed in https://github.com/lightgbm-org/LightGBM#external-unofficial-repositories. All but 1 of them either do not call into lib_lightgbm.{dll,dylib,so} at all or do so only through the C API. There's plenty of software that isn't public on GitHub but still... I think we should make this change.

The only exception is https://github.com/microsoft/LightGBM-transform, but that was abandoned 3 years ago, and builds its own copy of lib_lightgbm anyway, AND I'd like to drop support for it soon anyway: #7381

There's more work to do after this

I'd hoped this would fix https://github.com/lightgbm-org/LightGBM/issues/6595 but it doesn't, the reproducible example from there still reproduces.

But still, this will help prevent stuff like that!

References

19 total changed files
(4 files ignored)
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Loading changes...
Congrats, you've cleared the final file
All files reviewed β€” nice work, champ! πŸ’ͺ