我正在尝试使用 Python Ctypes 来连接已发布的(闭源)C++ 库。我(尝试)编写了一个基本的 C 风格函数包装器来构造 C++ vector 风格对象并调用 C++ 例程。我还(尝试)编写了一个基本的 python 脚本来加载共享库。除了调用 C++ 例程的行外,一切正常:
*** glibc detected *** python: free(): invalid next size (fast): 0x0000000001e73c00 ***
这是文件,不幸的是我不能分享标题,但如果需要我可以写一些类似的东西......
gaumixmod.cpp:
#include "nr3.h"
#include "cholesky.h"
#include "gaumixmod.h"
extern "C" {
void cGaumixmod(double* D, int Dm, int Dn, double* M, int Mm, int Mn) {
MatDoub ddata(Dm,Dn,*D); // construct Matrix (vector) type
MatDoub mmeans(Mm,Mn,*M); // construct Matrix (vector) type
//XXX test numpy array is coming through as C array and we can rw, checks OK
int i;
// for(i=0;i<Dn*Dm;++i) {
// printf("Address %x : ",(D+i));
// printf("was %f \t" , D[i]);
// D[i]+=1.0;
// printf("now: %f \n" , D[i]);
// }
// check that array D was copied to matrix ddata, and we can r/w
for(i=0;i<Dm*Dn;++i) {
printf("iter %d Address %x : ",i,ddata[i/Dm][i%Dm]);
printf("was %f \t" , ddata[i/Dm][i%Dm]);
ddata[i/Dm][i%Dm]+=1.0;
printf("now: %f \n" ,ddata[i/Dm][i%Dm]);
}
Gaumixmod::Gaumixmod(ddata,mmeans);
//return data from vector to ctypes array C so we can check data returns to python
//via numpy array, checks ok
for(i=0;i<Dm*Dn;++i) {
D[i] = ddata[i/Dm][i%Dm];
}
}
}
goumixmod.py:
import platform,ctypes
import numpy as np
# ------------------------------------------------------------------------
# define correct library from platfrom, assuming 64bit for linux machines
# ------------------------------------------------------------------------
if platform.system()=='Microsoft':
raise Exception('MS not supported.')
elif platform.system()=='Darwin':
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
elif platform.system()=='Linux':
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
else:
#hope for the best
libgaumixmod = ctypes.cdll.LoadLibrary("./gaumixmod.so")
# --------------------------------------------------
# define SafeCall
#---------------------------------------------------
def SafeCall(ret):
"""pass, code l8r""
print ret
#---------------------------------------------------
# define arg types and res types of function
# -----------------------------------------------------------------------
_gaumixmod = libgaumixmod.cGaumixmod
_gaumixmod.restype = ctypes.c_int
_gaumixmod.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS'),
ctypes.c_int,
ctypes.c_int,
np.ctypeslib.ndpointer(dtype=np.float64,flags='C_CONTIGUOUS'),
ctypes.c_int,
ctypes.c_int]
def gaumixmod(D,K):
"""Python binding for C++ guassian mixure model code."""
ret = _gaumixmod(D,D.shape[0],D.shape[1],K,K.shape[0],K.shape[1])
SafeCall(ret)
return D,K
D = np.ones((100,100)).astype(np.float64)
K = np.ones((4,1)).astype(np.float64)
print gaumixmod(D,K)
我用以下代码编译了这个爵士乐:
g++ -fPIC -c gaumixmod.cpp;
g++ -shared -o gaumixmod.so gaumixmod.o
然后运行
python gaumixmod.py
我的研究表明这个错误类似于 segFault,其中 python 试图访问其范围之外的内存...这是我不理解的部分,因为注释掉了 C++ 行 Gaumixmod::Gaumixmod() ,一切正常,并且该例程应该在 cGaumixmod() 函数中实例化的 vector 上运行,而不是 python numpy 数组。我对 C++ 真的很陌生,虽然我已经多次使用 C 类型来创建 C 库。我希望有一些 C++、python 和 ctypes 经验的人可以在这里提供一些见解/指导。
谢谢!
最佳答案
有一个用于与 C 接口(interface)的新库,您可能会看它:
关于c++ - Python Ctypes 崩溃调用 C++ 函数的 C 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11056978/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www