0

I've got a Camera (thorlabs dcc1645c) which comes with a "uc480.h" + "uc480.lib" to program it with c++. My problem is that I need to use the Camera with Python, so I tried to call a C++ Class with Python as told by Florian:

Calling C/C++ from python?

This is my c++ code (Cam2.cpp):

\#include "uc480.h"
class Cam{
public:
    UC480_CAMERA_LIST* pucl;
    int nNumberOfCameras;
    int GetNumberOfCameras(){
        return is_GetNumberOfCameras(&nNumberOfCameras);
    }   
};
extern "C" {
    Cam* Cam_new(){ return new Cam(); }
    int  Cam_GetNumberOfCameras(Cam* cam){ cam->GetNumberOfCameras(); }
}

I tried to compile with g++:

g++  -fPIC -c Cam2.cpp -o Cam2.o
g++  -shared -Wl,-soname,libCam2.so -o libCam2.so  Cam2.o

The second line gives an error:

Cam2.o:Cam2.cpp:(.text$_ZN3Cam18GetNumberOfCamerasEv[Cam::GetNumberOfCameras()]+
0x1a): undefined reference to `__imp_is_GetNumberOfCameras'
collect2.exe: error: ld returned 1 exit status

'is_GetNumberOfCameras' is defined in "uc480.lib", so in my opinion there is a problem with the linking. How do I fix it?

Also tell me please, if you now other possibilities to use the Camera with Python.

Community
  • 1
  • 1
Dragoner
  • 123
  • 1
  • 12

2 Answers2

0

I am working from memory; it might be enough to simply add the lib file to the link command, as in

g++  -shared -Wl,-soname,libCam2.so -o libCam2.so uc480.lib Cam2.o

Or, you might need to add an option like -L. -luc480 to the link command, assuming the library is in the same directory as the code.

EvertW
  • 1,160
  • 9
  • 18
  • Tried: ` g++ -shared -Wl,-soname,libCam2.so -o libCam2.so uc480.lib Cam2.o ` got the same error. Tried ` g++ -shared -Wl,-soname,libCam2.so -o libCam2.so Cam2.o -L.-luc480', got: 'c:/anaconda/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64 -mingw32/bin/ld.exe: skipping incompatible .\uc480.lib when searching for -luc48 0 c:/anaconda/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64 -mingw32/bin/ld.exe: cannot find -luc480 collect2.exe: error: ld returned 1 exit status` – Dragoner Apr 23 '14 at 12:24
  • @Dragoner The order of the files/libraries on the command line is important. If file A depends on library B, then A must be before B in the command line. Also, you library file is most likely for Windows while you are building a Linux (I guess) shared object. – Some programmer dude Apr 23 '14 at 12:36
  • I tried to put the command everywhere, but nothing worked. I want to use it with Windows. – Dragoner Apr 23 '14 at 12:59
-1

Thanks for your help so far. I found a solution for my problem. First of all here is my current c++ code:

#include "uc480.h" 
class Cam{
public:
    UC480_CAMERA_LIST* pucl = 0;
    int nNumberOfCameras;   
    int GetNumberOfCameras(){
        return is_GetNumberOfCameras(&nNumberOfCameras);
    }

extern "C"{
    __declspec(dllexport) Cam* Cam_New() { return new Cam(); }
    __declspec(dllexport) int Cam_GetNumberOfCameras(Cam* obj){ return obj->GetNumberOfCameras(); }
     }

I used Visual Studio to build a DLL. I followed the first steps of this instruction http://msdn.microsoft.com/de-de/library/ms235636.aspx :

To create a dynamic link library (DLL) project

  1. On the menu bar, choose File, New, Project.

  2. In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Win32.

  3. In the center pane, select Win32 Console Application.

  4. Specify a name for the solution — for example, MyDLL — in the Solution name box. Choose the OK button.

  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.

  6. On the Application Settings page, under Application type, select DLL.

  7. Choose the Finish button to create the project.

After that I added my c++ code to the MyDLL.cpp and compiled to get MyDLL.dll This can one use with python ctypes:

from ctypes import cdll
lib = cdll.LoadLibrary("PathToLib\MyDLL.dll")
class Camera(object):
    def __init__(self):
        self.obj = lib.Cam_New()
    def GetNumberOfCameras(self):
        return lib.Cam_GetNumberOfCameras(self.obj)
MyCam = Camera()
Numbers = MyCam.GetNumberOfCameras()

This works fine for me.

Dragoner
  • 123
  • 1
  • 12
  • You seem to have switched from *nix to Windows. Why? – David Heffernan Apr 25 '14 at 08:19
  • What do you mean with "*nix" ? – Dragoner Apr 26 '14 at 07:48
  • Generic Unix like system, linux, bsd, Unix, osx – David Heffernan Apr 26 '14 at 07:52
  • In my question I linked to http://stackoverflow.com/questions/145270/calling-c-c-from-python?lq=1 . The owner of this question asked for help with Windows, just as I did the whole time. Never wanted to get a soultion for *nix systems. Sorry for misunderstanding :(. – Dragoner Apr 26 '14 at 07:57
  • So where did the .so extensions come from. They are linux shared libs. It's easy to do this with gcc if you want. – David Heffernan Apr 26 '14 at 08:04
  • I did not know that .so are linux libs. As the owner of the question asked for help with windows and Florian answerd the question with the solution told above, I thought .so would be a necessary part to get it work in python with windows. – Dragoner Apr 26 '14 at 08:13