我正在将一个Python模块移植到Windows上。我有一个玩具示例如下。
文件夹结构如下:
foo/
libfoo/
foo.c
setup.py
setup.py
from setuptools import setup, Extension
sources = ['libfoo/foo.c']
foo = Extension('libfoo',
sources = sources,
define_macros = None,
include_dirs = ['./libfoo'],
libraries = None,
library_dirs = None,
)
setup(name = 'foo',
ext_modules = [foo],
install_requires = ['setuptools'],
)
libfoo/foo.c(为了完整性)
#include <stdio.h>
void foo() {
printf("Hello World!");
}
当我尝试安装软件包时,遇到了一个错误。
C:\Users\user\foo>python setup.py install
running install
running bdist_egg
running egg_info
creating foo.egg-info
writing requirements to foo.egg-info\requires.txt
writing foo.egg-info\PKG-INFO
writing top-level names to foo.egg-info\top_level.txt
writing dependency_links to foo.egg-info\dependency_links.txt
writing manifest file 'foo.egg-info\SOURCES.txt'
reading manifest file 'foo.egg-info\SOURCES.txt'
writing manifest file 'foo.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_ext
building 'libfoo' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
creating build\temp.win32-2.7\Release\libfoo
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox
/MD /W3 /GS- /DNDEBUG -I./libfoo -IC:\Python27\include -IC:\Python27\PC /Tclibfo
o/foo.c /Fobuild\temp.win32-2.7\Release\libfoo/foo.obj
foo.c
creating build\lib.win32-2.7
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\link.exe /DLL /nologo
/INCREMENTAL:NO /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:i
nitlibfoo build\temp.win32-2.7\Release\libfoo/foo.obj /OUT:build\lib.win32-2.7\l
ibfoo.pyd /IMPLIB:build\temp.win32-2.7\Release\libfoo\libfoo.lib /MANIFESTFILE:b
uild\temp.win32-2.7\Release\libfoo\libfoo.pyd.manifest
LINK : error LNK2001: unresolved external symbol initlibfoo
build\temp.win32-2.7\Release\libfoo\libfoo.lib : fatal error LNK1120: 1 unresolv
ed externals
error: command 'c:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\BIN\\l
ink.exe' failed with exit status 1120
似乎distutils包(在本例中为setuputils)将始终从共享扩展中导出一个符号,即“init + extension_name” [链接]。
这在Windows链接器的“EXPORT”选项[链接]中指定,但它找不到该符号。
需要帮助吗?
编辑:C代码不使用Python C API,即“#include ”。这是因为项目的目标是通过Python扩展将现有的C库封装在Python包装器中。该软件包适用于Unix / Linux。
initlibfoo
函数,以便Python在导入扩展时调用?还是只是使用setuptools编译共享库?如果是后者,对于Windows DLL,你需要做的第一件事就是定义导出符号,可以使用__declspec(dllexport)
或.DEF文件。 - Eryk Sun