2

尝试在 OSX 下在 Erlang 中运行 RabbitMQ 教程示例,但失败并显示以下消息:

./send.erl:20: can't find include lib "rabbit_common/include/rabbit.hrl"
./send.erl:21: can't find include lib "rabbit_common/include/rabbit_framing.hrl"
escript: There were compilation errors.

amqp_example.erl:

-module(amqp_example).

-include("amqp_client.hrl").

-compile([export_all]).

test() ->
    %% Start a network connection
    {ok, Connection} = amqp_connection:start(#amqp_params_network{}),
    %% Open a channel on the connection
    {ok, Channel} = amqp_connection:open_channel(Connection),

    %% Declare a queue
    #'queue.declare_ok'{queue = Q}
        = amqp_channel:call(Channel, #'queue.declare'{}),
    %% Publish a message
    Payload = <<"foobar">>,
    Publish = #'basic.publish'{exchange = <<>>, routing_key = Q},
    amqp_channel:cast(Channel, Publish, #amqp_msg{payload = Payload}),

    %% Get the message back from the queue
    Get = #'basic.get'{queue = Q},
    {#'basic.get_ok'{delivery_tag = Tag}, Content}
         = amqp_channel:call(Channel, Get),

    %% Do something with the message payload
    %% (some work here)

    %% Ack the message
    amqp_channel:cast(Channel, #'basic.ack'{delivery_tag = Tag}),

    %% Close the channel
    amqp_channel:close(Channel),
    %% Close the connection
    amqp_connection:close(Connection),

    ok.

请帮我解决这个问题。感谢!!!

4

1 回答 1

3

Erlang 有宏include_lib,它可以在路径中搜索库并且很方便,因为您不必指定库的版本 - 它会自动使用最新版本。所以,而不是

-include("rabbit_common-3.3.5/include/rabbit.hrl").

你可以写:

-include_lib("rabbit_common/include/rabbit.hrl").

因此,在您的情况下,您必须确保该文件rabbit_common-[version]/include/rabbit.hrl位于ERL_LIBS路径中。在本教程中,您正在使用,他们希望您从这里下载这些文件并像这样解压缩它们:

unzip -d deps deps/amqp_client.ez
unzip -d deps deps/rabbit_common.ez

这些解压命令在 OS X 上不起作用,因为 unzip 仅适用于 .zip 文件。所以这可能是你的问题。尝试使用另一个应用程序解压缩它们并仔细检查文件是否存在。不要忘记ERL_LIBS=deps在编译和运行示例之前添加:

ERL_LIBS=deps erlc -o ebin amqp_example.erl
ERL_LIBS=deps erl -pa ebin
于 2014-09-15T08:43:25.337 回答