erlangrebarrebar3relx

how to include .hrl files across multiple modules rebar3


I have several modules's directories. For each module, I have include (containing *.hrl files) and src (containing *.erl files) folder separeated. How can I share *.hrl file from a module to another without duplicating them?

With rebar, I added {erl_opts, [{i, "folderContainsIncludeFile"}]} and it worked.

But with rebar3, the compilation is failed by saying can't find include file "include/xx.hrl"


Solution

  • I take it then you don't have an umbrella project, you just have multiple directories at the same level, each one with its own project managed with rebar3. Something like:

    root_folder
     |- project1
     |  |- src
     |  |- include
     |  |  `- one.hrl
     |  `- rebar.config
     |- project2
     |  |- src
     |  |  `- two.erl
     |  |- include
     |  `- rebar.config
     …
    

    And you want to include one.hrl into two.erl.

    If that's the case, you should consider one of these alternatives:

    A. Moving to an umbrella project structure, like…

    root_folder
     |- rebar.config <<<<<<<< notice this file is here now
     `- apps
        |- project1
        |  |- src
        |  `- include
        |     `- one.hrl
        |- project2
        |  |- src
        |  |  `- two.erl
        |  `- include
        …
    

    B. Using individual repos for each project and configuring them as dependencies from each other. The structure is like the one you currently have, but now you have to add deps to rebar.config's. For instance, in our example, you should add the following line to project2's rebar.config:

    {deps, [project1]}.
    

    (if you manage to publish project1 in hex.pm)

    C. Properly setting $ERL_LIBS so that it includes the paths where your apps are built with rebar3. Something like…

    ERL_LIBS=$ERL_LIBS:/path/to/root_folder/project1/_build/lib:/path/to/root_folder/project2/_build/lib:…
    

    Hope this helps :)