Skip to content

Linking using ldconfig

Mark Winn edited this page Nov 1, 2019 · 1 revision

To link with ldconfig rather than using the LD_LIBRARY_PATH environment variable, you must add a line stating the directory where myosp has been installed, to /etc/ld.so.conf. So if myosp has been installed at '/usr/lib/myosp', the recommended install location, the line '/usr/lib/myosp' must be added to the file /etc/ld.so.conf. Ensure this is on its own line with no other library path.

Some Operating Systems, including CentOS and Ubuntu, have the line 'include /etc/ld.so.conf.d/*.conf', this allows you to create a new file under the directory /etc/ld.so.conf.d called .conf (in our case 'libmyosp.conf'), which contains a line stating the directory (or multiple lines for multiple directories) of the location of the library files.

In our case, it is important to ensure that our directory is searched before the directory that includes the library files for mysql, this way our lib file will be used over theirs. To ensure this, it is easiest not to create a new file under /etc/ld.so.conf.d, and instead just add the library directory as the first line of /etc/ld.so.conf.

Once the directory has been added to /etc/ld.so.conf, the command 'ldconfig' must be run. This will update /etc/ld.so.cache to include libraries in the directories specified in /etc/ld.so.conf (Side Note: How the linker works is: when looking for a .so file, it will search through the .so files specified in /etc/ld.so.cache). To ensure our library directory has been added properly, run the command 'ldconfig -v | grep :', if one of the lines in the output of this command is our library directory followed by a ':', then our directory has been added correctly.

So now, as long as our directory came before the mysql lib directory, your mysql program you are running should use the myosp libraries rather than the mysql libraries.

If you want to check this, set the environment variable 'LD_DEBUG' to libs by running the command 'export LD_DEBUG=libs'. Next run your mysql program, which in my case is called 'sqlconsole.php'. By setting LD_DEBUG to libs, you will get a lot of debug output from the linker, including declarations of its search for libraries and which library it uses when linking. To check whether your program is using the the myosp libraries, when you run your mysql program, part of the debug output should look like:

  2123:	find library=libmysqlclient_r.so.16 [0]; searching

  2123:	search cache=/etc/ld.so.cache

  2123:	 trying file=/usr/lib/myosp/libmysqlclient_r.so.16

  2123:	

  2123:	

  2123:	calling init: /usr/lib/myosp/libmysqlclient_r.so.16

From this you can see that it is looking for the library file for the mysql client. It searches /etc/ld.so.cache and finds '/usr/lib/myosp/libmysqlclient_r.so.16' (note you can tell this is our library based on the directory it is stored in), it checks this library in the line starting with 'trying file', and finds that it is successful, so it uses it starting in the line starting with 'calling init'.

Ensure once you have done this, that you unset 'LD_DEBUG' by running the command 'unset LD_DEBUG'.