Main Content

Set Run-Time Library Path for C++ Interface

If the C++ library has a compiled library file, then that file and its dependencies must be on your system path or run-time search path (rpath). The publisher provides information about the library files. If the library is completely defined in header or source files, then there might not be a compiled library file.

You can set the path each time that you work on the library or set it permanently by setting values in the system environment.

Add Library to Path in System Environment

If you configured your interface to run in out-of-process execution mode, you can set your system path environment variable using the OutOfProcessEnvironmentVariables argument in the clibConfiguration function. The setting is persistent across different MATLAB® sessions. For example, if you want interface myLib to access files in myPath on Windows®, type:

pathVar = dictionary("PATH",myPath);
configObj = clibConfiguration("myLib", ...
    ExecutionMode="outofprocess", ...
    OutOfProcessEnvironmentVariables=pathVar);

Note

The OutOfProcessEnvironmentVariables option is available in out-of-process execution mode only. For more information, see Load C++ Library In-Process or Out-of-Process.

Alternatively, refer to your operating system documentation for information about adding the library to the system path.

Temporarily Set Run-Time Library Path in MATLAB

On Windows platforms, if the compiled library files are located on rtPath, then in MATLAB call:

dllPath = 'rtPath'; 
syspath = getenv('PATH'); 
setenv('PATH',[dllPath pathsep syspath]);

Note

If you use these commands, then you must set the path each time that you start MATLAB.

Temporarily Set Run-Time Library Path at System Prompt

To set the run-time library path rtPath temporarily, run one of these commands before you start MATLAB. You must restart MATLAB from this system prompt.

  • Windows Command Processor:

    set PATH=rtPath;%PATH%
  • Linux®:

    LD_LIBRARY_PATH=rtPath:LD_LIBRARY_PATH
    export LD_LIBRARY_PATH
  • macOS (set path and start MATLAB):

    DYLD_LIBRARY_PATH=matlabroot/bin/macos:matlabroot/sys/os/macos /matlabexecutable

    Replace macos with either maca64 for macOS with Apple silicon or maci64 for macOS with Intel®. Set matlabexecutable to the full path to the MATLAB_macos executable inside the MATLAB application bundle. An example of a full path is /Applications/MATLAB_R2023b.app/Contents/MacOS/MATLAB_maca64.

Note

Start MATLAB in the same operating system prompt where you set the path environment variable. To verify the updated system path, in MATLAB type one of these commands:

getenv('PATH') % Windows
getenv('LD_LIBRARY_PATH') % Linux
getenv('DYLD_LIBRARY_PATH') % macOS

Note

If you use these commands, then you must set the path each time that you open the operating system prompt.

See Also

Topics