未解析的外部符号生成Python modu

2024-03-28 22:04:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我终于把Python集成到我的程序中了;我已经取得了很好的进展,但我(希望)遇到了最后一个障碍。我有以下代码(在python_mgr.cpp内):

void say_hello(const char* name)
{
    std::cout << "Hello, " << name << "!" << std::endl;
}

BOOST_PYTHON_MODULE(hello)
{
    def("say_hello", say_hello);
};

这很好,我可以构建一个hello.pyd文件并将其导入到要使用的程序中。但是,如果我将say_hello的内容更改为其他内容,例如:

^{pr2}$

我从setup.py得到以下输出:

running build
running build_ext
building 'hello' extension
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\boost_1_55_0 -I..\ext\glm-0.9.4.4 -IC:\Python27\include -IC:\Python27\PC /Tp../mandala/python_mgr.cpp /Fobuild\temp.win32-2.7\Release\../mandala/python_mgr.obj
python_mgr.cpp
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xlocale(337) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
c:\python27\include\pymath.h(22) : warning C4273: 'round' : inconsistent dll linkage
        C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\math.h(516) : see previous definition of 'round'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\BIN\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\boost_1_55_0\stage\lib /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:inithello build\temp.win32-2.7\Release\../mandala/python_mgr.obj /OUT:build\lib.win32-2.7\hello.pyd /IMPLIB:build\temp.win32-2.7\Release\../mandala\hello.lib /MANIFESTFILE:build\temp.win32-2.7\Release\../mandala\hello.pyd.manifest
   Creating library build\temp.win32-2.7\Release\../mandala\hello.lib and object build\temp.win32-2.7\Release\../mandala\hello.exp
python_mgr.obj : error LNK2019: unresolved external symbol "public: void __thiscall mandala::app_t::exit(void)" (?exit@app_t@mandala@@QAEXXZ) referenced in function __catch$?exec@python_mgr_t@mandala@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z$0
python_mgr.obj : error LNK2019: unresolved external symbol "struct mandala::app_t mandala::app" (?app@mandala@@3Uapp_t@1@A) referenced in function __catch$?exec@python_mgr_t@mandala@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z$0
build\lib.win32-2.7\hello.pyd : fatal error LNK1120: 2 unresolved externals
Press any key to continue . . . 

我不知道如何解决这个问题,也不知道为什么会出错。以下是我的setup.py脚本供参考:

from distutils.core import setup
from distutils.extension import Extension

setup(name='mandala',
    ext_modules=[
        Extension('hello', ['../mandala/python_mgr.cpp'],
        include_dirs=['C:\\boost_1_55_0', '..\ext\glm-0.9.4.4'],
        library_dirs=['C:\\boost_1_55_0\stage\lib'])
    ])

提前感谢您的帮助!在


Tags: buildapphelloreleaselibsetuptempcpp
1条回答
网友
1楼 · 发布于 2024-03-28 22:04:38

你收到的链接器错误抱怨:

public: void __thiscall mandala::app_t::exit(void)

它是这个类指针、“mandala::app”结构和exit函数。在

^{pr2}$

这意味着你编译的对象文件,或python所称的“模块”,不能识别这个mandala、app或exit是什么

this->mandala::app.exit();

因为它不包含该类的编译代码,“app”结构和相应的“exit”调用。(请注意,本例中的this调用是隐式的,但仍在使用。)

您需要将它们添加到模块中,或者将它们编译成单独的模块并将其链接起来。在

相关问题 更多 >