简体   繁体   English

对 Py_Initialize() 的未定义引用

[英]Undefined reference to Py_Initialize()

I was trying to embed some Python into a C program, but first I was testing some elementary operations as seen in the following code:我试图将一些 Python 嵌入到 C 程序中,但首先我正在测试一些基本操作,如以下代码所示:

#include <stdio.h>
#include <stdlib.h>
#include <random>
#include <chrono>
#include <iostream>
#include <math.h>
#include <Python.h>

int main()
{
    Py_Initialize();
    PyRun_SimpleString("print('Hello world')");
    Py_Finalize();

    // Some C code
}

It turns out that (a) the makefile does build well the program when commenting out the Python-related lines (Py_Initialize, PyRun_SingleString, Py_Finalize), (b) however, when adding the Python lines, the build returns the following error:事实证明,(a) 在注释掉 Python 相关的行 (Py_Initialize、PyRun_SingleString、Py_Finalize) 时,makefile 确实很好地构建了程序,(b) 但是,当添加 Python 行时,构建返回以下错误:

g++ -o program program.cpp -lm -std=c++11 -O3 -I/usr/include/python3.6m/
/tmp/ccgiujXn.o: In function `main':
program.cpp:(.text.startup+0x62): undefined reference to `Py_Initialize'
program.cpp:(.text.startup+0x70): undefined reference to `PyRun_SimpleStringFlags'
program.cpp:(.text.startup+0x75): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'exec_program' failed
make: *** [exec_program] Error 1

Do you know why this error occurs?你知道为什么会出现这个错误吗? I thought that the Python.h header was correctly added, but it does not seem to be the case when adding the Python lines.我认为正确添加了 Python.h 标头,但添加 Python 行时似乎并非如此。 My makefile is:我的生成文件是:

exec_program:  program.cpp
    g++ -o program program.cpp -lm -std=c++11 -O3 -I/usr/include/python3.6m/
    ./program
    rm program

I am using Ubuntu 18.04.4 LTS, g++ 7.4.0, Python 3.6.9 (/usr/lib/) and 3.7.6 (/home/anaconda3/lib).我使用的是 Ubuntu 18.04.4 LTS、g++ 7.4.0、Python 3.6.9 (/usr/lib/) 和 3.7.6 (/home/anaconda3/lib)。

Sorry if you find the question is reiterative, but I thought no other post had a solution for my problem.抱歉,如果您发现问题是重复的,但我认为没有其他帖子可以解决我的问题。

Thanks in advance for any help!在此先感谢您的帮助!

您必须使用-l链接库

You should not compile your module as stand alone executable, but rather as shared library object *.so , so objects line Py_Initialize will be avaliable at run time.您不应将模块编译为独立的可执行文件,而应将其编译为共享库对象*.so ,因此对象行Py_Initialize将在运行时可用。 The suggested by documentation way is文档方式的建议是

from distutils.core import setup, Extension

module1 = Extension('demo',
                   define_macros = [('MAJOR_VERSION', '1'),
                                 ('MINOR_VERSION', '0')],
                   include_dirs = ['/usr/local/include'],
                   libraries = ['tcl83'],
                   library_dirs = ['/usr/local/lib'],
                   sources = ['demo.c'])

setup (name = 'PackageName',
      version = '1.0',
      description = 'This is a demo package',
      author = 'Martin v. Loewis',
      author_email = 'martin@v.loewis.de',
      url = 'https://docs.python.org/extending/building',
      long_description = '''
This is really just a demo package.
''',
      ext_modules = [module1])

The distutils module will generate correct gcc command for you. distutils模块将为您生成正确的gcc命令。

You can read more about building python modules at official documentation https://docs.python.org/3/extending/building.html#building您可以在官方文档https://docs.python.org/3/extending/building.html#building 中阅读有关构建 python 模块的更多信息

添加/usr/lib/x86_64-linux-gnu/库目录和python3.6m库:

g++ -o program program.cpp -lm -std=c++11 -O3 -I/usr/include/python3.6m/ -L /usr/lib/x86_64-linux-gnu/ -lpython3.6m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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