Controlling placement of `cdef extern from` blocks

5 views
Skip to first unread message

Jacob Faibussowitsch US

unread,
Jun 10, 2025, 4:54:57 PMJun 10
to cython-users
I have a function defined in C code (in an `cdef extern from` block) where I want to detect if CYTHON_TRACE is defined. However, it seems that `extern from` blocks are placed above the section where all of the CYTHON_* pre-processor variables are handled.

I then tried to do it in a python function:
```
cdef extern from *:
    int CYTHON_TRACE

cdef void foo():
    if CYTHON_TRACE:
          ...
```
But then CYTHON_TRACE is sometimes not defined, which then lead to compile errors from the C/C++ compiler down the line. I could try to do
```
#ifndef CYTHON_TRACE
#define CYTHON_TRACE 0
#endif
```
in a cdef extern block, but that leads back to the original problem. CYTHON_TRACE would never be defined because this happens above the handling for it...

da-woods

unread,
Jun 11, 2025, 3:17:17 PMJun 11
to cython...@googlegroups.com

I think you can use the "#cython: preliminary_late_includes = True" directive which pushes the extern blocks further down in the source file.

I think the plan is to eventually replace that macro with something better so do be prepared for it to break at some point. But that should probably work for now.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cython-users/a7b74934-432a-440d-8da9-c2db5809d261n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages