python如何调用C++外部库中的类?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python如何调用C++外部库中的类?相关的知识,希望对你有一定的参考价值。
如外部库
#include "FlyCapture2.h"
using namespace FlyCapture2;
using namespace std;
Camera *pCam;
我怎样在python中用pCam
//YourFile.cpp (compiled into a .dll or .so file)
#include
//For std::nothrow
//Either include a header defining your class, or define it here.
extern "C" //Tells the compile to use C-linkage for the next scope.
//Note: The interface this linkage region needs to use C only.
void * CreateInstanceOfClass( void )
// Note: Inside the function body, I can use C++.
return new(std::nothrow) MyClass;
//Thanks Chris.
void DeleteInstanceOfClass (void *ptr)
delete(std::nothrow) ptr;
int CallMemberTest(void *ptr)
// Note: A downside here is the lack of type safety.
// You could always internally(in the C++ library) save a reference to all
// pointers created of type MyClass and verify it is an element in that
//structure.
//
// Per comments with Andre, we should avoid throwing exceptions.
try
MyClass * ref = reinterpret_cast
(ptr);
return ref->Test();
catch(...)
return -1; //assuming -1 is an error condition.
//End C linkage scope.
第二步:
gcc -shared -o test.so test.cpp
#creates test.so in your current working directory.
第三步:
>>> from ctypes import cdll
>>> stdc=cdll.LoadLibrary("libc.so.6") # or similar to load c library
>>> stdcpp=cdll.LoadLibrary("libstdc++.so.6") # or similar to load c++ library
>>> myLib=cdll.LoadLibrary("/path/to/test.so")
>>> spam = myLib.CreateInstanceOfClass()
>>> spam
[outputs the pointer address of the element]
>>> value=CallMemberTest(spam)
[does whatever Test does to the spam reference of the object] 参考技术A 你好流程比较复杂,需要通过一个c的接口来做。下面是一个简单的例子,你也可以到booth去看看他们是怎么用的。
//YourFile.cpp (compiled into a .dll or .so file)
#include
//For std::nothrow
//Either include a header defining your class, or define it here.
extern "C" //Tells the compile to use C-linkage for the next scope.
//Note: The interface this linkage region needs to use C only.
void * CreateInstanceOfClass( void )
// Note: Inside the function body, I can use C++.
return new(std::nothrow) MyClass;
//Thanks Chris.
void DeleteInstanceOfClass (void *ptr)
delete(std::nothrow) ptr;
int CallMemberTest(void *ptr)
// Note: A downside here is the lack of type safety.
// You could always internally(in the C++ library) save a reference to all
// pointers created of type MyClass and verify it is an element in that
//structure.
//
// Per comments with Andre, we should avoid throwing exceptions.
try
MyClass * ref = reinterpret_cast
(ptr);
return ref->Test();
catch(...)
return -1; //assuming -1 is an error condition.
//End C linkage scope.
第二步:
gcc -shared -o test.so test.cpp
#creates test.so in your current working directory.
第三步:
>>> from ctypes import cdll
>>> stdc=cdll.LoadLibrary("libc.so.6") # or similar to load c library
>>> stdcpp=cdll.LoadLibrary("libstdc++.so.6") # or similar to load c++ library
>>> myLib=cdll.LoadLibrary("/path/to/test.so")
>>> spam = myLib.CreateInstanceOfClass()
>>> spam
[outputs the pointer address of the element]
>>> value=CallMemberTest(spam)
[does whatever Test does to the spam reference of the object]本回答被提问者和网友采纳
以上是关于python如何调用C++外部库中的类?的主要内容,如果未能解决你的问题,请参考以下文章