0

我将如何能够做到以下几点:

system.c

#include<stdio.h>

int main()

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

        return(0);
}

来自使用 erlang 客户端rabbitmq-tutorials/erlang/的 RabbitMQ 教程

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.

然后运行:

gcc system.c -o system

noob$./system

输出:

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_client.hrl

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

然后运行noob$./system

和繁荣:

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

因此,escript 在编译时似乎存在 PATHS 问题,并且它在 C 的 system() 调用中被调用。

知道我将如何实现这一目标吗?

TIA

4

1 回答 1

0

感谢这个问题

我能够动态地添加额外的 PATH 目录,以便 escript 找到运行脚本所需的 erlang 客户端代码。

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"),

我仍然不太明白为什么从 shell 中使用 -pz 相对路径选项调用 escript 起作用并且在 C 的 system() 中不起作用的原因。

于 2012-12-11T19:25:08.253 回答