我尝试在 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/