现在,我已经将python称为C ++。使用ctype在两者之间进行连接。而且我在运行时遇到有关核心转储的问题。
我有一个名为“ libfst.so”的库 这是我的代码。 NGramFST.h
#include "NGramFST.h"
NGramFST* NGramFST::m_Instace = NULL;
extern "C" {
double FST_getProbability(std::string word, std::string context){
return NGramFST::getInstance()->getProbabilityOfWord(word, context);
}
}
NGramFST.cpp
from ctypes import cdll
lib = cdll.LoadLibrary('./libfst.so')
#-------------------------main code------------------------
class FST(object):
def __init__(self):
print 'Initializing'
def getProbabilityOfWord(self, word, context):
lib.FST_getProbability(word, context)
fst = FST()
print fst.getProbabilityOfWord(c_wchar_p('jack london'), c_wchar_p('my name is'))
这是我的python代码。
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
这是错误
Public Sub TransformToColumnsByAcct()
Dim rngSrcData As Range, i As Long, objDict As Scripting.Dictionary, strKey As String, arrRows As Variant
Dim lngHeaderStartCol As Long, x As Long, lngSrcRow As Long, lngWriteRow As Long, lngMaxUbound As Long
Set rngSrcData = Selection
Set objDict = New Scripting.Dictionary
With rngSrcData
' Get all of the unique accounts, this will also determine for us the amount of columns we need to provide for.
' Start from the 2nd row because the 1st contains the header.
For i = 2 To .Rows.Count
strKey = .Cells(i, 1)
If Not objDict.Exists(strKey) Then
objDict.Add strKey, Array(i)
Else
arrRows = objDict.Item(strKey)
ReDim Preserve arrRows(UBound(arrRows) + 1)
arrRows(UBound(arrRows)) = i
objDict.Item(strKey) = arrRows
If UBound(arrRows) > lngMaxUbound Then
lngMaxUbound = UBound(arrRows)
End If
End If
Next
' Clear all of the cells in the destination worksheet.
shTransformed.Cells.Clear
' Add the header for the key field.
shTransformed.Cells(1, 1) = .Cells(1, 1)
lngHeaderStartCol = 2
' Now get all of the column headers excluding the first as this contains the key and write them to the
' transformed worksheet. Dynamically increment the 2nd header by 1 each time.
For i = 1 To lngMaxUbound + 1
' Determine the start column for the header to be copied to factoring in the first field.
If i > 1 Then
lngHeaderStartCol = lngHeaderStartCol + .Columns.Count - 1
End If
.Range(.Cells(1, 2).Address & ":" & .Cells(1, .Columns.Count).Address).Copy shTransformed.Cells(1, lngHeaderStartCol)
' Incremement the header text by 1 and put an underscore.
shTransformed.Cells(1, lngHeaderStartCol) = shTransformed.Cells(1, lngHeaderStartCol) & "_" & i
Next
' Now write out all of the unique keys to the transformed sheet along with the data.
For i = 0 To objDict.Count - 1
strKey = objDict.Keys(i)
arrRows = objDict.Item(strKey)
lngWriteRow = i + 2
' Write the key to the first column.
shTransformed.Cells(lngWriteRow, 1) = strKey
lngHeaderStartCol = 2
' Now process each row of data for the unique key.
For x = 0 To UBound(arrRows)
lngSrcRow = arrRows(x)
If x > 0 Then
lngHeaderStartCol = lngHeaderStartCol + .Columns.Count - 1
End If
' Copy the data for the given row to the transformed sheet.
.Range(.Cells(lngSrcRow, 2).Address & ":" & .Cells(lngSrcRow, .Columns.Count).Address).Copy shTransformed.Cells(lngWriteRow, lngHeaderStartCol)
Next
Next
End With
End Sub
我再次进行了审查,但我无法发现问题所在。
答案 0 :(得分:1)
ctypes
不了解C ++类型(未称为c++types
)。它无法处理std::string
。仍然不知道您的函数是否期望std::string
自变量。
为了使用ctypes
,您的库需要一个C兼容的接口。 extern "C"
是必要的,但还不够。这些函数实际上必须可以从C调用。
更好的方法是使用现代的C ++ / Python绑定库,例如pybind11。
答案 1 :(得分:0)
当我在下面更改python代码时就可以使用
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
print fst.getProbabilityOfWord(b_string1, b_string2)
和c ++代码更改以下参数的类型
FST_getProbability(const char* word, const char* context)