[ell-i-developers] Re: Embedding python in emulator

  • From: Asif Sardar <engr.asif.sardar@xxxxxxxxxxxxxx>
  • To: "ell-i-developers@xxxxxxxxxxxxx" <ell-i-developers@xxxxxxxxxxxxx>
  • Date: Wed, 15 Oct 2014 13:48:01 +0300

Yup, cross-compiling will not work, in website the procedure was basically
cross-compiling python for target platform.

Anyway, it seems to be 32-bit vs 64-bit platform issue. I separately
compiled and linked a C++ program to embed python in it using 32-bit
python. It compiled and linked with those CFLAGS and LDFLAGS, but the
executable was not running reporting error: cannot execute binary file:
Exec format error. I am using 64 bit OS Ubuntu and when I compile the above
program using the default python I have in Ubuntu, it compiled, linked and
worked. Also, the following errors seems to be processor architectural
issues with binaries. I don't have enough skills with kernel-level or OS
system level debugging issues, so if anybody else got any ideas then please
share with us on this platform.

The program is also attached to this e-mail, its very simple program to
test embedding python in C/C++ programs.

I will stick to the procedure where python calls C/C++ code in emulator and
in return gets values from the emulator using callbacks.

On Tue, Oct 14, 2014 at 8:58 PM, Pekka Nikander <pekka.nikander@xxxxxx>
wrote:

