我想用C ++编写一个Python模块并尝试用distutils
生成它,但是当我尝试使用重载函数时它突然给了我一个编译错误。我该如何应对?
这是一个简单的模块,根据official manual.
编写该模块由3个文件组成:pymega.cpp
(Python解释器的模块接口),payload.h
和payload.cpp
(这里应该是"有效载荷")。 / p>
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.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
答案 0 :(得分:1)
您已在void uber_function(int)
命名空间中声明::PyMega
,然后继续在void uber_function(int)
命名空间中定义::
。这是来自不同名称空间的两个不同的函数,具有相同的名称和签名,这会导致冲突。可能不是你想要的。
要定义::PyMega::uber_function
,您必须说
namespace PyMega
{
void uber_function(int) {
....
}
}