如何编译cython代码
问题描述:
我想编译我的代码。我想写我自己的cpp文件,从其他库调用函数。如何编译cython代码
我cpp的代码
//my_vl.h
int my_vl_k(int normalized_feat_set, int k){}
我PXD文件
#my_vl.pxd
import libc.stdlib
cdef extern from "my_vl.h":
int my_vl_k(int normalized_feat_set, int k)
我知道我不需要.h文件和文件.pxd现在,但它是很好的做法,如果你可以帮助我如何包括这真的会有所帮助。
我的cpp文件:
//my_vl.cpp
extern "C" {
#include <vl/random.h>
#include <vl/generic.h>
#include <vl/kmeans.h>
#include <vl/mathop.h>
}
#include <cstdlib>
#include <stdio.h>
int my_vl_k(int normalized_feat_set, int K){
int r = 5;
return r;
}
我PYX文件:
#my_vl.pyx
cimport my_vl
cdef extern from "my_vl.cpp":
int my_vl_k(int, int)
我的安装文件:
#setup.my_val.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
sourcefiles = ['my_vl.pyx', 'my_vl.cpp']
ext_modules = [Extension("my_vl",
sourcefiles,
include_dirs = ['/path/to/vlfeat-0.9.20'],
libraries = ['vl'],
library_dirs = ['/path/to/vlfeat-0.9.20/bin/glnxa64/']
)]
setup(
name = 'my_val app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
错误我得到的是:
python setup.my_val.py build_ext --inplace
running build_ext
skipping 'my_vl.c' Cython extension (up-to-date)
building 'my_vl' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/path/to/vlfeat-0.9.20 -I/usr/include/python2.7 -c my_vl.cpp -o build/temp.linux-x86_64-2.7/my_vl.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/path/to/vlfeat-0.9.20 -I/usr/include/python2.7 -c my_vl.cpp -o build/temp.linux-x86_64-2.7/my_vl.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
c++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/my_vl.o build/temp.linux-x86_64-2.7/my_vl.o -L/path/to/vlfeat-0.9.20/bin/glnxa64/ -lvl -o /mnt/disk2/work/visual_clusters/my_vl.so
build/temp.linux-x86_64-2.7/my_vl.o: In function `my_vl_k(int, int)':
/path/to/my_vl.cpp:45: multiple definition of `my_vl_k(int, int)'
build/temp.linux-x86_64-2.7/my_vl.o:/path/to/my_vl.cpp:45: first defined here
collect2: error: ld returned 1 exit status
error: command 'c++' failed with exit status 1
不知道我必须做的
答
在my_v1.h你应该使用头卫士:
my_v1.h:
#ifndef MY_V1_H
#define MY_V1_H
// your function decl.
#endif
答
你在你的头文件中提供的my_vl_k
定义({}
)。相反,写
int my_vl_k(int normalized_feat_set, int k);
(更换大括号从而提供了一个分号这只是说,该函数存在一个无用的空的定义)。
我在我的.h文件改变到 的#ifndef MY_Vl_H 的#define MY_Vl_H INT my_vl_k(INT normalized_feat_set,INT K){}; #endif 我仍然得到同样的错误。我不认为我的安装文件知道我的h文件,因为我没有包含它或将它传递给它。 – user1871528