Erlang: How to include libraries

℡╲_俬逩灬. 提交于 2019-12-22 10:04:08

问题


I'm writing a simple Erlang program that requests an URL and parses the response as JSON.

To do that, I need to use a Library called Jiffy. I downloaded and compiled it, and now i have a .beam file along with a .app file. My question is: How do I use it? How do I include this library in my program?. I cannot understand why I can't find an answer on the web for something that must be very crucial.

Erlang has an include syntax, but receives a .hrl file.

Thanks!


回答1:


You don't need to include the file in your project. In Erlang, it is at run time that the code will try to find any function. So the module you are using must be in the search path of the VM which run your code at the point you need it, that's all.

For this you can add files to your path when you start erlang: erl -pa your/path/to/beam (it exists also -pz see erlang doc)

Note that it is also possible to modify the path from the application itself using code:add_path(Dir).

You should have a look to the OTP way to build applications in erlang documentation or Learn You Some Erlang, and also look at Rebar a tool that helps you to manage erlang application (for example starting with rebar or rebar wiki)




回答2:


To add to Pascal's answer, yes Erlang will search for your files at runtime and you can add extra paths as command line arguments.

However, when you build a project of a scale that you are including other libraries, you should be building an Erlang application. This normally entails using rebar.

When using rebar, your app should have a deps/ directory. To include jiffy in your project, it is easiest to simply clone the repo into deps/jiffy. That is all that needs to be done for you to do something like jiffy:decode(Data) in your project.

Additionally, you can specify additional include files in your rebar.config file by adding extra lines {erl_opts, [{i, "./Some/path/to/file"}]}.. rebar will then look for file.so using that path.



来源:https://stackoverflow.com/questions/26662424/erlang-how-to-include-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!