2014-04-22 86 views
0

我有一個帶有「uc480.h」+「uc480.lib」的攝像頭(thorlabs dcc1645c)用C++編程。我的問題是我需要使用與Python的相機,所以我試圖調用C++類與Python作爲告知由弗洛裏安:使用ctypes從python調用C++:g ++ undefined reference

Calling C/C++ from python?

這是我的C++代碼(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(); } 
} 

我試圖克++編譯:

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

第二行給出了一個錯誤:

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'在「uc480.lib」中定義,所以我認爲鏈接存在問題。我如何解決它?

也請告訴我,如果你現在有其他的可能性,使用相機與Python。

+2

那麼你實際上並沒有*與'uc480'庫鏈接*。因此,鏈接庫時無法找到該函數。 –

+0

請告訴我,我如何鏈接它。 – Dragoner

回答

0

我從內存中工作;它可能是不夠的,只是添加的lib文件鏈接命令,如

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

或者,你可能需要像-L. -luc480添加一個選項的鏈接命令,假設該庫是在同一目錄下碼。

+0

試過:'g ++ -shared -Wl,-soname,libCam2.so -o libCam2.so uc480.lib Cam2.o'得到了同樣的錯誤。試過'g ++ -shared -Wl,-soname,libCam2.so -o libCam2.so Cam2.o -L.-luc480',得到:'c:/ anaconda/mingw/bin /../ lib/gcc/x86_64- w64-mingw32/4.7.0 /../../../../ x86_64-w64 -mingw32/bin/ld.exe:跳過不兼容。\ uc480.lib搜索-luc48時 c: /anaconda/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64 -mingw32/bin/ld.exe:can not找到-luc480 collect2.exe:錯誤:ld返回1退出狀態# – Dragoner

+0

@Dragoner命令行上的文件/庫的順序很重要。如果文件A依賴於庫B,則A必須位於命令行中的B之前。此外,當您構建Linux(我猜)共享對象時,您的庫文件最有可能用於Windows。 –

+0

我試圖把命令放在任何地方,但沒有任何工作。我想在Windows中使用它。 – Dragoner

-1

感謝您的幫助。我爲我的問題找到了解決方案。首先這裏是我目前的C++代碼:

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

我用Visual Studio來構建一個DLL。我跟着這個指令http://msdn.microsoft.com/de-de/library/ms235636.aspx的第一步:

要創建一個動態鏈接庫(DLL)項目

  1. 在菜單欄,選擇文件,新建,項目。

  2. 在New Project對話框的左窗格中,展開Installed,Templates,Visual C++,然後選擇Win32。

  3. 在中央窗格中,選擇Win32控制檯應用程序。

  4. 在解決方案名稱框中指定解決方案的名稱 - 例如MyDLL。選擇確定按鈕。

  5. 在Win32 Application Wizard對話框的Overview頁面上,選擇Next按鈕。

  6. 在應用程序設置頁面的應用程序類型下選擇DLL。

  7. 選擇完成按鈕來創建項目。

之後,我將我的C++代碼添加到MyDLL.cpp並編譯爲獲取MyDLL。DLL 這可以用一個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() 

這對我工作得很好。

+0

您似乎已經從* nix切換到Windows。爲什麼? –

+0

「* nix」是什麼意思? – Dragoner

+0

通用Unix像系統,Linux,BSD,Unix,OSX –