0

I'm trying to embed Python in C++. This is my Python file (with the name EmbedTest.py):

from __future__ import division

class model:  
    def init(self,a,b):  
        self.a = a  
        self.b = b  
         def test_method(a,b):  
    m = model(a,b)  
    m.add(1)  
    print("a: ",a,"b: ",b)  
    return (a+b,a-b,a*b)

This is my C++ file (with the name EmbedTest.cpp and located in the same folder as EmbedTest.py)

#include <Python.h>  

int main(int argc, char *argv[]) {  
    PyObject *pName, *pModule, *pFunc;  
    PyObject *pArgs, *pValue, *pValue_1, *pValue_2;  
    double sum,diff,prod;  
    double a = atof(argv[1]);  
    double b = atof(argv[2]);   
    Py_Initialize();  
    pName = PyUnicode_DecodeFSDefault("EmbedTest.py");  
    pModule = PyImport_Import(pName);  
    Py_DECREF(pName);  

    if(pModule != NULL) {
      pFunc = PyObject_GetAttrString(pModule,"test_method");    
      if(pFunc && PyCallable_Check(pFunc)) {  
          pArgs = PyTuple_New(2);  
          pValue_1 = PyFloat_FromDouble(a);  
          pValue_2 = PyFloat_FromDouble(b);  
          if (!pValue_1) {
          Py_DECREF(pArgs);
            Py_DECREF(pModule);
        fprintf(stderr, "Cannot convert argument\n");
        return 1;  
        }  
        if (!pValue_2) {
        Py_DECREF(pArgs);
        Py_DECREF(pModule);
        fprintf(stderr, "Cannot convert argument\n");
        return 1;  
        }  
        PyTuple_SetItem(pArgs, 0, pValue_1);  
        PyTuple_SetItem(pArgs, 1, pValue_2);  
        
        pValue = PyObject_CallObject(pFunc, pArgs);  
            Py_DECREF(pArgs);  
            if (pValue != NULL) {
            sum = PyFloat_AsDouble(PyTuple_GetItem(pValue,0));
            diff = PyFloat_AsDouble(PyTuple_GetItem(pValue,1));
            prod = PyFloat_AsDouble(PyTuple_GetItem(pValue,2));  
              printf("a: %f b: %f sum: %f diff: %f prod: %f",a,b,sum,diff,prod);  
              Py_DECREF(pValue);  
            }  
            else {  
                Py_DECREF(pFunc);  
                Py_DECREF(pModule);  
                PyErr_Print();  
                fprintf(stderr,"Call failed\n");  
                return 1;  
            }       
          } else {  
              if (PyErr_Occurred())  
                PyErr_Print();  
              fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);  
          }  
          Py_XDECREF(pFunc);  
          Py_DECREF(pModule);  
        }  
        else {  
          PyErr_Print();  
          fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);  
          return 1;  
    }  
    if (Py_FinalizeEx() < 0) {  
        return 120;  
    }  
    return 0;  }

Compiling is fine. I use the flags suggested by python3.6-config --cflags. Hence

gcc -c -I/home/MyFolder/anaconda3/include/python3.6m -Wno-unused-result -Wsign-compare -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -O3 -pipe -fdebug-prefix-map==/usr/local/src/conda/- -fdebug-prefix-map==/usr/local/src/conda-prefix -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -flto -DNDEBUG -fwrapv -O3 -Wall -Wstrict-prototypes EmbedTest.cpp

works fine.

However, when I try to link, I get a problem. I use the flags suggested by python3.6-config --ldflags. Hence I try

gcc -o EmbedTest.o -L/home/MyFolder/anaconda3/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/home/MyFolder/anaconda3/lib -lpython3.6m -lpthread -ldl -lutil -lrt -lm -Xlinker -export-dynamic.

However, I get the following error message:

/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In function `_start':
/build/glibc-6V9RKT/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status

Any idea what goes wrong here?

4
  • 2
    -o EmbedTest.o This means "create an output file named EmbedTest.o". You are overwriting an object file you've compiled a second ago. Commented Sep 29, 2020 at 17:56
  • @n.'pronouns'm. Thanks! If I remove the "-o" I get another error however: bytecode stream generated with LTO version 6.0 instead of the expected 3.0 Do you happen to know what this means? Commented Sep 29, 2020 at 18:00
  • 2
    You are probably trying to link with binaries in /home/MyFolder/anaconda3 built with a compiler much newer than your default one. Upgrade your tools, or remove -flto. Commented Sep 29, 2020 at 18:46
  • @n.'pronouns'm. Thanks for the suggestions. Unfortunately, removing -flto does not change anything the error is still the same. Commented Sep 29, 2020 at 19:02

1 Answer 1

1

You are not linking the .o file, you are outputing to it.

Change

gcc -o EmbedTest.o -L/home/MyFolder/anaconda3/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/home/MyFolder/anaconda3/lib -lpython3.6m -lpthread -ldl  -lutil -lrt -lm  -Xlinker -export-dynamic

to

gcc -o EmbedTest EmbedTest.o -L/home/MyFolder/anaconda3/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/home/MyFolder/anaconda3/lib -lpython3.6m -lpthread -ldl  -lutil -lrt -lm  -Xlinker -export-dynamic
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Unfortunately some new problem showed up (see the comments on my original post), but this is probably due to having old tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.