Skip to content

Give run_mode and rel_max_lost_particles C linkage - #4050

Merged
paulromano merged 1 commit into
openmc-dev:developfrom
shimwell:fix-lib-settings-unmangled-globals
Aug 5, 2026
Merged

paulromano merged 1 commit into
openmc-dev:developfrom
shimwell:fix-lib-settings-unmangled-globals

Conversation

@shimwell

@shimwell shimwell commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Description

openmc.lib reads both of these globals and both are declared in namespace openmc::settings without extern "C", so the symbols are name-mangled and the lookup has never resolved.

On a Linux build of current develop:

>>> ctypes.c_int.in_dll(dll, 'run_mode')
ValueError: openmc/lib/libopenmc.so: undefined symbol: run_mode

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 18) on any C++ source files (if applicable)
    - [ ] I have followed the style guidelines for Python source files (if applicable)
    - [ ] I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

openmc.lib reads both of these through ctypes in_dll using their plain
names, but both were declared in namespace openmc::settings without
extern "C", so the symbols are name-mangled and the lookup has never
resolved. On a Linux build:

    >>> ctypes.c_int.in_dll(dll, 'run_mode')
    ValueError: openmc/lib/libopenmc.so: undefined symbol: run_mode

nm shows why: _ZN6openmc8settings8run_modeE and
_ZN6openmc8settings22rel_max_lost_particlesE. So
openmc.lib.settings.run_mode raises through its property getter, and
openmc.lib.settings.rel_max_lost_particles raises through the _DLLGlobal
descriptor. Every other global in this file that openmc.lib reads is
already declared extern "C", including max_lost_particles on the line
immediately above rel_max_lost_particles.

This is not platform specific. The two symbols are mangled on Linux,
macOS and Windows alike.

rel_max_lost_particles has been broken since it was introduced.
d763806 renamed it from relative_max_lost_particles on both the C++
and Python sides to make the names match, but with no extern "C" the
symbol never matched under either spelling.

After the change, both resolve and read the correct values, with
rel_max_lost_particles giving the 1.0e-6 default from settings.cpp:

    run_mode = 0                    (RunMode::UNSET)
    rel_max_lost_particles = 1e-06

solver_type is left alone since openmc.lib does not read it.

@nuclearkevin nuclearkevin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@paulromano
paulromano enabled auto-merge (squash) August 5, 2026 14:42

@GuySten GuySten left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

RunMode is not a C type.
According to Gemini we should declare it to inherit from some int type.

@paulromano

Copy link
Copy Markdown
Contributor

@GuySten when it is not declared to inherit from a type specifically, the standard says it is an int

@GuySten

GuySten commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

My bad.

@paulromano
paulromano merged commit c192830 into openmc-dev:develop Aug 5, 2026
15 checks passed
jon-proximafusion added a commit to shimwell/openmc that referenced this pull request Aug 11, 2026
The only conflict was in include/openmc/settings.h, where develop gained
the same C linkage for run_mode and rel_max_lost_particles from openmc-dev#4050
that this branch added for the Windows DLL exports. Resolved by keeping
develop's linkage together with this branch's OPENMC_API annotations and
the openmc/export.h include, which develop does not have.
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