Erlang - undefined function

2019-06-28 05:35发布

问题:

I'm trying to execute a very simple Erlang code, and it's not working. I've tryied executed some hello worlds without problem, but not mine own code.

-module(server).
%% Exported Functions
-export([start/0, process_requests/1]).
%% API Functions
start() ->
    ServerPid = spawn(server, process_requests, [[]]),
    register(myserver, ServerPid).
process_requests(Clients) ->
    receive
         {client_join_req, Name, From} ->
         NewClients = [From|Clients],  %% TODO: COMPLETE
            broadcast(NewClients, {join, Name}),
            process_requests(NewClients);  %% TODO: COMPLETE
        {client_leave_req, Name, From} ->
            NewClients = lists:delete(From, Clients),  %% TODO: COMPLETE
            broadcast(Clients, {leave, Name}),  %% TODO: COMPLETE
            process_requests(NewClients);  %% TODO: COMPLETE
        {send, Name, Text} ->
            broadcast(Clients, {message, Name, Text}),  %% TODO: COMPLETE
            process_requests(Clients)
end.
%% Local Functions
broadcast(PeerList, Message) ->
    Fun = fun(Peer) -> Peer ! Message end,
    lists:map(Fun, PeerList). 

Compile result:

5> c(server).
    {ok,server}
6> server:start().       
** exception error: undefined function server:start/0

回答1:

You compile you code with c/1, but you forgot to load it to VM with l/1. While VM does loads modules new automatically (modules not yet loaded to VM), it doesn't reload them each time you compile to new beam.

If you do it a lot in development you might want to look into tools like sync.



回答2:

Try to check with pwd(). whether you are in the directory where your listed server code is. Seems to be a path issue. It also can happen that in your code:get_path() there is a directory where another server.beam is sitting that has not got a start function.