1

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tomasso
  • 23
  • 9
  • 1
    “*The thing is that I'm not using Delphi (and I don't really want to use it) but only C++Builder.*” - FYI, C++Builder includes a Delphi compiler, so you can use Delphi components/libraries in C++Builder projects. – Remy Lebeau Sep 30 '20 at 08:04
  • 1
    You are right, a Delphi compiler is there. I agree that using P4D should be possible but, not having Delphi in my distribution, I can't even open the "Demos" files to learn how to use P4D and try to make it work with C++ projects (also, I don't know how to code in Delphi at all). – Tomasso Sep 30 '20 at 08:34
  • 1
    you don’t need to open Delphi *projects* in the IDE just to look at their *source code*. But if you don’t know how to at least *read* Delphi code, you are likely going to have difficulties being successful with C++Builder long term, since most components, demos, documentations, etc that could be applied to C++Builder are written for Delphi only. C++Builder shares common frameworks with Delphi, and has many compiler extensions for Delphi compatibility. So having at least a working understanding of Delphi is a fairly important skill for any C++Builder developer to have. – Remy Lebeau Sep 30 '20 at 08:36

2 Answers2

1

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
Alex B.
  • 43
  • 1
  • 5
  • @user186876 (the great David I) thanks for your effort and suggestions for this! P4D is a great thing and of course a good way to achieve the results, but I agree with Tomasso: there are situations where you "could" use a delphi component but better not. After all, if the library you need to include IS a C/C++ library, why my C/C++ compiler should not directly embed it?? – Alex B. Mar 03 '21 at 17:05
0

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.

user186876
  • 191
  • 5