When Python is embedded from C language, an error occurs in which Py_Initialize() does not exist.

Asked 1 years ago, Updated 1 years ago, 81 views

Nice to meet you.I recently started Python.
We are currently developing in C language and would like to do some of the processing in Python.
I used this article to embed Python.
http://d.hatena.ne.jp/mscp/20090919/1261917834

Based on the results,
Py_Initialize() or less unique functions do not exist. An error has occurred and cannot be compiled.
Makefile and error details are as follows:

gcc-I/usr/include/python 2.6-L/usr/lib/python 2.6 test.c-otest

Error Contents

 make: Warning: File `Makefile' has a modified time of 7.4e+02 seconds, which is the future time gcc
 - I/usr/include/python 2.6 - L/usr/lib/python 2.6 test.c-o test Infile included from/usr/include/python 2.6/Python.h:8,
                  from test.c:7:/usr/include/python 2.6/pyconfig.h:1031:1:warning:"_POSIX_C_SOURCE"
 definedInfile included from /usr/include/sys/types.h:27,
                  from test.c:1:/usr/include/features.h:210:1:warning:this is the location of the previous definition
 /tmp/ccZzd8yt.o:in function `main':test.c:(.text+0x381):
 Undefined reference to `Py_SetProgramName' test.c:(.text+0x386):
 Undefined reference to `Py_Initialize' test.c:(.text+0x392):
 Undefined reference to `PyImport_ImportModule' test.c:(.text+0x3b8):
 Undefined reference to `PyObject_CallMethod' test.c:(.text+0x3de):
 Undefined reference to `PyArg_Parse' test.c:(.text+0x3ef): `Py_Finalize'
 Undefined reference to collect2:ld returned1 exit status make:***
 [test] Error 1

I think the library is probably not well pasted, but
I don't know how to do it.
Please give me some advice.

◆Development environment
OS: Debian@SQUEEZE
Version—Python 2.6
IDE:NetBeans

test.c

#include<sys/types.h>
# include<sys/stat.h>
# include <fcntl.h>
# include <signal.h>
# include <termios.h>
# include <unistd.h>
# include <Python.h>

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

# define MAIN_C
# include "exitfail.h"

#define SERIAL_BOUNDRATE 115200
# define BUF_SIZE 256
#define MAXITEM20

#define THRMO7

#define INDEX_HEADER10
#define INDEX_HEADER21
#define INDEX_DATASIZE 12
#define INDEX_DATASIZE23
#define INDEX_DATA4
#define BIT_OFSET8

#define HEEX_HEADER 10xA5
#define HEEX_HEADER 20x5A
# define HEX_MSB0x8000
# define HEX_EOT0x04

int main(intargc, char*argv[])
{

    exitfail_init();
    PyObject*pModule,*pTmp;
    char*sTmp;
    Py_SetProgramName(argv[0]);
    Py_Initialize();

    pModule=PyImport_ImportModule("script");


    pTmp = PyObject_CallMethod(pModule, "func", NULL);


    PyArg_Parse(pTmp, "s", & sTmp);

    printf("%s\n", sTmp);

    Py_Finalize();

    return EXIT_SUCCESS;
}

python linux gcc

2022-09-30 21:19

1 Answers

gcc-I/usr/include/python 2.6-L/usr/lib/python 2.6 test.c-otest

You have specified a library path, but you have not specified a critical library.

gcc-I/usr/include/python 2.6-L/usr/lib/python 2.6 test.c-o test-lpython 2.6

Is that right? (I don't know what the name of the debian library is, so if it's wrong, look in /usr/lib/python2.6)

However, we recommend using the python-config and pkg-config commands to specify the appropriate compilation options.

gcc test.c-o test$(python-config --cflags --ldflags)

Just to be sure, the above command is to run on bash.
When writing in Makefile, the "$" mark is considered a macro, so

gcc test.c-o test$$(python-config --cflags --ldflags)

Overlap "$" like this or

gcc test.c-o test$ (shell python-config --cflags --ldflags)

Use the "shell" macro as shown in .

(I've never used NetBeans, so maybe there's a NetBeans way, so I'm sorry.)

Not for the questioner

(When I tried linuxmint18, python-config and pkg-config output are different, is that okay?)

$pkg-config --cflags --libs python
-I/usr/include/python 2.7 -I/usr/include/x86_64 -linux-gnu/python 2.7 -lpython 2.7
$ python-config --cflags --libs
-I/usr/include/python 2.7-I/usr/include/x86_64-linux-gnu/python 2.7-fno-strategic-alizing-Wdate-time-D_FORTIFY_SOURCE=2-g-fastack-protector-strong-Wformat-Werror=format-security-Wrapunde-Offic-WrapDec-WrapDec-Olde
-lpython 2.7-lpthread-ldl-lutil-lm 

It's going to be long, so I'll leave a comment here.

The first is the library filename.

The file name of the gcc library is usually iblibthrow.a 」 and --lthrowthrow 」 when linking.
So this time I'm looking for libpython 2.6.a (or libpython*.a).

First of all, I would like to confirm.

  • Is debian a 32-bit version?Is it a 64-bit version?
  • Does python use the one in the Debian package?
  • Run dpkg-query-Libpython 2.6-dev to see a list of the package filenames.Look for "libpython 2.6.a" in it and see if it is in "/usr/lib/python 2.6".
    If the directory is different, change "-L/usr/lib/python 2.6" to "-L<that directory>" and build it.
  • Try running python-config --cflags --ldflags, as you wrote for each person above, and check the output.If "python-config" cannot be performed, install "python-dev".(I don't have squeeze on hand, so the package name may be wrong.)
  • If that doesn't work, try running "find/usr/lib-name 'libpython*.a' to find the python library file.

As #simone said, the compilation goes through, so the library should have been installed...

# (I changed python-config's "--libs" to "--ldflags" after Neuront pointed it out.)


2022-09-30 21:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.