2016-09-23 209 views
-2

我想編譯我的代碼。我想寫我自己的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 

不知道我必須做的

回答

0

在my_v1.h你應該使用頭衛士:

my_v1.h:

#ifndef MY_V1_H 
#define MY_V1_H 

// your function decl. 

#endif 
+0

我在我的.h文件改變到 的#ifndef MY_Vl_H 的#define MY_Vl_H INT my_vl_k(INT normalized_feat_set,INT K){}; #endif 我仍然得到同樣的錯誤。我不認爲我的安裝文件知道我的h文件,因爲我沒有包含它或將它傳遞給它。 – user1871528

0

你在你的頭文件中提供的my_vl_k定義({} )。相反,寫

int my_vl_k(int normalized_feat_set, int k); 

(更換大括號從而提供了一個分號這只是說,該函數存在一個無用的空的定義)。