构建Python C++;扩展在重载函数上失败 我想在C++中编写一个Python模块,并尝试用 ditudiss/COD>来对它进行BIOTHOLD,但是当我尝试使用重载函数时,它突然给了我一个编译错误。我怎样才能应付他们
下面是一个简单的模块,根据 该模块由3个文件组成:构建Python C++;扩展在重载函数上失败 我想在C++中编写一个Python模块,并尝试用 ditudiss/COD>来对它进行BIOTHOLD,但是当我尝试使用重载函数时,它突然给了我一个编译错误。我怎样才能应付他们,c++,C++,下面是一个简单的模块,根据 该模块由3个文件组成:pymega.cpp(Python解释器的模块接口)、payload.h和payload.cpp(这里应该是“payload”) pymega.cpp //pymega.cpp #include <Python.h> #include <iostream> using namespace std; #include "payload.h" static PyObject* test(PyObject* self, P
pymega.cpp
(Python解释器的模块接口)、payload.h
和payload.cpp
(这里应该是“payload”)
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;
}
//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 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_函数(int)
,然后在:
命名空间中定义了void uber_函数(int)
。这是来自不同名称空间的两个不同函数,具有相同的名称和签名,这会导致冲突。可能不是你想要的
要定义::PyMega::uber_函数
您必须说
namespace PyMega
{
void uber_function(int) {
....
}
}
要重现这个问题,Python中的任何东西都是必要的吗?它需要多个文件吗?请创建一个最小的示例。也就是说,
1
的类型是int
,而函数需要无符号int
。您可能需要1u
,但我懒得在最小化代码之前分析所有代码。是的,它确实需要3个文件来重现错误。当我试图将它合并到一个文件中时,错误消失了。但是,当然,还有更多的函数,函数也更长。#include
只会将文件内容粘贴到位,所以我不敢相信您做出了适当的努力来重现一个示例。
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
namespace PyMega
{
void uber_function(int) {
....
}
}