0

Trying to compile this a.cpp file, using

g++ a.cpp -I ~/anaconda3/include/python3.7m/ -l ~/anaconda3/lib/python3.7/

Error I am getting is

/usr/bin/ld: cannot find -l/home/rverma/anaconda3/lib/python3.7/
collect2: error: ld returned 1 exit status

Tried g++ a.cpp -I ~/anaconda3/include/python3.7m/ -l ~/anaconda3/lib/ this as well for giving

/usr/bin/ld: cannot find -l/home/rverma/anaconda3/lib/ collect2: error: ld returned 1 exit status

Where my a.cpp file looks like this:

#include <Python.h>
#include <stdlib.h>
int main()
{
   // Set PYTHONPATH TO working directory
   setenv("PYTHONPATH",".",1);

   PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;


   // Initialize the Python Interpreter
   Py_Initialize();


   // Build the name object
   pName = PyUnicode_FromString((char*)"arbName");

   // Load the module object
   pModule = PyImport_Import(pName);


   // pDict is a borrowed reference 
   pDict = PyModule_GetDict(pModule);


   // pFunc is also a borrowed reference 
   pFunc = PyDict_GetItemString(pDict, (char*)"someFunction");

   if (PyCallable_Check(pFunc))
   {
       pValue=Py_BuildValue("(z)",(char*)"something");
       PyErr_Print();
       printf("Let's give this a shot!\n");
       presult=PyObject_CallObject(pFunc,pValue);
       PyErr_Print();
   } else 
   {
       PyErr_Print();
   }
   printf("Result is %ld\n",PyLong_AsLong(presult));
   Py_DECREF(pValue);

   // Clean up
   Py_DECREF(pModule);
   Py_DECREF(pName);

   // Finish the Python Interpreter
   Py_Finalize();


    return 0;
}

And my argName.py looks like this:

def someFunction(text):
    print 'You passed this Python program '+text+' from C! Congratulations!'
    return 12345

Please help

  • It looks like your compile command line is wrong: `-l ~/anaconda3/lib/python3.7/` Library paths are given with big L (`-L`) while libraries themselves are given with little L (`-l`). – Scheff's Cat Dec 17 '20 at 12:47
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Scheff's Cat Dec 17 '20 at 12:51
  • @Scheff, I tried using `-L` as well, but at that time, it didn't even try linking, seemed like it was as good if you haven't given the path altogether. – Rishabh Verma Dec 18 '20 at 06:27
  • You have to provide the library paths (each with `-L`) _and_ the libraries you want to link (each with `-l`). Furthermore, libraries on *ix are usually "prefixed" with `lib`. This prefix has to be left out. E.g. to link `libXml.a`, you have to write `-lXm`. (I'm not sure whether it works as well if the library name is given with `lib`.) FYI: [g++ - Options for Linking](https://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Link-Options.html#Link-Options) – Scheff's Cat Dec 18 '20 at 06:40
  • @Scheff, can you please give the complete command, you want to get executed, I am having a bit of trouble, understanding what exactly I need to run. Sorry, if it's a lot to ask. – Rishabh Verma Dec 18 '20 at 06:47
  • Sorry, I cannot. I don't know anaconda and of what libraries it consists. You have to look into the doc. or to google for open source projects where you can get inspiration from. – Scheff's Cat Dec 18 '20 at 06:54

0 Answers0