我正在尝试将Python嵌入Windows的C++ Builder应用程序中。
经过对该主题(example)的大量研究,我发现Python4Delphi项目看起来非常有趣,而且正是我想要做的事情。事实是我不是在使用Delphi(并且我真的不想使用它),而是在使用C++ Builder。
我还找到了一些将Python嵌入C++项目(example)的示例,因此我正在尝试这种方法。问题是,即使第一个简单的“Hello world”示例,我也无法编译。
这是我遵循的步骤:
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include <conio.h>
#include <Python.h>
int main()
{
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
Py_Finalize();
printf("\nPress any key to exit...\n");
if(!_getch()) _getch();
return 0;
}
在编译期间,我多次遇到以下错误:[bcc32c Error] pymath.h(22): declaration conflicts with target of using declaration already in scope.
例如,当编译
pymath.h
的第22行时,就会发生这种情况:#ifndef HAVE_ROUND
extern double round(double);
#endif
我认为这可能是由于编译器和/或pyconfig.h
配置引起的。作为C++的初学者和万物之王,我很难解决这个问题。您能帮我吗,您有什么建议吗?
编辑
从2020年4月开始,C++ Builder似乎支持Boost库,该库可用于在C++程序中实现Python。因此,以前的错误不再出现,而是出现了另一个错误。
使用与以前相同的脚本,但使用
#include <boost/Python.hpp>
,出现错误:[ilink32 Error] Fatal: Impossible to open the file 'LIBBOOST_PYTHON38-BCB32C-MT-S-X32-1_68.LIB'
您有解决这个问题的想法吗?
最佳答案
我在Python4Delphi中使用了C++ Builder 10.4。
我已经为Windows安装了Python:
Python版本
3.8.2(标签/v3.8.2:7b3ab59,2020年2月25日,22:45:29)[MSC v.1916 32位(Intel)]
版本信息。
sys.version_info(major = 3,minor = 8,micro = 2,releaselevel ='final',serial = 0)
安装和测试步骤:
无效__fastcall TForm2::Button1Click(TObject * Sender)
{
PythonEngine1-> ExecStrings(Memo1-> Lines);
}
// ------------------------------------------------ ---------------------------
无效__fastcall TForm2::Button2Click(TObject * Sender)
{
如果(OpenDialog1-> Execute()){
Memo1-> Lines-> LoadFromFile(OpenDialog1-> FileName);
}
}
// ------------------------------------------------ ---------------------------
无效__fastcall TForm2::Button3Click(TObject * Sender)
{
如果(SaveDialog1-> Execute()){
Memo1-> Lines-> SaveToFile(SaveDialog1-> FileName);
}
}
PythonEngine.hpp-C:\ Users \ david \ Documents \ Embarcadero \ Studio \ Projects \ Python4Delphi \ python4delphi-master \ Source
Python_D.bpi,Python_D.dcp,Python_D.lib-C:\ Users \ Public \ Documents \ Embarcadero \ Studio \ 21.0 \ Dcp
我将在一篇博客文章中工作,在那里我可以放置屏幕快照和源代码链接。
现在暂时没有在任何地方安装C++ BUilder 2020。我认为您应该能够使用C++ Builder随附的Delphi命令行编译器,但是必须尝试一下-我在单独的计算机上安装了RAD Studio 10.4和RAD Studio 10.4.1。
关于python - 如何在C++ Builder 2010(非Delphi)应用程序中嵌入Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64100660/