简体   繁体   中英

How to embed Python in a C++ Builder 2010 (not Delphi) application?

I'm trying to embed Python in a C++Builder application for Windows.

After many research on the subject ( example ), I have found the Python4Delphi project that seems very interesting and exactly what I'm trying to do. The thing is that I'm not using Delphi (and I don't really want to use it) but only C++Builder.

I have also found some examples to embed Python in C++ projects ( example ), so I am trying this method. The problem is I can not compile even the first simple "Hello world" example.

Here are the steps I followed:

  1. Creating in C++ builder a New console application project
  2. Include Python.h files
  3. compile the following code:
#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;
}

During the compilation I am getting the following error multiples times:

[bcc32c Error] pymath.h(22): declaration conflicts with target of using declaration already in scope.

This happens, for example, when line 22 from pymath.h is compiled:

#ifndef HAVE_ROUND
extern double round(double);
#endif

I think it probably comes because of compiler and/or pyconfig.h configuration. As a beginner in C++ and this king of thing, it is difficult for me to solve this problem.

Can you help me, do you have any suggestion?

EDIT

Since April 2020, C++Builder seems to support the Boost library, which can be used to implement Python in a C++ program. So the previous errors no longer appear, instead I have a different error.

Using the same script as before, but using #include <boost/Python.hpp> , I have the error:

[ilink32 Error] Fatal: Impossible to open the file 'LIBBOOST_PYTHON38-BCB32C-MT-S-X32-1_68.LIB'

Do you have an idea to solve this problem?

I used C++Builder 10.4 with Python4Delphi.

I have Python for Windows installed:

Python version 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] Version info. sys.version_info(major=3, minor=8, micro=2, releaselevel='final', serial=0)

Steps to install and test:

  1. Grabbed the latest release of Python4Delphi on GitHub ( https://github.com/pyscripter/python4delphi ). Unzipped it to my projects folder.

  2. Opened the Python_D.dproj package project.

  3. Set the Project Options for the Delphi Compiler | C/C++ Output file generation option to Generate all C++Builder files (including package libs)

  4. Built and installed the Delphi4Python components

  5. Opened the Demo1 Delphi project and tested it to make sure that I had the components and Python working. C:\\Users\\david\\Documents\\Embarcadero\\Studio\\Projects\\Python4Delphi\\python4delphi-master\\Demos\\Demo01

  6. Created a New C++Builder VCL application. Copied all of the components from the Delphi Demo01 form.

  7. added event handlers for the 3 buttons:

    void __fastcall TForm2::Button1Click(TObject *Sender) { PythonEngine1->ExecStrings( Memo1->Lines ); } //---------------------------------------------------------------------------

    void __fastcall TForm2::Button2Click(TObject *Sender) { if (OpenDialog1->Execute()) { Memo1->Lines->LoadFromFile( OpenDialog1->FileName ); } } //---------------------------------------------------------------------------

    void __fastcall TForm2::Button3Click(TObject *Sender) { if (SaveDialog1->Execute()) { Memo1->Lines->SaveToFile( SaveDialog1->FileName ); } }

  8. Did a project make - when asked where the header file and lib files were I pointed them to (you could also just update the project's search directories).

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

  1. Ran the Demo1 C++ version - and could put in Python script code and test successfully :D

I'll work on a blog post where I can put screen shots and source code link.

Don't have C++BUilder 2020 installed anywhere for now. I think you should be able to use the Delphi command line compiler that comes with C++Builder but will have to try this - I have RAD Studio 10.4 and RAD Studio 10.4.1 on separate machines.

According to Python documentation, Borland compiler is supported.

So, at the moment, the only way is to revert back to the use "Classic Borland Compiler" in the "Project Options > Building > C++ Compiler" tab.

Then you need to create OMF compatible libs out of the dlls provided with the installation and add them to the project:

implib -aa python3.lib python3.dll
implib -aa python39.lib python39.dll

If you get a linker error:

[ilink32 Error] Error: Unresolved external '__Py_RefTotal' referenced from ...
[ilink32 Error] Error: Unresolved external '__Py_NegativeRefcount' referenced from ...

you need to modify the include file pyconfig.h:

#ifdef _DEBUG
//#       define Py_DEBUG  <<--- comment this!!
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM