gpt4 book ai didi

c++ - 构建 Python C++ 扩展在重载函数上失败

转载 作者:行者123 更新时间:2023-11-28 06:35:47 27 4
gpt4 key购买 nike

我想用 C++ 编写一个 Python 模块并尝试使用 distutils 编译它,但是当我尝试使用重载函数时它突然给我一个编译错误。我该如何应对?

这是一个简单的模块,根据official manual.编写的

该模块由 3 个文件组成:pymega.cpp(Python 解释器的模块接口(interface))、payload.hpayload.cpp(这里应该是“有效载荷”)。

pymega.cpp

//pymega.cpp
#include <Python.h>

#include <iostream>
using namespace std;

#include "payload.h"

static PyObject* test(PyObject* self, PyObject* args)
{
cout << "Running test method!" << endl;
cout << "^__^" << endl;

PyMega::uber_function(7, 40);
return Py_None;
}

//Module methods declatarion
static PyMethodDef Methods[] = {
{"test", test, METH_VARARGS, "Hell yeah!!"},
{NULL, NULL, 0, NULL} /* Sentinel */
};

static struct PyModuleDef pymega = {
PyModuleDef_HEAD_INIT,
"pymega", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
Methods
};

// Module nitialization
PyMODINIT_FUNC PyInit_pymega(void)
{
cout << "Initializing PyMega..." << endl;
PyObject *m;

m = PyModule_Create(&pymega);
if (m == NULL)
{
cout << "PyMega init failed" << endl;
return NULL;
}

return m;
}

有效载荷.h

//payload.h
#ifndef PAYLOAD_H_
#define PAYLOAD_H_

#include <Python.h>

#include <iostream>
using namespace std;

namespace PyMega {

void uber_function(unsigned int arg1);
void uber_function(unsigned int arg1, unsigned int arg2);

} // PyMega


#endif // PAYLOAD_H_

payload.cpp

//payload.cpp
#include "payload.h"

#include <iostream>
using namespace std;

using namespace PyMega;

void uber_function(unsigned int arg1)
{
cout << "uber_function " << arg1 << endl;
}

void uber_function(unsigned int arg1, unsigned int arg2)
{
cout << "uber_function " << arg1 << " " << arg2 << endl;
uber_function(arg1);
}

此外,这是 setup.py(我的安装脚本)。

from distutils.core import setup, Extension

module1 = Extension('pymega',
sources=['pymega.cpp', "payload.cpp"],
language="c++")

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

当我尝试运行 setup.py build 时,它给我以下错误日志:

running build
running build_ext
building 'pymega' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
-g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security
-D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c pymega.cpp -o build/temp.
linux-x86_64-3.4/pymega.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC
but not for C++ [enabled by default]
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
-g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security
-D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c payload.cpp -o build/temp
.linux-x86_64-3.4/payload.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC
but not for C++ [enabled by default]
payload.cpp: In function ‘void uber_function(unsigned int, unsigned int)’:
payload.cpp:16:21: error: call of overloaded ‘uber_function(unsigned int&)’ is a
mbiguous
uber_function(arg1);
^
payload.cpp:16:21: note: candidates are:
payload.cpp:8:6: note: void uber_function(unsigned int)
void uber_function(unsigned int arg1)
^
In file included from payload.cpp:1:0:
payload.h:11:8: note: void PyMega::uber_function(unsigned int)
void uber_function(unsigned int arg1);
^
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

最佳答案

您已经在 ::PyMega 命名空间中声明了 void uber_function(int),然后继续定义 void uber_function(int):: 命名空间中。这是来自不同 namespace 的两个不同函数,具有相同的名称和签名,这会导致冲突。可能不是您想要的。

要定义 ::PyMega::uber_function必须

namespace PyMega
{
void uber_function(int) {
....
}
}

关于c++ - 构建 Python C++ 扩展在重载函数上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com