Search code examples
cython

How can I get cython to use a c++20 compiler?


I am trying to use some features of std in C++20, but when I try to compile with Cython I get an error.

%%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.

When I run a similar C++ program in visual studio it works just fine.

#include <iostream>
#include <bit>

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

3

How can I see what compiler I am using, or how can I set it to one which uses c++20?


Solution

  • you need to pass an argument to the compiler to enable c++20, specifically -std=c++20 for gcc or /std:c++20 for msvc using the --compile-args or -c argument of cython magic.

    %%cython -c=-std=c++20 --cplus
    
    from libcpp.bit cimport popcount
    print(popcount(<unsigned long>(5)))
    

    this works on colab, which uses linux with gcc, for windows with msvc use -c=/std:c++20 instead.