cython - 如何让 cython 使用 c++20 编译器?

标签 cython

我尝试在 C++20 中使用 std 的一些功能,但是当我尝试使用 Cython 编译时出现错误。

%%cython -a -+

from libcpp.bit cimport popcount
print(popcount(5))

Content of stdout: _cython_magic_8a7cc06c3f4134f8362279784510a3a14b752950.cpp
The contents of <bit> are available only with C++20 or later.

当我在 Visual Studio 中运行类似的 C++ 程序时,它工作得很好。

#include <iostream>
#include <bit>

int main()
{
    std::cout << std::popcount((unsigned int) 7);
}

3

如何查看我正在使用的编译器,或者如何将其设置为使用 c++20 的编译器?

最佳答案

您需要向编译器传递一个参数以启用 c++20,特别是用于 gcc 的 -std=c++20/std:c++20 对于 msvc,使用 cython magic--compile-args-c 参数.

%%cython -c=-std=c++20 --cplus

from libcpp.bit cimport popcount
print(popcount(<unsigned long>(5)))

这适用于 colab,它使用带有 gcc 的 Linux,对于带有 msvc 的 Windows,则使用 -c=/std:c++20

关于cython - 如何让 cython 使用 c++20 编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77636105/

相关文章:

python - Pandas/Python - 使用 stack() groupby() 和 apply() 的性能非常慢

python - 高效且经济地运行 python 程序的多个实例?

python - numpy 数组乘法比带向量乘法的循环慢?

python - Cython 初学者 - 加速 numpy 广播

python - Numpy->Cython 转换 : Compile error:Cannot convert 'npy_intp *' to Python object

Cython:LINK: fatal error LNK1181:无法打开输入文件

python - 完全 Cython 化的应用程序

python - 我可以在 PyPy 中嵌入 CPython 吗?

python - 分析 GIL

python - Cython 子类化 "First base of"... "is not an extension type"即使它是用 cdef 定义的