Skip to content

[BUG]: MacOS Compilation Error: Name collision between <complex.h> 's I macro and Coords I variables in components ( Union_master , Refractor ) #2546

Description

@MilanKlausz

Mostly AI generated bug report, but it's better than what I would write :)
I ran into issue when using SasView sample component together with Union_master, but the provided simple example reproduces the same error.

Describe the bug
On macOS, compiling instruments that include <tgmath.h> (e.g. when using SasView models or GPU-accelerated configurations) fails with compiler syntax errors when components like Union_master or Refractor are included in the same translation unit.

This occurs because <tgmath.h> automatically includes the C99 <complex.h> header, which defines the imaginary unit macro #define I _Complex_I. This macro collides with the local Coords I variable declarations used in the math sections of Union_master.comp and Refractor.comp.

Document steps to reproduce the bug
Steps to reproduce the behavior:
1. Save the following minimal instrument as test_collision.instr:

  DEFINE INSTRUMENT test_collision()
  
  DECLARE
  %{
  /* 
   * Simulates includes that pull in <complex.h> (like <tgmath.h> for type-generic math).
   */
  #include <tgmath.h>
  %}

  TRACE

  COMPONENT origin = Arm() AT (0,0,0) ABSOLUTE

  COMPONENT ref = Refractor(
    xwidth=0.1, yheight=0.1, zdepth=0.1
  ) AT (0,0,1) RELATIVE origin

  END
  1. Compile the instrument using the terminal command:
mcrun -c test_collision.instr
  1. See compiler syntax errors pointing to the variable I (which has been macro-expanded to _Complex_I):
test_collision.c:11257:16: error: expected identifier or '('
 11257 |         Coords I = coords_scale (V, 1 / v); // normalised ray = v/|v|
       |                ^
  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/complex.h:42:11: note: expanded from macro 'I'
     42 | #define I _Complex_I
         |           ^
  test_collision.c:11260:44: error: passing '_Complex float' to parameter of incompatible type 'Coords'
   11260 |         double cos_theta1 = -coords_sp (N, I); // cos(theta1) = -N.I
         |                                            ^

Expected behavior
The generated C code should compile successfully without syntax errors.

Screenshots
N/A

OS and McStas/McXtrace version (please complete the following information):

  • OS: macOS 15.7.7 (Build 24G720), x86_64
  • McCode variant: McStas
  • Version: 3.7.9

Add your code / dataset
See above.
Command used:

    mcrun -c test_collision.instr

(using Xcode SDK clang as the C compiler).

Additional context
The variable I is declared and used in at least the following places:
1. union/Union_master.comp (around line 2746):
c Coords I = coords_scale (V, 1 / v_length);
2. optics/Refractor.comp (around line 596):
c Coords I = coords_scale (V, 1 / v);

Suggested Fix:
Rename the I variable in the affected components to a less generic name (e.g. I_dir or I_vec representing the normalized ray direction). Using single-letter variables like I is highly prone to collisions with C99 headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions