从C++调用Python,如何链接特定的Acon DA环境?

2024-05-16 02:44:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试将Python嵌入到C++中,遵循以下指南:

https://docs.python.org/3.7/extending/embedding.html

这对于较旧的系统级Python 2.7安装非常有效。我现在正试图使用一个特定的Anaconda环境来完成同样的工作,在那里我安装了一个我想要使用的库

我的hello world示例是

#include <iostream>
#include <Python.h>
int main(int argc, char* argv[]){
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    Py_SetProgramName(program);
    Py_Initialize();

    std::cout << "trying to import dolfin" << std::endl;
    PyRun_SimpleString("import dolfin");
    std::cout << "success?" << std::endl;

    Py_Finalize();
    return 0; 
}

试图通过

g++ -I/home/peter/anaconda3/envs/fenicsproject/include/python3.7m/ python_from_cpp.cpp -L/home/peter/anaconda3/envs/fenicsproject/lib/python3.7/config-3.7m-x86_64-linux-gnu/ -lpython3.7m

(注1:包括部分工程,其他标志为推荐的链接标志,见上述链接中的1.6.) (注2:我希望最终通过mpic++进行编译,但我会首先尝试进行编译。)

这将提供以下输出:

lto1: internal compiler error: in lto_tag_to_tree_code, at lto-streamer.h:1005
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
lto-wrapper: fatal error: g++ returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status

欢迎提供任何帮助/见解


Tags: topyimporthomeincludeerrorprogrampeter