制作Python C扩展时的库链接错误

时间:2016-07-05 20:04:29

标签: python c python-c-extension

在过去的几天里,我试图为Python创建一个C扩展,但是遇到了错误LNK2001:通常是微不足道的,但我还没有找出原因。

首先,这里是相关的C代码:

#include "Python.h"
#include <time.h>
#include "visa.h" /* external VISA library */
#include "visatype.h" 

...

/*Static variables*/
static ViSession defaultRM;
static ViSession instr;
static ViStatus status;
static ViUInt32 retCount;
static ViUInt32 writeCount;

...

/*Method table for the Python interpreter.
static PyMethodDef VISAIO_Methods[] = {
    {"openMainframe", VISAIO_openMainframe, METH_NOARGS, "Open a VISA session with the mainframe."},
    {"sendMsg", VISAIO_sendMsg, METH_VARARGS, "Send a message to the specified port. 0 = mainframe. (int port, char* message)"},
    {"readMsg", VISAIO_readMsg, METH_VARARGS, "Return a message of specified length from the buffer of the specified port. 0 = mainframe. (int port, char* message, int maxLength)"},
    {"closeMainFrame", VISAIO_closeMainframe, METH_NOARGS, "Close the VISA session."}
};

/*Structure passed to Python module initializer. Arguments are unexplained in documentation, but two are obvious.*/
static struct PyModuleDef VISAIOmodule = {
    PyModuleDef_HEAD_INIT,
    "VISAIO",
    NULL,
    -1,
    VISAIO_Methods
};

/*Initializes the module for use by the Python program.*/
PyMODINIT_FUNC PyInit_VISAIO(void)
{
    return PyModule_Create(&VISAIOmodule);
}

和Python:

from distutils.core import setup, Extension

module1 = Extension('VISAIO',
                depends = ['visa32.lib'],
                include_dirs = ['Include'],
                libraries = ['visa32'],
                sources = ['VISAIOmodule.c'],)

setup (name = 'VISAIO',
       version = '1.0',
       description = 'This handles low-level IO with the VISA library.',
       ext_modules = [module1])

我使用命令python setup.py build运行Python文件 并获得以下内容:

running build
running build_ext
building 'VISAIO' extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD
-IInclude "-IC:\Program Files (x86)\Python35-32\include" 
"-IC:\Program Files (x86)\Python35-32\include" 
"-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" 
"-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" 
"-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" 
"-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" 
"-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" 
"-IC:\Program Files (x86)\Windows Kits\8.1\include\\um"
"-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" /TcVISAIOmodule.c /Fobuild\temp.win32-3.5\Release\VISAIOmodule.obj
VISAIOmodule.c
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO 
"/LIBPATH:C:\Program Files (x86)\Python35-32\libs" 
"/LIBPATH:C:\Program Files (x86)\Python35-32\PCbuild\win32" 
"/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB"
"/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x86"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\lib\um\x86"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x86" visa32.lib 
/EXPORT:PyInit_VISAIO build\temp.win32-3.5\Release\VISAIOmodule.obj 
/OUT:build\lib.win32-3.5\VISAIO.cp35-win32.pyd /IMPLIB:build\temp.win32-3.5\Release\VISAIO.cp35-win32.lib
   Creating library build\temp.win32-3.5\Release\VISAIO.cp35-win32.lib and object build\temp.win32-3.5\Release\VISAIO.cp35-win32.exp
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viSetAttribute@12
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viOpenDefaultRM@4
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viWrite@16
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viClose@4
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viOpen@20
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viRead@16
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viClear@4
VISAIOmodule.obj : error LNK2001: unresolved external symbol _viGetAttribute@12
build\lib.win32-3.5\VISAIO.cp35-win32.pyd : fatal error LNK1120: 8 unresolved externals

其结果是编译的VISAIOmodule.obj文件,它向我指示C编译器正确地将VISA库链接到源代码。但是,当我跟着this alternative method写信时,gcc告诉我VISAIOmodule.obj: file not recognized: File format not recognized.

当程序继续创建Python库时,会抛出链接错误。我已经绝对确定所讨论的库位于所有标准搜索目录以及我指定的目录中。发生了什么事?

0 个答案:

没有答案