将 PCL 链接到 Cython C++ 模块

Linking PCL to Cython C++ module

本文关键字:C++ 模块 Cython PCL 链接      更新时间:2023-10-16

我正在为用python编写的模拟器开发实时激光雷达数据处理器。 由于数据量很大,我真的需要c/c ++性能。 所以我找到了Cython,它看起来令人难以置信,除了它在编译时不能包含pcl库的事实。

所以我想构建我的 .so 文件,自己链接 pcl,然后在 Python 包装器中调用库,但仍然没有得到任何结果。这是我的 setup.py 和我的.pyx

Setup.py:

#!/usr/bin/env python
import sys
import os
import shutil
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

import numpy
setup(cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("multiply",
sources=["cythonBridge.pyx", "Monitor.cpp"],
libraries=["myLib"],
language="c++",
include_dirs=[numpy.get_include()])],
)

cythonBridge.pyx:

#distutils: language = c++

"""
multiply.pyx
simple cython test of accessing a numpy array's data
the C function: c_multiply multiplies all the values in a 2-d array by a scalar, in place.
"""
import cython
# import both numpy and the Cython declarations for numpy
import numpy as np
cimport numpy as np

# declare the interface to the C code
cdef extern void c_multiply (double* array, double value, int m, int n)
@cython.boundscheck(False)
@cython.wraparound(False)
def multiply(np.ndarray[double, ndim=2, mode="c"] input not None, double value):
"""
multiply (arr, value)
Takes a numpy array as input, and multiplies each element by value, in place
param: array -- a 2-d numpy array of np.float64
param: value -- a number that will be multiplied by each element in the array
"""
cdef int m, n
m, n = input.shape[0], input.shape[1]
c_multiply (&input[0,0], value, m, n)
return None

错误日志(调用python setup.py 安装时(:

gcc -pthread -B /home/francesco/anaconda3/envs/carla/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/francesco/anaconda3/envs/carla/lib/python3.6/site-packages/numpy/core/include -I/home/francesco/anaconda3/envs/carla/include/python3.6m -c Monitor.cpp -o build/temp.linux-x86_64-3.6/Monitor.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
In file included from Monitor.cpp:13:0:
myLib.h:4:10: fatal error: pcl/io/pcd_io.h: No such file or directory
#include <pcl/io/pcd_io.h>
^~~~~~~~~~~~~~~~~
compilation terminated.

您是否正在使用文件distutils.cfg?您可以将目录pcl/io/添加到 [build_ext] 部分下的文件中

[build_ext]
include_dirs= path/to/pcl/io/

或修改 setup.py 中的build_ext,例如

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension(".CythonTutorialsrchelloworld", [".CythonTutorialsrchelloworld.pyx"])]
)

或者将该标头所在的文件夹添加到列表Extensions / include_dirs

https://stackoverflow.com/a/29627625/7919597

https://github.com/cython/cython/issues/2771