On this page

6.1 – Loading the Libraries in C code

A C host program must explicitly load the standard libraries into a state, if it wants its scripts to use them. For that, the host program can call the function luaL_openlibs. Alternatively, the host can select which libraries to open, by using luaL_openselectedlibs. Both functions are declared in the header file lualib.h.

The stand-alone interpreter lua (see §7) already opens all standard libraries.


luaL_openlibs

[-0, +0, e]

void luaL_openlibs (lua_State *L);

Opens all standard Lua libraries into the given state.


luaL_openselectedlibs

[-0, +0, e]

void luaL_openselectedlibs (lua_State *L, int load, int preload);

Opens (loads) and preloads selected standard libraries into the state L. (To preload means to add the library loader into the table package.preload, so that the library can be required later by the program. Keep in mind that require itself is provided by the package library. If a program does not load that library, it will be unable to require anything.)

The integer load selects which libraries to load; the integer preload selects which to preload, among those not loaded. Both are masks formed by a bitwise OR of the following constants:

  • LUA_GLIBK: the basic library.
  • LUA_LOADLIBK: the package library.
  • LUA_COLIBK: the coroutine library.
  • LUA_STRLIBK: the string library.
  • LUA_UTF8LIBK: the UTF-8 library.
  • LUA_TABLIBK: the table library.
  • LUA_MATHLIBK: the mathematical library.
  • LUA_IOLIBK: the I/O library.
  • LUA_OSLIBK: the operating system library.
  • LUA_DBLIBK: the debug library.