Home >Backend Development >C++ >How to Set Specific Library Paths in G and LD?
How to Prioritize Specific Library Path Preferences
When using g and ld to compile a C program, it's possible to encounter scenarios where a library of the same name exists both in a default path and a custom path, leading to conflicts. To resolve this, there are two primary approaches:
Using LD_LIBRARY_PATH (or Equivalent)
The LD_LIBRARY_PATH environment variable allows you to specify the search path for dynamic libraries. To prioritize your custom library, add its path to the LD_LIBRARY_PATH before the default path. For example:
<code class="bash">export LD_LIBRARY_PATH=/my/dir:$LD_LIBRARY_PATH</code>
Using the "-Wl,-rpath" Option
The "-Wl,-rpath" option passed to g instructs the linker to use a specific path as the runtime library search path. This path will be given precedence over the standard search path. An example command would be:
<code class="bash">g++ -Wall -g -o my_binary -L/my/dir -lfoo -Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH) bar.cpp</code>
Additional Considerations
The above is the detailed content of How to Set Specific Library Paths in G and LD?. For more information, please follow other related articles on the PHP Chinese website!