在 Erlang 中运行 C 代码块
如何从 Erlang 运行 C 代码块? (或者从 erlang 调用 C 函数?)
How to Run C Code Block from Erlang? ( Or Call a C function from erlang? )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是为了创建驱动程序。
首先,您需要创建 C/C++ 文件来执行此操作。
他们将需要包含
然后您需要设置驱动程序映射
注意:如果您尝试运行 C++ 代码而不是 C,您将需要
并且您将需要使用 (char *) 转换任何文字字符串
那么它很好定义一个包含端口信息的结构体
最后,您需要设置所有函数
您需要将此 C/C++ 代码编译为共享对象并将其与 erl 接口链接
现在从 erlang 您'我想做几件事:
您需要加载驱动程序
然后您将打开驱动程序的端口
最后您可以将数据发送到该端口
This is for creating a driver
Firstly you'll need to create the C/C++ files to do it.
They will need to include
Then you'll need to set up the driver mapping
Note: if you are trying to run C++ code instead of C you'll need
And you will need to cast any literal string with (char *)
Then it's good to define a struct that'll contain the port information
Lastly, you'll want to set up all the functions
You'll want to compile this C/C++ code into a shared object and link it with the erl interface
Now from erlang you'll want to do a couple things:
You'll need to load the driver
Then you'll open a port to the driver
And lastly you can sent data to the port
最新的方法将考虑 NIF http://www.erlang.org/doc/man/erl_nif .html(小心,它可能会使虚拟机崩溃)。常规方法涉及链接到驱动程序(谷歌搜索链接,因为反垃圾邮件保留它)
The newest approach would consider NIFs http://www.erlang.org/doc/man/erl_nif.html (be careful, it can crash VM). Regular way to do it involves linked in drivers (google up the link, because anti-spam holds it)