1

Below is the code for calling the C++ API's from Python:

#include <Python.h>

#include "dijsdk.h"

#include <iostream>

#include <QString>

#include "arraysize.h"

static PyObject* SDKTest_GetVersion(PyObject* self,PyObject* args);

static PyMethodDef defmethod[]={
{"DijSDK_GetVersion", SDKTest_GetVersion, METH_VARARGS, "DijSDK_GetVersion() -> string. Retrieves SDK version number."},
{NULL,NULL,0,NULL}
};

PyMODINIT_FUNC

initSDKTest(void) {
(void) Py_InitModule("SDKTest", defmethod);
}

static PyObject* SDKTest_GetVersion(PyObject* self,PyObject* args)
{
const char* name;

if (!PyArg_ParseTuple(args, "s", &name))
return NULL;

printf("Hello %s!\n", name);

error_t result = E_OK;
QString m_guids[16];
DijSDK_CamGuid guids[ARRAYSIZE(m_guids)] = {0};
unsigned int numGuids = ARRAYSIZE(guids);
char version[256] = {0};
unsigned int maxLength = ARRAYSIZE(version);
result = DijSDK_GetVersion(version, maxLength);
std::cout<<"version = "<<version<<"\n";
result = DijSDK_Init(); //API call
result = DijSDK_FindCameras(guids, &numGuids); //API call
std::cout<<"\nNumguids = " <<numGuids;
std::cout<<"\nResult = "<<result;
Py_RETURN_NONE;
}

I am compiling this file using the following command:

  $g++ -fPIC -g -c -Wall -I/usr/include/python2.7 -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. SDKTest.cpp

I am creating the .so file using the command:

  $ g++ -shared -rdynamic -o SDKTest.so SDKTest.o -L/usr/lib/i386-linux-gnu/ -lpython2.7 -lDijSDK -lQtGui -lQtCore -Wl,-rpath=/home/embadmin/Desktop/linux/DijSDK-1.1.7-Linux/bin/

And once .so file is created I am placing the file in the : /usr/lib/python2.7/dist-packages and I am importing this module "SDKTest" from the python interpreter.

Here everything is going fine without any errors.

My concern here is: During runtime the C++ library DijSDK.so file will load some library which should be placed where the .so file is loading from the python. I have the placed runtime libraries in the folder where .so file is placed but still it is not getting loaded.

I have tried the same scenario with the pure c++ code and the libraries are getting called perfectly and whats special with the python here? I am doing anything wrong here?

And the same code is working fine in Windows, there I am creating the pyd file using Visual Studio.

Is there any settings I have to do in Linux to work correctly?

Bharathi
  • 337
  • 1
  • 5
  • 17

0 Answers0