RNikoopour
RNikoopour

Reputation: 533

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.

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

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(). I get the following error ** exception error: undefined function amqp_connection:start/1

What is the appropriate way to include amqp_client.hrl in a library?

I tried -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 but that also made no difference.

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:

(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 M-x erl-shell you can pass the flags to erl.

The snippet was taken from http://erlang.org/pipermail/erlang-questions/2007-January/024966.html.

Upvotes: 1

Views: 355

Answers (1)

The undefined function amqp_connection:start/1 message usually means that the amqp_connection module is not in the search path.

You need to start Erlang with the same -pz flag you used in your escript. 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. That's the preferred way to include headers from external applications (either OTP or third-party).

Upvotes: 3

Related Questions