c - 如何在 Windows 中编译 Python 3.7 的 C 扩展

标签 c windows visual-studio python-3.7

我正在尝试编译一个 C 文件以在 Python 中使用。该文件是微分代数方程 (DAE) 的求解器。我的问题是,当我编译 setup.py 文件时,我收到错误无法打开源文件:'daesolver.c':没有这样的文件或目录。由于 C 文件非常复杂,我尝试用一​​个更简单的示例来做同样的事情。我使用了 Elliot Forbes 在 ( https://tutorialedge.net/python/python-c-extensions-tutorial/ ) 中提供的 HelloWorld 示例。即使使用这个非常简单的函数,我也会收到相同的错误。 这是我从构建命令收到的完整输出。

C:\Users\Administrator>python c:\temp\teste_C2py\setup.py build
running build
running build_ext
building 'myModule' extension
C:\Program Files (x86)\Microsoft Visual 
Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c 
/nolog
o /Ox /W3 /GL /DNDEBUG /MD -IC:\ProgramData\Anaconda3\include - 
IC:\ProgramData\Anaconda3\include "-IC:\Program Files (x8
6)\Microsoft Visual 
Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "- 
IC:\Program Files (x86)\Microsoft
Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "- 
IC:\Program Files (x86)\Windows Kits\10\include\10.0.1
7763.0\ucrt" "-IC:\Program Files (x86)\Windows 
Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Ki
ts\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows 
Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files
(x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /Tctest.c 
/Fobuild\temp.win-amd64-3.7\Release\test.obj
test.c
c1: fatal error C1083: Cannot open source file: 'test.c': No such file or 
directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual 
Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\Ho
stX86\\x64\\cl.exe' failed with exit status 2

这是 test.c 文件

#include <Python.h>

// Function 1: A simple 'hello world' function
static PyObject* helloworld(PyObject* self, PyObject* args)
{
    printf("Hello World\n");
    return Py_None;
}

// Our Module's Function Definition struct
// We require this `NULL` to signal the end of our method
// definition
static PyMethodDef myMethods[] = {
    { "helloworld", helloworld, METH_NOARGS, "Prints Hello World" },
    { NULL, NULL, 0, NULL }
};

// Our Module Definition struct
static struct PyModuleDef myModule = {
    PyModuleDef_HEAD_INIT,
    "myModule",
    "Test Module",
    -1,
    myMethods
};

// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_myModule(void)
{
    return PyModule_Create(&myModule);
}

这是 setup.py

from distutils.core import setup, Extension
setup(name = 'myModule', version = '1.0',  \
   ext_modules = [Extension('myModule', ['test.c'])])

这两个文件位于同一文件夹 c:\temp\teste_C2py 中。

我在 Anaconda 发行版 64 位中使用 Python 3.7(我也想对 x86 执行相同操作)。

我已经在同一环境中安装了 Visual Studio 2017(社区)以及所有必要的编译器(至少我是这么认为)。

真正奇怪的是,当我在虚拟机中安装 vs build tools 2017 时,我被迫这样做,但是当我尝试在另一台计算机中复制相同的内容时,它给了我这个错误。 我还搞乱了我第一次做这项工作的环境,现在我无法再这样做了。

我还设置了环境变量 VS150COMNTOOLS = C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools

我似乎在 Visual Studio 配置中遗漏了一些内容,因为找不到同一文件夹中的文件是没有意义的。

最佳答案

我发现了问题。过去,这是一件非常愚蠢的事情。我试图从 python.exe 所在的文件夹运行安装脚本(因为我的机器中有不同版本的 Python)。但是,如果我移动到 setup.py 文件所在的文件夹并在 setup.py 之前没有任何路径的情况下运行 coomand,则构建可以正常工作!我不知道为什么相对路径在这里不起作用。 我希望这能有所帮助!

关于c - 如何在 Windows 中编译 Python 3.7 的 C 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55545200/

相关文章:

c# - Web 部署代理服务返回 500 内部服务器错误

c - 信号异步是什么意思?

远程对象的 C++ 调用方法(类似于 RPC)

c++ - 检测移除打开的串口设备(Qt/Windows)

windows - DirectShow - 如何从源过滤器读取文件

c# - 按文件名的内容对文件进行排序

c - 从 YUV420P 到 RGB 的 FFMPEG Api 转换产生奇怪的输出

c - 我不懂河内算法

c++ - 使用 SSE 内在函数时如何确保 NaN 传播?

c++ - VS 自动并行化