> I don't think cross-compiling python for ARM is the right way.  You will
> not be able to run the result anywhere.
>
> --Pekka
>
>
> On 2014–10–14, at 20:27 , Asif Sardar <engr.asif.sardar@xxxxxxxxxxxxxx>
> wrote:
>
> By native I meant my own laptop e.g. using gcc-linux to compile python. I
> want to use python interpretor to be called from emulator code to call some
> python functions (This will help later on for multi-purposes e.g. we can
> call python from C/C++ independently). Currently, the callbacks I have
> implemented have to pass-on information to python by actually calling the
> emulator from python itself (2-way calling). If I am able to embed python
> in emulator, it will asynchronously or independently call python whenever
> needed from C/C++ code. Actually, the test libraries are written in python
> language in Robot Framework that is why I am interested to do these kind of
> functionality to both C/C++ and python.
>
> I will try the steps in
> https://www.ailis.de/~k/archives/19-ARM-cross-compiling-howto.html#python
> to cross-compile the python for ARM itself. I will share the findings here!
>
> On Tue, Oct 14, 2014 at 8:12 PM, Jose Granados Vergara <
> jose.granados@xxxxxx> wrote:
>
>>  Isn't the emulator native? why do you need to bind Python to the ARM
>> binaries?
>>
>>  -Jose
>>   ------------------------------
>> *De:* Asif Sardar [engr.asif.sardar@xxxxxxxxxxxxxx]
>> *Enviado:* martes, 14 de octubre de 2014 08:04 p.m.
>> *Para:* Jose Granados Vergara
>> *CC:* ell-i-developers@xxxxxxxxxxxxx; Lari Lehtomäki
>> *Asunto:* Re: Embedding python in emulator
>>
>>   Thanks Jose,
>>
>>  On native platform, its working for me as well. I think I have to
>> re-compile or cross-compile 32-bit python using ARM tools e.g. using
>> something like CXX=arm-none-linux-gnueabi-g++ and then rebuild robot
>> framework.
>>
>>  I have to compile the emulator files using arm-tool chain and I am
>> including the 32-bit python in it built with native gcc toolchain.
>>
>>  If anyone else has some suggestions that I will really appreciate!
>>
>> On Tue, Oct 14, 2014 at 7:58 PM, Jose Granados Vergara <
>> jose.granados@xxxxxx> wrote:
>>
>>>  This worked for me:
>>>
>>> something.c:
>>>
>>> #include "Python.h"
>>>
>>> int main()
>>> {
>>>     Py_Initialize();
>>>     return 0;
>>> }
>>>
>>> ~$ g++ -I/usr/include/python2.7 something.c -o something -lpython2.7
>>>
>>>
>>>  -Jose
>>>   ------------------------------
>>> *De:* Asif Sardar [engr.asif.sardar@xxxxxxxxxxxxxx]
>>> *Enviado:* martes, 14 de octubre de 2014 07:40 p.m.
>>> *Para:* ell-i-developers@xxxxxxxxxxxxx
>>> *CC:* Lari Lehtomäki; Jose Granados Vergara
>>> *Asunto:* Fwd: Embedding python in emulator
>>>
>>>    Hi,
>>>
>>>  Has anybody embedded python in C/C++ application. My platform is
>>> 64-bit and python binaries I am using for Runtime/emulator is 32-bit. I
>>> have added following flags in CFLAGS and LDFLAGS to compile and link the
>>> c/c++ files in emulator with (#include
>>> </path/32-bit-python/include/python.h>)
>>>
>>>  CFLAGS += -I/opt/pym32/bin/include/python2.7 -fno-strict-aliasing -DNDEBUG 
>>> -g -fwrapv -O3 -Wall -Wstrict-prototypes
>>>
>>>  LDFLAGS += -L/opt/pym32/lib/python2.7/config -lpthread -ldl -lutil -lm 
>>> -lpython2.7 -Xlinker -export-dynamic
>>>
>>>  The files compiles but breaks when linking. Can anybody help with
>>> following errors:
>>>
>>>  -----------------------
>>>  ./libsystem_stm32f4discovery.a(Register.o): In function
>>> `Register::GPIO_Callback_Python(std::string, std::string, unsigned int,
>>> std::string)':
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:41:
>>> undefined reference to `Py_Initialize'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:43:
>>> undefined reference to `PyRun_SimpleStringFlags'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:44:
>>> undefined reference to `Py_Finalize'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:41:
>>> undefined reference to `Py_Initialize'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:43:
>>> undefined reference to `PyRun_SimpleStringFlags'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:44:
>>> undefined reference to `Py_Finalize'
>>> ./libsystem_stm32f4discovery.a(system_stm32f4xx.o): In function
>>> `__exchange_and_add':
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>>  -----------------------
>>>
>>>
>>> ---------- Forwarded message ----------
>>> From: Asif Sardar <engr.asif.sardar@xxxxxxxxxxxxxx>
>>> Date: Mon, Oct 13, 2014 at 6:17 PM
>>> Subject: Embedding python in emulator
>>> To: Pekka Nikander <pekka.nikander@xxxxxx>
>>>
>>>
>>> Hi Pekka,
>>>
>>>  I am trying to include python.h in emulator/inc/register.h
>>>
>>>  I have to include cflags and ldflags in Makefile system of emulator
>>> e.g.
>>>
>>>  -I/opt/include/python2.7 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 
>>> -Wall -Wstrict-prototypes
>>>
>>>  -L/opt/lib/python2.7/config -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker 
>>> -export-dynamic
>>>
>>>  However, I am getting:
>>> ----------------------------------------------------
>>>  g++ -m32 -march=i386 -m32 -march=i386 -Xlinker -export-dynamic -o
>>> robot_library  robot_library.o stm32f4discovery.o libSPI.a "-L."
>>> "-L/home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../variants/stm32f4discovery"
>>> "-lsystem_stm32f4discovery" -lstdc++
>>> ./libsystem_stm32f4discovery.a(Register.o): In function
>>> `Register::GPIO_Callback_Python(std::string, std::string, unsigned int,
>>> std::string)':
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:41:
>>> undefined reference to `Py_Initialize'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:43:
>>> undefined reference to `PyRun_SimpleStringFlags'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:44:
>>> undefined reference to `Py_Finalize'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:41:
>>> undefined reference to `Py_Initialize'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:43:
>>> undefined reference to `PyRun_SimpleStringFlags'
>>> /home/asif/Ell-i-Working-Directory/Ell-i-Software-Development/Runtime/stm32/tests/robot_library/../../emulator/src/Register.cpp:44:
>>> undefined reference to `Py_Finalize'
>>> ./libsystem_stm32f4discovery.a(system_stm32f4xx.o): In function
>>> `__exchange_and_add':
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> /usr/include/c++/4.8/ext/atomicity.h:49: undefined reference to
>>> `__atomic_fetch_add_4'
>>> collect2: error: ld returned 1 exit status
>>> make[1]: *** [robot_library] Error 1
>>>  -----------------------------------
>>>
>>>  Can you help me in including these flags in the Makefile system and
>>> link the python with it? It will help a lot, to call from c to python
>>> independently.
>>>
>>>
>>>
>>>  --
>>>
>>>
>>>
>>> *With Best Regards, Asif Sardar. +358 43 8265795 <%2B358%2043%208265795>
>>> *
>>>
>>>
>>>
>>>  --
>>>
>>>
>>>
>>> *With Best Regards, Asif Sardar. +358 43 8265795 <%2B358%2043%208265795>
>>> *
>>>
>>
>>
>>
>>  --
>>
>>
>>
>> *With Best Regards, Asif Sardar. +358 43 8265795 <%2B358%2043%208265795> *
>>
>
>
>
> --
>
>
>
> *With Best Regards,Asif Sardar.+358 43 8265795 <%2B358%2043%208265795>*
>
>
>


-- 



*With Best Regards,Asif Sardar.+358 43 8265795*
#include </opt/pym32/include/python2.7/Python.h>

int
main(int argc, char *argv[])
{
  //Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

Other related posts: