编译python3.2 C模块时,链接器请求'python26.lib

2 投票
1 回答
516 浏览
提问于 2025-04-17 08:38

我正在尝试编译一个随3.2源代码一起提供的示例Visual Studio解决方案。我按照说明把目录上移了一层。不过,我用的Visual Studio版本和他们的不同。

但不知道为什么,我遇到了这个问题:

1>LINK : fatal error LNK1181: cannot open input file 'python26.lib'

我对链接器(或者说Visual C++)的工作原理不是很了解。不过,我检查过包含和库的目录,没发现有什么看起来不对的地方,适用于VS和这个项目。

有没有人能帮我理解并解决这个问题呢?

这是示例模块的源代码:

#include "Python.h"

static PyObject *
ex_foo(PyObject *self, PyObject *args)
{
    printf("Hello, world\n");
    Py_INCREF(Py_None);
    return Py_None;
}

static PyMethodDef example_methods[] = {
    {"foo", ex_foo, METH_VARARGS, "foo() doc string"},
    {NULL, NULL}
};

static struct PyModuleDef examplemodule = {
    PyModuleDef_HEAD_INIT,
    "example",
    "example module doc string",
    -1,
    example_methods,
    NULL,
    NULL,
    NULL,
    NULL
};

PyMODINIT_FUNC
PyInit_example(void)
{
    return PyModule_Create(&examplemodule);
}

1 个回答

1

我不建议使用源代码里提供的示例代码,你的项目设置里必须包含 Python26.lib。其实用 distutils 会简单得多,下面是一个简单模块的 setup.py 文件:

from distutils.core import setup, Extension

module1 = Extension('Simple',
                    sources = ['Simple.c'])

setup (name = 'Simple',
       version = '1.0',
       description = 'This is a Simple package',
       ext_modules = [module1])

你需要在一个已经运行过 vcvarsall.bat 的 cmd.exe 窗口中运行它,并且确保 Python 在你的路径中。

不过,如果你一定要使用 Visual Studio 的话:

  1. 把 Python 库添加到项目中:python32.lib
  2. 把 Python 的目录添加到项目中:C:\Python32\include 和 C:\Python32\libs
  3. 记得把链接器的输出设置为 .pyd 文件。
  4. 输出目录应该是 C:\Python32\Lib\site-packages,或者你可以自己把生成的 .pyd 文件复制到 site_packages 里。

撰写回答