0

How would I be able to do as follows:

system.c

#include<stdio.h>

int main()

{
        system("/home/noob/send.erl");

        return(0);
}

From the RabbitMQ tutorial using erlang client rabbitmq-tutorials/erlang/

send.erl

#!/usr/bin/env escript
%%! -pz ./deps/amqp_client ./deps/rabbit_common ./deps/amqp_client/ebin ./deps/rabbit_common/ebin

-include_lib("deps/amqp_client/include/amqp_client.hrl").

main(_) ->
    {ok, Connection} = 
    amqp_connection:start(#amqp_params_network{host = "localhost"}),
    {ok, Channel} = amqp_connection:open_channel(Connection),

    amqp_channel:call(Channel, #'queue.declare'{queue = <<"hello">>}),
    amqp_channel:cast(Channel, 
             #'basic.publish'{
               exchange = <<"">>,
               routing_key = <<"hello">>}, 
              #amqp_msg{payload = <<"Hello World!">>}),
    io:format("[x] Sent 'Hello World!'~n"),
    ok = amqp_channel:close(Channel),
    ok = amqp_connection:close(Connection),
    ok.

Then run:

gcc system.c -o system

noob$./system

Outputs:

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.

So I do this on amqp_client.hrl

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

And then run noob$./system

and Boom:

escript: exception error: undefined function amqp_connection:start/1
  in function  erl_eval:do_apply/6 (erl_eval.erl, line 572)
  in call from erl_eval:expr/5 (erl_eval.erl, line 367)
  in call from escript:eval_exprs/5 (escript.erl, line 836)
  in call from erl_eval:local_func/5 (erl_eval.erl, line 470)
  in call from escript:interpret/4 (escript.erl, line 754)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_it/1

So it seems that escript has issues with PATHS when compiling and its called fro within C's system() call.

Any idea how would I be able to achieve this?

TIA

lfurrea
  • 176
  • 1
  • 10

1 Answers1

0

Thanks to this question

I was able to dinamically add the extra PATH directories so that escript would find the erlang client code required to run the script.

true = code:add_pathz(filename:dirname(escript:script_name()) ++ "/deps/amqp_client/ebin"),
true = code:add_pathz(filename:dirname(escript:script_name()) ++ "/deps/rabbit_common/ebin"),

I still don't quite understand the reason why calling escript with the -pz relative paths option from within the shell workd and does not work from within C's system().

Community
  • 1
  • 1
lfurrea
  • 176
  • 1
  • 10