Cython 基于外部值的条件编译
我尝试从 Cython pxd 有条件地编译(或生成)为 c 代码。我读到我可以 DEF 定义 aa 值,并 IF 根据其值有条件地生成,但是如何从 pxd 文件外部获取该值?
具体来说,这两种情况现在对我来说很有趣:
- 为 Cython 提供一些命令行定义,最好通过 Cython.Distutils setuptools 的方式
- extern-ed C 头文件定义一些值,并根据该值有条件地使用 Cython 进行定义(也许不可能现在?)
谢谢
I try to conditionally compile (or generate) to c code from a Cython pxd. I read that I can DEF to define aa value and IF to conditionally generate based on its value, but how can I get this value to get from outside of the pxd file?
Specifically these two cases are interesting for me now:
- give some command-line define to Cython, preferrably through the Cython.Distutils setuptools way
- the extern-ed C header file defines some value, and conditionally define using Cython based on this value (maybe impossible now?)
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以生成一个 pxi 文件,并在执行 IF 之前包含它(与 ./configure 也生成 config.h 相同。)
例如,这就是我们在 Kivy setup.py 中所做的:
然后,在您的 pxd 中:
You could generate a pxi file, and include it before doing your IF (same as ./configure generate a config.h too.)
This is what we do in Kivy setup.py for example :
And then, in your pxd :
实际上,第二种选择更容易。在某个 .h 文件中创建一个 FLAG,然后
当您想使用它时,只需编写即可
,即使 Cython 会生成代码,C 编译器也会自动修剪它,因为它在编译时知道 FLAG 的值。
Actually, the second option is easier. Create a FLAG in some .h file and then do
then when you want to use it, just write
and even though Cython will generate the code, the C compiler will automatically trim this away as it knows the value of FLAG at compile time.