dlclose() — Close a dlopen() object


Standards / Extensions C or C++ Dependencies
Single UNIX Specification, Version 3
both z/OS® V1R6


#define _UNIX03_SOURCE
#include <dlfcn.h>

int  dlclose(void *handle);

General description

Informs the system that the dynamic link library (DLL) referenced by a handle returned from a previous dlopen() invocation is no longer needed by the application. Once a DLL has been closed, an application should assume that its symbols and the symbols of any dependent DLLs are no longer available to dlsym().

Returned value

NULL is returned if the referenced DLL was successfully closed. If the DLL could not be closed, or if handle does not refer to an open DLL, a non-zero value will be returned.

Usage notes

  1. A conforming application should use a handle returned from a dlopen() invocation only within a given scope, bracketed by the dlopen() and dlclose() operations. The value of a handle must be treated as an opaque object by the application, used only in calls to dlsym() and dlclose().
  2. DLLs that are loaded explicitly, that is with dlopen(), and are not freed with a corresponding call to dlclose(), are freed automatically at enclave termination in LIFO sequence.
  3. Non-local C++ static destructors defined in a DLL are executed only once, when the DLL program object is deleted from memory.
  4. More detailed diagnostic information is available through dlerror(), the _EDC_DLL_DIAG environment variable, and the Language Environment® DLL Failure control block (CEEDLLF) chain.
  5. This function is not available under SPC, MTF and CSP environments.


The following example illustrates use of dlopen() and dlclose():
/* Open a dynamic library and then close it ... */

#include <dlfcn.h>

void *mylib;
int eret;

mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
eret = dlclose(mylib);

Related information