저는 Cython을 처음 접했고 Cython을 사용하여 C/C++ 정적 라이브러리를 래핑했습니다. 나는 다음과 같이 간단한 예를 만들었다. Test.hCython으로 랩 C++ lib
:
#ifndef TEST_H
#define TEST_H
int add(int a, int b);
int multipy(int a, int b);
#endif
Test.cpp에
#include "test.h"
int add(int a, int b)
{
return a+b;
}
int multipy(int a, int b)
{
return a*b;
}
은 그럼 컴파일하고 구축 g ++를 사용했다.
g++ -c test.cpp -o libtest.o
ar rcs libtest.a libtest.o
이제 libtest.a라는 정적 라이브러리가 생겼습니다.
Test.pyx :
cdef extern from "test.h":
int add(int a,int b)
int multipy(int a,int b)
print add(2,3)
Setup.py :
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension("test",
["test.pyx"],
language='c++',
include_dirs=[r'.'],
library_dirs=[r'.'],
libraries=['libtest']
)]
setup(
name = 'test',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
의 I라는 :
python setup.py build_ext --compiler=mingw32 --inplace
출력했다 :
running build_ext
cythoning test.pyx to test.cpp
building 'test' extension
creating build
creating build\temp.win32-2.6
creating build\temp.win32-2.6\Release
C:\Program Files\pythonxy\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -I. -IC:\
Python26\include -IC:\Python26\PC -c test.cpp -o build\temp.win32-2.6\Release\test.o
writing build\temp.win32-2.6\Release\test.def
C:\Program Files\pythonxy\mingw\bin\g++.exe -mno-cygwin -mdll -static --entry _D
[email protected] --output-lib build\temp.win32-2.6\Release\libtest.a --def build\temp.w
in32-2.6\Release\test.def -s build\temp.win32-2.6\Release\test.o -L. -LC:\Python
26\libs -LC:\Python26\PCbuild -ltest -lpython26 -lmsvcr90 -o test.pyd
g++: build\temp.win32-2.6\Release\libtest.a: No such file or directory
error: command 'g++' failed with exit status 1
또한 라이브러리 = [ 'libtest'] 대신 라이브러리 = [ 'test']를 사용하려고했습니다. 그것은 나에게 같은 오류를 주었다.
그에 대한 단서가 있습니까?
감사합니다.
어디에서 extern "C"를 선언해야합니까?그러나, 문제는 지금 빌드가 함수 대신 add() 또는 mulitpy() 중 하나 대신 libtest.a를 찾지 못한다고 불평한다는 것입니다. 그래서 이것이 효과가 있을지 확신하지 못합니다. 'build \ temp.win32-2.6 \ Release'에는 libtest.a가 없다는 것이 나에게 이상하다. Cython 자체에서 생성 한 빌드 폴더가 아닌가요? 왜 Cython은 libtest.a를 찾으려고 했습니까? –
'extern "C"'는'.h'의 함수 선언에 들어가며, 잘못된'library_dirs'을 고친 후에 직면하게 될 다음 문제가 있습니다 (라이브러리는 현재 디렉토리에 있습니다. "그리고'Release'는 컴파일러/링커가 라이브러리를 찾을 때 현재 디렉토리가됩니다). –