1

I am trying to embed Python into C++ following this guide here:

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

This works very well for an older system-wide Python 2.7 installation. I am now trying to do the same using a particular Anaconda enviroment where I installed a library I want to use.

My hello world example for this is

#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; 
}

Trying to compile this via

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

(Note1: The include part works, the other flags are recommended linking flags, see 1.6. in above link) (Note2: I want to compile via mpic++ in the end, but trying to get this going first.)

This gives the output:

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

Any help/insights are appreciated.

Peter Meisrimel
  • 392
  • 2
  • 10
  • 1
    Offhand, it looks like you're using the g++ that's part of your main OS. Can you see what happens if you use the Anaconda-provided g++ (probably in the package `gxx_impl_linux-64`)? – jjramsey Mar 03 '20 at 17:12
  • I tried with "x86_64-conda_cos6-linux-gnu-g++" instead of g++, which gives me ~100 lines of this type: /home/peter/anaconda3/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: /tmp/cctY26pt.ltrans1.ltrans.o: in function `thread_stack_size': :(.text.unlikely+0x4496): undefined reference to `pthread_attr_setstacksize'. – Peter Meisrimel Mar 04 '20 at 09:27
  • What happens if you add `-lpthread` to the command line you use to compile? – jjramsey Mar 04 '20 at 12:47
  • This did indeed fix the compiling issue, thanks! (Needed to add a couple more flags:-lpthread -lrt -ldl -lutil did it in the end). Now I am getting issues when trying to run: Could not find platform independent libraries Could not find platform dependent libraries Consider setting $PYTHONHOME to [:], this seems to be a not uncommon issue with multiple Python installations. – Peter Meisrimel Mar 04 '20 at 13:35
  • Hey, that's progress. Anyway, I'd recommend doing what the error message says. If you're not sure how to do that, Googling "bash set environment variable" (without the quotes) should get you plenty of information on the matter. – jjramsey Mar 04 '20 at 13:49
  • Progress indeed. Setting PYTHONHOME and PYTHONPATH to the correct environment lets it run. However when importing my desired package, some initialization inside that package fails, but I suppose that might be specific to that package. Thanks for all the help! – Peter Meisrimel Mar 04 '20 at 16:13
  • Note to anyone coming across this in the future: I instead switched to a system-wide installation of Python 3.6 and my package, which works without any problems so far. – Peter Meisrimel Mar 09 '20 at 09:05
  • @PeterMeisrimel So basically, your solution to using an Anaconda env of Python with C++ is not to use Anaconda? – Avrdan Feb 13 '23 at 08:52
  • Correct, it was easier in the end. – Peter Meisrimel Feb 14 '23 at 09:51

0 Answers0