【问题标题】:RabbitMQ erlang client, escript from within C system() callRabbitMQ erlang 客户端,来自 C system() 调用中的 escript
【发布时间】:2012-11-29 09:14:15
【问题描述】:

我怎样才能做到以下几点:

system.c

#include<stdio.h>

int main()

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

        return(0);
}

来自 RabbitMQ 教程使用 erlang 客户端 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.

然后运行:

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

【问题讨论】:

    标签: erlang rabbitmq


    【解决方案1】:

    感谢question

    我能够动态添加额外的 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() 中不起作用的原因。

    【讨论】:

      猜你喜欢
      • 2017-06-20
      • 2015-12-05
      • 2012-06-03
      • 2015-11-17
      • 2013-04-16
      • 2014-04-05
      • 2017-01-13
      • 2013-10-24
      • 1970-01-01
      相关资源
      最近更新 更多