My test project is structured this way:
./Emakefile:
{"source/*", [debug_info, {outdir, "binary"}]}.
./source/test.erl:
-module(test).
-behaviour(test_behaviour).
./source/test_behaviour.erl:
-module(test_behaviour).
-export([behaviour_info/1]).
behaviour_info(callbacks) -> [];
behaviour_info(_) -> undefined.
When I use the command erl -make in the project directory (.), I get the following output:
Recompile: source/test_behaviour
Recompile: source/test
source/test.erl:2: Warning: behaviour test_behaviour undefined
Why does erl print this warning ? It compiled test_behaviour.erl before test.erl, so I guess it should be able to find test_behaviour.beam in the binary folder.
Behaviors are resolved at the compile time. Hence the Erlang compiler should find the behavior beam file and call the behavior module's behaviour_info/1 function.
Here test_behaviour.beam is not in the code path of the compiler. You can run by calling
erl -pa ebin -make
This solved the problem for me. I tried specifying in Emakefile but with no luck. Could not find the documentation also.
Also found that oder of -pa ebin has to be before -make (not sure why though).
Related
IDE: IntelliJ IDEA 2022.2.2
I set a breakpoint in my Erlang code which has eunit testcases.
In order to debug it, I clicked on "Run --> Debug". I got an error "Test suite was cancelled". What could be the reason?
"C:\Program Files\Erlang OTP\bin\erl.exe" -pa C:\Users\bhu\AppData\Local\Temp\intellij_erlang_debugger_7789 -pa C:/Users/bhu/AppData/Local/Temp/eunit_teamcity9 -pa F:/1TB/P/workspace-IntelliJ-Erlang1/out/test/workspace-IntelliJ-Erlang1 -pa F:/1TB/P/workspace-IntelliJ-Erlang1 -run debugnode main 50067 -noshell -s init stop
Testing started at 20:59 ...
** Invalid beam file or no abstract code: "F:\\1TB\\P\\workspace-IntelliJ-Erlang1\\src\\my_sort.erl"
Test suite was cancelled. Reason: {abort,{module_not_found,my_sort}}
The error means that the module my_sort was not loaded into the VM. You can find the function here. I think this is because the search path to your module my_sort is not added to the code path using the flag -pa /mypath/my_sort.erl.
Could you try to add to the code path using -pa ..?
erlc -I <abspath-to-include-dir> <module>.erl on the command line correctly compiles <module>
But in the Erlang shell (erl), the following produces errors with "cannot find include file":
c(<module>, [{i, <abspath-to-include-dir>}]).
Why? Shouldn't the behavior of these two ways of compiling files be the same?
Try writing the path as a list of directories, as in {i, [".../here/", ".../there/"]}, even if it's just a single directory.
I have the following Erlang code:
#!/usr/bin/env escript
%%! -pz ../deps/amqp_client ../deps/rabbit_common ../deps/amqp_client/ebin ../deps/rabbit_common/ebin
% RMQ module
-module(rmq).
-export([main/1, send/1, validate/0, test/0]).
-include_lib("../deps/amqp_client/include/amqp_client.hrl").
main(_) ->
%send(<<"test_esio">>),
%validate(),
Pid = spawn(rmq, test, []),
% Pid = spawn(fun() -> test() end), <= I've tried this way too
Pid ! s.
test() ->
receive
s ->
io:format("BAR ~n"),
send(<<"esio">>),
test();
get ->
validate(),
test();
_ ->
io:format("FOO"),
test()
end.
I run this with:
excript rmq.erl
This code doesn't work. Looks like spawn doesn't work.
Rest of my code works, function send and validate works correctly if I run it from main (I've commented its). What I'm doing wrong?
Sorry, maybe it's a dumb question but I'm a beginner with erlang. I've tried search answer in internet and books and I failed...
The problem is not actually in spawn, but in module/escript confusion.
In few words, escript file are not really modules, not from point of Erlang VM, even if you use -module() directive. They are interpreted, and not compiled at all, and definitely they can not be called by module like "rmq:test()", or in you case trough dynamic module call by spawn.
Easiest solution is separate script from actual modules. In your rmq.es you would just start some proper module:
#!/usr/bin/env escript
%%! -pz ../deps/amqp_client ../deps/rabbit_common ../deps/amqp_client/ebin ../deps/rabbit_common/ebin
main(_) ->
rmq:start().
And there in module rmq.erl:
-module(rmq).
-export([start/0, send/1, validate/0, test/0]).
-include_lib("../deps/amqp_client/include/amqp_client.hrl").
start() ->
Pid = spawn(rmq, test, []),
%% Pid = spawn(?MODULE, test, []), %% works with macro too
%% Pid = spawn(fun() -> test() end), <= I've tried this way too
Pid ! s.
test() ->
receive
s ->
io:format("BAR ~n"),
send(<<"esio">>),
test();
get ->
validate(),
test();
_ ->
io:format("FOO"),
test()
end.
Or you could just start this module without escript, with -run flag like this
erl -pz deps/*/ebin -run rmq start
EDIT regarding compilation problems
You compile your modules with erlc command. To just compile use erlc rmq.erl, which will produce rmq.beam file in current directory. But convention is to keep all your source files in src directory, all compiled files in ebin direcory, and things like run-scripts could be placed in the top directory. Something like that:
project
|-- ebin
| |-- rmq.beam
|
|-- src
| |-- rmq.erl
|
|-- rmq.es
Assuming that you run all your shell commands form project directory, to compile all file from src and place .beam binaries in ebin use erlc src/rmq.erl -o ebin, or erlc src/* -o ebin In documentation you can find you explanation of -o flag"
-o directory
The directory where the compiler should place the output files. If not specified, output files will be placed in the current working directory.
Then, after compilation you can run your code, either with erl Erlang VM or using escript (which kind-off uses erl.
erl to runs code from compiled modules, and to do that he needs to be able to locate those compiled *.ebin binaries. For this he uses code path, which is the list of directors in which he will search for those files. This list automatically consist standard library directories, and of course you can add to it directories with your own code with use of -pa flag.
-pa Dir1 Dir2 ...
Adds the specified directories to the beginning of the code path, similar to code:add_pathsa/1. See code(3). As an alternative to -pa, if several directories are to be prepended to the code and the directories have a common parent directory, that parent directory could be specified in the ERL_LIBS environment variable. See code(3).
In your case it would be erl -pa ebin, or to include binaries of all deps you can use erl -pa ebin -pa deps/*/ebin.
Exactly same options are used in second line of your escript. With exception of * character, which will not be expand like it would be in the shell. In escript you have to provide paths to each dependency separately. But the idea of including -pa ebin stays exactly the same.
To automate and standardize this process tools like rebar and erlang.mk where created (I would recommend the later). Using those should help you a little with your workflow.
I'm trying to use mochiweb in a module, but can't find a way to make the module 'aware' of mochiweb.
mochiweb_html:parse("<XML>").
This works fine in erl, but I keep getting undefined function when I use it inside a module.
if i understand your question, it sounds like a code path problem. as long as mochiweb_html.beam is in your code path, you can use any exported function.
running it from the erlang shell (erl) may have worked because the beam file was in your cwd.
for example:
# cd /home/blah/src/mochiweb/ebin; erl
1> code:where_is_file("mochiweb_html.beam").
"./mochiweb_html.beam"
# cd /tmp; erl
1> code:where_is_file("mochiweb_html.beam").
non_existing
make sure that the directory containing mochiweb_html.beam is in your code path.
to add it to your code path, use command lind args (-pa, -pz):
# erl -pa /home/blah/src/mochiweb/ebin
1> code:where_is_file("mochiweb_html.beam").
"/home/blah/src/mochiweb/ebin/mochiweb_html.beam"
or using the code module (code:add_patha, code:add_pathz):
# erl
1> code:where_is_file("mochiweb_html.beam").
non_existing
2> code:add_patha("/home/blah/src/mochiweb/ebin/").
true
3> code:where_is_file("mochiweb_html.beam").
"/home/blah/src/mochiweb/ebin/mochiweb_html.beam"
I have an Emakefile that looks like:
%% --
%%
%% --
{'/Users/user/projects/custom_test/trunk/*',
[debug_info,
{outdir, "/Users/user/projects/custom_test/trunk/ebin"},
{i, "/Users/user/projects/custom_test/trunk/include/."}
]
}.
What is an explanation in layman's terms for what each item does in the list?
How do I run the emakefile so that I am able to compile it?
After compilation, how do I run that generated BEAM file?
1/ {"source files globbed", Options}
Here the options are :
debug_info add debug info for the debugger
{outdir, "/Users/user/projects/custom_test/trunk/ebin"} where should the output be written (the .beam files)
{i, "/Users/user/projects/custom_test/trunk/include/."} where to find the .hrl header files.
2/ erl -make
3/ erl -pa /Users/user/projects/custom_test/trunk/ebin starts a shell.
Find the module serving as an entry point in your application and call the functions :
module:start().
You can also run the code non interactively :
erl -noinput -noshell -pa /Users/user/projects/custom_test/trunk/ebin -s module start
For the Emakefile synax visit the man page
In the directory where the Emakefile is run erl -make to compile using the Emakefile
Simplest way to run would be to simply start an erlang shell in the same directory as the beam files with the command erl. Then run the code with module_name:function_name(). (including the dot).