While experimenting with the OpenNI2 samples, I sometimes would get the following error when trying to run one of the executables:
~/OpenNI-Linux-x86-2.2$ Samples/Bin/SimpleViewer Samples/Bin/SimpleViewer: error while loading shared libraries: libOpenNI2.so: cannot open shared object file: No such file or directory
The dynamic library file libOpenNI2.so is located in the Bin folder alongside the executable. One would expect that like with DLLs on Windows, the dynamic linker have the executable's directory in it's search path.
Well, it turns out that for Linux's linker it is not the case. It searches in the working directory but not in the directory of the executable.
This can be easily fixed by copying the library into a directory that is on the search path of the linker. According to this guide, the convention is to put libraries in /usr/local/lib. So all that needs to be done are the following actions:
~$sudo cp "OpenNI-Linux-x86-2.2 Samples/Redist/libOpenNI2.so" /usr/local/lib ~$sudo ldconfig