简体   繁体   English

使用ctypes从python调用c ++:g ++未定义参考

[英]Calling c++ from python using ctypes: g++ undefined reference

I've got a Camera (thorlabs dcc1645c) which comes with a "uc480.h" + "uc480.lib" to program it with c++. 我有一个Camera(thorlabs dcc1645c),它带有一个“ uc480.h” +“ uc480.lib”,可以用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: 我的问题是我需要将Camera与Python一起使用,所以我尝试按照Florian的说明使用Python调用C ++类:

Calling C/C++ from python? 从python调用C / C ++吗?

This is my c++ code (Cam2.cpp): 这是我的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(); }
}

I tried to compile with g++: 我试图用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. “ is_GetNumberOfCameras”在“ uc480.lib”中定义,因此我认为链接存在问题。 How do I fix it? 我如何解决它?

Also tell me please, if you now other possibilities to use the Camera with Python. 还请告诉我,如果您现在还有其他将Python与Python结合使用的可能性。

I am working from memory; 我正在记忆中工作; it might be enough to simply add the lib file to the link command, as in 只需将lib文件添加到链接命令中就足够了,例如

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

Or, you might need to add an option like -L. -luc480 或者,您可能需要添加-L. -luc480类的选项-L. -luc480 -L. -luc480 to the link command, assuming the library is in the same directory as the code. -L. -luc480链接命令,假定库与代码位于同一目录中。

Thanks for your help so far. 感谢您一直以来的帮助。 I found a solution for my problem. 我找到了解决问题的方法。 First of all here is my current c++ code: 首先,这里是我当前的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(); }
     }

I used Visual Studio to build a DLL. 我使用Visual Studio构建DLL。 I followed the first steps of this instruction http://msdn.microsoft.com/de-de/library/ms235636.aspx : 我按照此说明的第一步http://msdn.microsoft.com/de-de/library/ms235636.aspx

To create a dynamic link library (DLL) project 创建动态链接库(DLL)项目

  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. 在“新建项目”对话框的左窗格中,展开“已安装”,“模板”,“ Visual C ++”,然后选择“ Win32”。

  3. In the center pane, select Win32 Console Application. 在中央窗格中,选择“ Win32控制台应用程序”。

  4. Specify a name for the solution — for example, MyDLL — in the Solution name box. 在“解决方案名称”框中指定解决方案的名称,例如MyDLL。 Choose the OK button. 选择确定按钮。

  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button. 在“ Win32应用程序向导”对话框的“概述”页面上,选择“下一步”按钮。

  6. On the Application Settings page, under Application type, select DLL. 在“应用程序设置”页上的“应用程序类型”下,选择“ 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: 之后,我将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()

This works fine for me. 这对我来说很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM