Embedding Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Franz

    #1

    Embedding Python

    Hi!

    I wanted to run some Python code from inside a C program, so I did it
    like it was explained in the Python manual:

    #include <Python.h>
    int main(int argc, char *argv[]) {
    Py_Initialize() ;
    PyRun_SimpleStr ing("print 'Hallo World!'\n");
    Py_Finalize();
    return 0;
    }

    Then I tried to compile this by using the command: gcc halloworld.cpp
    But everytime I get the following errors:

    /home/mmf/tmp/ccVD2V4h.o(.tex t+0x1d): In function `main':
    : undefined reference to `Py_Initialize'
    /home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
    : undefined reference to `PyRun_SimpleSt ring'
    /home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
    : undefined reference to `Py_Finalize'
    /home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
    `__gxx_personal ity_v0'
    collect2: ld returned 1 exit status

    What am I doing wrong?

    Markus
  • Diez B. Roggisch

    #2
    Re: Embedding Python

    > /home/mmf/tmp/ccVD2V4h.o(.tex t+0x1d): In function `main':[color=blue]
    > : undefined reference to `Py_Initialize'
    > /home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
    > : undefined reference to `PyRun_SimpleSt ring'
    > /home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
    > : undefined reference to `Py_Finalize'
    > /home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
    > `__gxx_personal ity_v0'
    > collect2: ld returned 1 exit status
    >
    > What am I doing wrong?[/color]

    Not linking against libpython.so?
    --
    Regards,

    Diez B. Roggisch

    Comment

    • Markus Franz

      #3
      Re: Embedding Python

      Diez B. Roggisch wrote:
      [color=blue][color=green]
      >>/home/mmf/tmp/ccVD2V4h.o(.tex t+0x1d): In function `main':
      >>: undefined reference to `Py_Initialize'
      >>/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
      >>: undefined reference to `PyRun_SimpleSt ring'
      >>/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
      >>: undefined reference to `Py_Finalize'
      >>/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
      >>`__gxx_person ality_v0'
      >>collect2: ld returned 1 exit status
      >>
      >>What am I doing wrong?[/color]
      >
      > Not linking against libpython.so?[/color]

      I don't understand what you mean...

      Markus

      Comment

      • Diez B. Roggisch

        #4
        Re: Embedding Python

        Markus Franz wrote:
        [color=blue]
        > Diez B. Roggisch wrote:
        >[color=green][color=darkred]
        >>>/home/mmf/tmp/ccVD2V4h.o(.tex t+0x1d): In function `main':
        >>>: undefined reference to `Py_Initialize'
        >>>/home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
        >>>: undefined reference to `PyRun_SimpleSt ring'
        >>>/home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
        >>>: undefined reference to `Py_Finalize'
        >>>/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
        >>>`__gxx_perso nality_v0'
        >>>collect2: ld returned 1 exit status
        >>>
        >>>What am I doing wrong?[/color]
        >>
        >> Not linking against libpython.so?[/color]
        >
        > I don't understand what you mean...[/color]

        Well, you need to link against the python library to make known where the
        respective symbols are from. That is also necessary for all other libs out
        there - e.g. the linker switch -lm links against libm.so. For someone who
        _codes_ in C/C++ that should be obvious - or so I thought. What
        build-environment do you use?

        The line
        [color=blue][color=green][color=darkred]
        >>>/home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
        >>>`__gxx_perso nality_v0'[/color][/color][/color]

        suggest that you also miss linking against libstdc++ - an error that
        sometimes happened to me too - no idea why, it _should_ be included
        implicitely when linking C++ objects. No big deal though, just also add it
        to the linking process.
        --
        Regards,

        Diez B. Roggisch

        Comment

        • Mark Tolonen

          #5
          Re: Embedding Python


          "Markus Franz" <mf1987@arcor.d e> wrote in message
          news:576fb15a.0 503260342.15ea4 630@posting.goo gle.com...[color=blue]
          > Hi!
          >
          > I wanted to run some Python code from inside a C program, so I did it
          > like it was explained in the Python manual:
          >
          > #include <Python.h>
          > int main(int argc, char *argv[]) {
          > Py_Initialize() ;
          > PyRun_SimpleStr ing("print 'Hallo World!'\n");
          > Py_Finalize();
          > return 0;
          > }
          >
          > Then I tried to compile this by using the command: gcc halloworld.cpp
          > But everytime I get the following errors:
          >
          > /home/mmf/tmp/ccVD2V4h.o(.tex t+0x1d): In function `main':
          > : undefined reference to `Py_Initialize'
          > /home/mmf/tmp/ccVD2V4h.o(.tex t+0x2a): In function `main':
          > : undefined reference to `PyRun_SimpleSt ring'
          > /home/mmf/tmp/ccVD2V4h.o(.tex t+0x32): In function `main':
          > : undefined reference to `Py_Finalize'
          > /home/mmf/tmp/ccVD2V4h.o(.eh_ frame+0x11): undefined reference to
          > `__gxx_personal ity_v0'
          > collect2: ld returned 1 exit status
          >
          > What am I doing wrong?
          >
          > Markus[/color]

          This works for me on Redhat 9:

          g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
          -ldl -lutil /usr/lib/python2.2/config/libpython2.2.a

          I discovered what libraries to link to by asking distutils:

          import distutils.sysco nfig
          distutils.sysco nfig.get_config _var('LIBS')
          distutils.sysco nfig.get_config _var('SYSLIBS')

          g++ links to the right libraries for .cpp files.

          -Mark


          Comment

          • Diez B. Roggisch

            #6
            Re: Embedding Python

            > This works for me on Redhat 9:[color=blue]
            >
            > g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
            > -ldl -lutil /usr/lib/python2.2/config/libpython2.2.a
            >[/color]

            Why did you chose the static variant? This should be equivalent:

            g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
            -ldl -lutil -lpython2.2

            --
            Regards,

            Diez B. Roggisch

            Comment

            • Mark Tolonen

              #7
              Re: Embedding Python


              "Diez B. Roggisch" <deetsNOSPAM@we b.de> wrote in message
              news:d24ao4$8c1 $00$1@news.t-online.com...[color=blue][color=green]
              >> This works for me on Redhat 9:
              >>
              >> g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
              >> -ldl -lutil /usr/lib/python2.2/config/libpython2.2.a
              >>[/color]
              >
              > Why did you chose the static variant? This should be equivalent:
              >
              > g++ x.cpp -o x -I/usr/include/python2.2 -pthread -lm
              > -ldl -lutil -lpython2.2
              >
              > --
              > Regards,
              >
              > Diez B. Roggisch[/color]

              On my system, for whatever reason, the .so library isn't present. I have
              the python-devel package installed.

              -Mark


              Comment

              • Heiko Wundram

                #8
                Re: Embedding Python

                Am Samstag, 26. März 2005 20:43 schrieb Mark Tolonen:[color=blue]
                > On my system, for whatever reason, the .so library isn't present. I have
                > the python-devel package installed.[/color]

                I actually can't believe this; do

                ldconfig -p|grep "python"

                as root and look for any output. And remember that the shared library isn't
                installed by the devel package, but by the standard python package, as the
                binary /usr/bin/python is only a "stub", which chains to the python
                interpreter in the shared lib (at least for any distribution I know of, it
                would be braindead to link the command-line interpreter statically anyway).

                Sample output:

                heiko heiko # ldconfig -p|grep python
                libpython2.4.so .1.0 (libc6) => /usr/local/lib/libpython2.4.so .1.0
                libpython2.4.so (libc6) => /usr/local/lib/libpython2.4.so
                libpython2.3.so .1.0 (libc6) => /usr/lib/libpython2.3.so .1.0
                libpython2.3.so (libc6) => /usr/lib/libpython2.3.so
                heiko heiko #

                --
                --- Heiko.

                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.4.1 (GNU/Linux)

                iD8DBQBCRczAf0b pgh6uVAMRAv0AAJ 9ro3+QmTIQDgg4/rnuApsqRHffbgCf UCh/
                BZFYiU8h4u6Ya9S CYyZy94A=
                =RrOj
                -----END PGP SIGNATURE-----

                Comment

                • Reinhold Birkenfeld

                  #9
                  Re: Embedding Python

                  Heiko Wundram wrote:[color=blue]
                  > Am Samstag, 26. März 2005 20:43 schrieb Mark Tolonen:[color=green]
                  >> On my system, for whatever reason, the .so library isn't present. I have
                  >> the python-devel package installed.[/color]
                  >
                  > I actually can't believe this; do
                  >
                  > ldconfig -p|grep "python"[/color]

                  Or, use

                  ldd =python

                  to exactly display what library your current executable is using.

                  (users of a shell other than Zsh must replace '=python' by '`which python`')


                  Reinhold

                  Comment

                  • Mark Tolonen

                    #10
                    Re: Embedding Python


                    "Reinhold Birkenfeld" <reinhold-birkenfeld-nospam@wolke7.n et> wrote in
                    message news:3altmaF6ca j9aU1@individua l.net...[color=blue]
                    > Heiko Wundram wrote:[color=green]
                    >> Am Samstag, 26. März 2005 20:43 schrieb Mark Tolonen:[color=darkred]
                    >>> On my system, for whatever reason, the .so library isn't present. I
                    >>> have
                    >>> the python-devel package installed.[/color]
                    >>
                    >> I actually can't believe this; do
                    >>
                    >> ldconfig -p|grep "python"[/color]
                    >
                    > Or, use
                    >
                    > ldd =python
                    >
                    > to exactly display what library your current executable is using.
                    >
                    > (users of a shell other than Zsh must replace '=python' by '`which
                    > python`')
                    >
                    >
                    > Reinhold[/color]

                    $ ldd /usr/bin/python
                    libdl.so.2 => /lib/libdl.so.2 (0x40023000)
                    libpthread.so.0 => /lib/tls/libpthread.so.0 (0x40028000)
                    libutil.so.1 => /lib/libutil.so.1 (0x40036000)
                    libm.so.6 => /lib/tls/libm.so.6 (0x40039000)
                    libc.so.6 => /lib/tls/libc.so.6 (0x42000000)
                    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

                    I also (before I originally posted) did a "find / -name libpython*" with no
                    success. Looks like Redhat 9 ships with a statically linked version of
                    python.

                    -Mark


                    Comment

                    • Heiko Wundram

                      #11
                      Re: Embedding Python

                      Am Samstag, 26. März 2005 21:36 schrieb Mark Tolonen:[color=blue]
                      > I also (before I originally posted) did a "find / -name libpython*" with no
                      > success. Looks like Redhat 9 ships with a statically linked version of
                      > python.[/color]

                      Hmm... Sorry to have thought otherwise... RedHat is braindead. :-)

                      --
                      --- Heiko.

                      -----BEGIN PGP SIGNATURE-----
                      Version: GnuPG v1.4.1 (GNU/Linux)

                      iD4DBQBCRdlbf0b pgh6uVAMRAmImAJ 4q8n+pgUDQ65bDo bXDsunKA8fi9wCY 8N3m
                      mBDvW/jcOEAAfIU4FQm7J g==
                      =wnlO
                      -----END PGP SIGNATURE-----

                      Comment

                      Working...