链接错误来编译调用extern库

时间:2017-08-05 16:16:14

标签: c visual-c++ cython

我编译了一个名为edlib的C模块用于编辑距离,然后我把它包含在我的项目中,

setup.py
structureS.pyx
cedlib.pxd
cedlib
|_____include
      |______edlib.h
|_____lib
      |______edlib.lib

但是在运行python setup build_ext --inplace之后我有链接错误,这是代码,

#file:setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules = [
    Extension(
        "structureS",
        ["structureS.pyx"],
        extra_compile_args=['/openmp'],
        extra_link_args=['/openmp'],
        include_dirs = ['edlib/include'],
        #libraries = ['edlib'],
        library_dirs = ['edlib/lib'],
        # sources = ['edlib/src/edlibe.cpp']

    )
]
setup(
    name='structureS',
    ext_modules=cythonize(ext_modules),
)

pxd文件由此链接cedlib.pxd

中模块的作者提供
#structureS.pyx
from cedlib cimport EdlibAlignResult, edlibAlign, edlibDefaultAlignConfig,edlibFreeAlignResult

cdef char* query = "ACCTCTG"
cdef char* target = "ACTCTGAAA"
cdef EdlibAlignResult result = edlibAlign(query, 7, target, 9, edlibDefaultAlignConfig())
if (result.status == 0): #EDLIB_STATUS_OK
    printf("%d", result.editDistance)

edlibFreeAlignResult(result)

这是错误消息:

  

structureS.obj:错误LNK2001:symbole externenonrésoluedlibFreeAlignResult

     

structureS.obj:错误LNK2001:symbole externenonrésoluedlibAlign

     

structureS.obj:错误LNK2001:symbole externenonrésolu   edlibDefaultAlignConfig

注意:我的项目是针对64位平台编译的,并且该模块是针对32位平台配置的,我试图用Visual Studio 2017编译64位模块没有成功,

1 个答案:

答案 0 :(得分:2)

我通过使用cython源直接编译C模块源来解决它,这是解决方案

#file:setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules = [
    Extension("structureS",
         ["structureS.pyx", "edlib/src/edlib.cpp"],
         include_dirs=["edlib/include"],
         depends=["edlib/include/edlib.h"],
        extra_compile_args=['/openmp'],
        extra_link_args=['/openmp'],
    )
]
setup(
    name='structureS',
    ext_modules=cythonize(ext_modules),
)