[英]Building Python C++ extension fails on overloaded functions
我想用C ++編寫一個Python模塊,並嘗試使用distutils
將其模塊化,但是當我嘗試使用重載函數時,它突然給了我一個編譯錯誤。 我該如何應對?
該模塊包含3個文件: pymega.cpp
(Python解釋器的模塊接口), payload.h
和payload.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;
}
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
您在::PyMega
命名空間中聲明了void uber_function(int)
,然后在::
名稱空間中定義了void uber_function(int)
。 這是來自具有相同名稱和簽名的不同名稱空間的兩個不同函數,這會導致沖突。 可能不是您的意圖。
要定義::PyMega::uber_function
您必須說
namespace PyMega
{
void uber_function(int) {
....
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.