[英]Including amqp_client.hrl for RabbiqMQ in Erlang Library
I'm trying to figure out how to include amqp_client.hrl in a library I'm writing. 我试图弄清楚如何在我正在编写的库中包含amqp_client.hrl。
I am able to use it in a script based on the following example: https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/erlang/send.erl 我可以在基于以下示例的脚本中使用它: https : //github.com/rabbitmq/rabbitmq-tutorials/blob/master/erlang/send.erl
When I try to use it in a non script setting: 当我尝试在非脚本设置中使用它时:
-module(rabbitMQHandler).
-compile(export_all).
-include("amqp_client/include/amqp_client.hrl").
test() ->
{ok, Connection} =
amqp_connection:start(#amqp_params_network{host = "localhost"}),
{ok, Channel} = amqp_connection:open_channel(Connection),
ok = amqp_channel:close(Channel),
ok = amqp_connection:close(Connection),
ok.
I can compile rabbitMQHandler.erl but when I execute rabbitMQHandler:test().
我可以编译rabbitMQHandler.erl,但是当我执行rabbitMQHandler:test().
I get the following error ** exception error: undefined function amqp_connection:start/1
我收到以下错误** exception error: undefined function amqp_connection:start/1
What is the appropriate way to include amqp_client.hrl
in a library? 在库中包含amqp_client.hrl
的适当方法是什么?
I tried -include_lib("amqp_client/include/amqp_client.hrl").
我尝试了-include_lib("amqp_client/include/amqp_client.hrl").
but that made no difference. 但这没什么区别。
I tried including %%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
我尝试包括%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
%%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
but that also made no difference. %%! -pz ./amqp_client ./rabbit_common ./amqp_client/ebin ./rabbit_common/ebin
但这也没有区别。
EDIT: 编辑:
For those of you using the Erlang repl in emacs add the following into your .emacs
file in order to pass the flags to your repl: 对于那些在emacs中使用Erlang repl的用户,请将以下内容添加到您的.emacs
文件中,以便将标志传递给您的repl:
(defun erl-shell (flags)
"Start an erlang shell with flags"
(interactive (list (read-string "Flags: ")))
(set 'inferior-erlang-machine-options (split-string flags))
(erlang-shell))
The with Mx erl-shell
you can pass the flags to erl. 使用Mx erl-shell
可以将标志传递给erl。
The snippet was taken from http://erlang.org/pipermail/erlang-questions/2007-January/024966.html . 该摘录摘自http://erlang.org/pipermail/erlang-questions/2007-January/024966.html 。
The undefined function amqp_connection:start/1
message usually means that the amqp_connection
module is not in the search path. undefined function amqp_connection:start/1
消息通常表示amqp_connection
模块不在搜索路径中。
You need to start Erlang with the same -pz
flag you used in your escript. 您需要使用在脚本中使用的相同的-pz
标志来启动Erlang。 For example: 例如:
$ erl -pz ./amqp_client ./rabbit_common ./amqp_client/ebin
You can double-check it works by querying module informations: 您可以通过查询模块信息来再次检查它是否有效:
1> amqp_connection:module_info().
[{module,amqp_connection},
{exports,[{start,1},
{open_channel,1},
{open_channel,2},
...
Then you can run your code as usual. 然后,您可以照常运行代码。
About -include
vs. -include_lib
, the latter is the appropriate one in your case. 关于-include
与-include_lib
,后者是适合您的情况。 That's the preferred way to include headers from external applications (either OTP or third-party). 这是包含来自外部应用程序(OTP或第三方)标头的首选方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.