This document explains how to use local and system includes in Fun source files and how `FUN_LIB_DIR` controls where system includes are resolved from. It also covers namespaced includes with `as`.
## Quick summary
- Local includes: `#include "relative/path/to/file.fun"`
- Resolved relative to the current working directory (`$PWD`) where you run `fun`.
- System includes: `#include <path/inside/stdlib.fun>`
- Resolved using:
1) `FUN_LIB_DIR` (if set)
2) `DEFAULT_LIB_DIR` (compile-time default; typically `/usr/share/fun/lib/` after install)
3) Fallback to `lib/` under the current working directory (developer convenience)
- Optional namespacing: `#include <...> as ns` or `#include "..." as ns`
## Local includes (quoted)
Use double quotes to include files relative to the directory you execute `fun` from.
- You usually do not need `FUN_LIB_DIR` because `DEFAULT_LIB_DIR` points to the installed stdlib (e.g., `/usr/share/fun/lib/` on Linux). You can still set `FUN_LIB_DIR` to test custom or alternate library trees.
- Interaction with `DEFAULT_LIB_DIR`:
- If `FUN_LIB_DIR` is not set or is empty, the interpreter tries `DEFAULT_LIB_DIR` (a compile-time value). Build systems may set this via `-DDEFAULT_LIB_DIR=...`. The project’s CMake sets sensible OS-specific defaults.
- Developer fallback:
- If neither `FUN_LIB_DIR` nor `DEFAULT_LIB_DIR` resolves the include, the interpreter finally tries `lib/<path>` relative to `$PWD`.
Tips:
- To verify which stdlib is being used, temporarily include a file you control and log from it.
- Mind your current directory: running `fun` from a different folder changes how quoted includes resolve and can change the `lib/` fallback for angle-bracket includes.
## Common patterns and best practices
- Prefer quoted includes for project-internal modules: `#include "src/mods/foo.fun"`
- Prefer angle brackets for stdlib and external libs: `#include <utils/math.fun>`
- Use `as` for clarity and to avoid name clashes: `#include <utils/math.fun> as math`
- When developing against the repo without installing, export `FUN_LIB_DIR=$(pwd)/lib` before running examples or your app.
- Keep paths portable: avoid OS-specific separators inside include paths; the resolver handles path joining.
## Troubleshooting include errors
- Error: `Include error: cannot read '...'`
- Check if the path exists in the intended location:
- For `"..."`: is the path correct relative to your current working directory?
- For `<...>`: does the file exist under `FUN_LIB_DIR/<...>` or `DEFAULT_LIB_DIR/<...>` or `./lib/<...>`?
- Ensure `FUN_LIB_DIR` is exported in the same shell session where you run `fun`.
- On Windows, verify you used the correct syntax for setting environment variables in CMD vs. PowerShell.