0

I'm trying to use cython to speed up my code. Since I'm working with an array of strings, I want to use string and vector from c++. But I have problems compiling if I import c libraries. For an example, I tried to implement an example from here: https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html. So, my code is

from libcpp.vector cimport vector

def primes(unsigned int nb_primes):
    cdef int n, i
    cdef vector[int] p
    p.reserve(nb_primes)  # allocate memory for 'nb_primes' elements.

    n = 2
    while p.size() < nb_primes:  # size() for vectors is similar to len()
        for i in p:
            if n % i == 0:
                break
        else:
            p.push_back(n)  # push_back is similar to append()
        n += 1

    # Vectors are automatically converted to Python
    # lists when converted to Python objects.
    return p

I save thiscode like 'test_char.pyx'. For compilation i use it:

from Cython.Build import cythonize
setup(name='test_char',
      ext_modules = cythonize('test_char.pyx')
      )

After that i get test_char.c, but i don't get test_char.py. If i will use this code (without cimport):

def primes(int nb_primes):
    cdef int n, i, len_p
    cdef int p[1000]
    if nb_primes > 1000:
        nb_primes = 1000
    len_p = 0  # The current number of elements in p.
    n = 2
    while len_p < nb_primes:
        # Is n prime?
        for i in p[:len_p]:
            if n % i == 0:
                break

        # If no break occurred in the loop, we have a prime.
        else:
            p[len_p] = n
            len_p += 1
        n += 1

    # Let's return the result in a python list:
    result_as_list  = [prime for prime in p[:len_p]]
    return result_as_list

all be right. So, plz, any ideas?

Ivan
  • 11
  • 2
  • 1
    What is the error? – Linux Geek Apr 19 '21 at 19:23
  • @LinuxGeek, the consol close very fast, but i didn't see any formal errors. But i expect the new file 'test_char.py' after run setup.py, but I don't get it. If i delete call lib (first string), then i get 'test_char.py'. So, It is the problem. – Ivan Apr 19 '21 at 20:05
  • 1
    You will not get `test_char.py`. You will get either `test_char.so` on Linux, or `test_char.pyd` on Windows. Do you see those? – Tim Roberts Apr 19 '21 at 20:24
  • 1
    I think your question is a duplicate of https://stackoverflow.com/questions/53471655/compiling-cython-with-gcc-no-such-file-or-directory-from-include-ios/ (but it isn't really clear enough to be sure) – DavidW Apr 19 '21 at 21:54
  • @TimRoberts, i see test_char.py.c. When i compile code without libs, i see two files: test_char.py.c and test_char.py – Ivan Apr 21 '21 at 11:51
  • @DavidW, Thanks, when i add "language='c++'" in setup.exe, i get need files. This source helped me! – Ivan Apr 21 '21 at 21:47

1 Answers1

1
from distutils.extension import Extension
extensions = [
    Extension("test_char", ["test_char.pyx"]
              , language="c++"
              )
]
setup(
    name="test_char",
    ext_modules = cythonize(extensions),
)

it can solve this problem

Ivan
  • 11
  • 2