我已经创建了一个静态库,其中包含用于导入模块的函数:
void callFn()
{
_putenv_s("PYTHONPATH", ".");
Py_Initialize();
namespace python = boost::python;
try
{
python::object my_python_class_module = python::import("pythonFile");
python::object test = my_python_class_module.attr("Test")();
test.attr("fn")("from c++");
}
catch (const python::error_already_set&)
{
PyErr_Print();
}
}
PythonFile.py的内容:
class Test():
def fn(self,message):
print ("From python ")
我正在从exe中使用的另一个x.dll调用此函数(allFn()):
void ClassName::abc()
{
callFn();
}
最初,当我编译x.dll时,我收到一个链接错误: LINK:致命错误LNK1104:无法打开文件‘Boost_python39-vc142-mt-x64-1_71.lib’
然后,我构建了Boost源代码以创建Boost_python39-vc142-mt-x64-1_71.lib. 这样做可以使编译成功,但是在运行应用程序时,应用程序崩溃并显示错误:
加载库x时出错-无法加载库x:找不到指定的模块.
请注意,在调用Fn()中注释"try"中的3行,并重复上述过程并不会使应用程序崩溃.您知道Boost::Python::Import()为什么会导致应用程序崩溃吗?