[erlang-questions] include vs include_lib

Kevin A. Smith kevin@REDACTED
Wed Jul 22 17:34:03 CEST 2009


For me, -include() and -include_lib() serve two different use cases.

Use Case 1: Your project has -define'd constants, parse transforms,  
etc which are required for others to use your code. Examples include   
qlc or webmachine.

Solution: Put your .hrl files in project_name/include. As long as your  
project is on the code path then clients of your code can use - 
include_lib() to load the .hrl files.

Use Case 2: You've centralized common constants, etc in a .hrl file.  
These constants are intended to be used internally by your project   
code. This means at no time will any client need access to them.

Solution: Put your .hrl files in project_name/src. Your code can use - 
include() to pull in the .hrl files.

Keep in mind that both include directives are compile-time constructs.  
You can put all of your header files in src, compile your code using - 
include(), and then selectively "export" the header files you want to  
publish by copying them into include. I do this a lot since it makes  
my compile process easier -- no code path to futz with -- and it makes  
my client code cleaner -- they can use -include_lib to find my public  
header files.

--Kevin
On Jul 22, 2009, at 9:20 AM, Peter Mechlenborg wrote:

> Hi
> I have found myself starting to always use '-include_lib()' instead  
> of just
> '-include()', because it makes compilation easier - you don't need to
> specify include directories, and files become "self-contained"  
> regarding
> compilation.
>
> Why do people use '-include()' instead of '-include_lib()'?  Am I  
> missing
> something obvious here?
>
> Have fun,
>
>  --  Peter



More information about the erlang-questions mailing list