Help with "Variable used before defined" error in UDOs across include files

Hello Csound community,

I’m working on a complex project with multiple include files and UDOs, and I’m encountering a persistent compilation error related to global variables:

error: Variable 'gi_tc_source_density' used before defined
Line 81
from file udos/stateTransition.udo (12), from file main.csd (1),
error: Variable type for gi_tc_source_density could not be determined.
from file udos/stateTransition.udo (12), from file main.csd (1),
Parsing failed due to syntax errors
Stopping on parser failure
cannot compile orchestra

Project Structure

My project is structured as follows:

  • main.csd includes several .orc files
  • The variable gi_tc_source_density is defined in second.orc
  • It’s used in stateTransition.udo

I’ve verified that second.orc is included BEFORE stateTransition.udo:

#include "MACROS/first.orc"
#include "MACROS/second.orc"
#include "MACROS/debug.orc"

; Then UDOs
#include "udos/utils.udo"
...
#include "udos/stateTransition.udo"

Problematic Code

In second.orc, I have:

; Source state parameters
gi_tc_source_density init 0
gi_tc_source_register init 0
gi_tc_source_movement init 0

In stateTransition.udo, around line 81:

opcode selectNextState, iii, 0
    ; This line causes the error:
    iStateIdx = (gi_tc_source_density * 9) + (gi_tc_source_register * 3) + gi_tc_source_movement
    
    ; Rest of the code...
endop

What I’ve Tried

  1. Double-checking include order
  2. Adding explicit init statements at the beginning of main.csd
  3. Moving variable declarations to first.orc
  4. Checking for typos in variable names

I’m puzzled because the include order seems correct, and this worked before making some recent modifications to second.orc.

Questions

  1. Why would Csound report a variable as “used before defined” when it’s defined in an earlier include file?
  2. Could there be something specific about how UDOs process global variables across include files?
  3. Is there a way to debug the actual compilation order of included files?
  4. Would using explicit parameters instead of globals in the UDOs be a better approach?

Any guidance would be greatly appreciated. I’m considering restructuring my UDOs to use parameters instead of directly accessing globals, but I’d like to understand why this is happening first.

Thank you!