-Wl,option
Pass option as an option to the linker. If option contains commas, it is split into
multiple options at the commas. You can use this syntax to pass an argument
to the option. For example, ‘-Wl,-Map,output.map’ passes ‘-Map output.map’
to the linker. When using the GNU linker, you can also get the same effect
with ‘-Wl,-Map=output.map’.
-rpath dir
Add a directory to the runtime library search path. This is used when
linking an ELF executable with shared objects. All -rpath
arguments are concatenated and passed to the runtime linker, which uses
them to locate shared objects at runtime. The -rpath option is
also used when locating shared objects which are needed by shared
objects explicitly included in the link; see the description of the
-rpath-link option. If -rpath is not used when linking an
ELF executable, the contents of the environment variable
LD_RUN_PATH will be used if it is defined.
The -rpath option may also be used on SunOS. By default, on
SunOS, the linker will form a runtime search patch out of all the
-L options it is given. If a -rpath option is used, the
runtime search path will be formed exclusively using the -rpath
options, ignoring the -L options. This can be useful when using
gcc, which adds many -L options which may be on NFS mounted
filesystems.
For compatibility with other ELF linkers, if the -R option is
followed by a directory name, rather than a file name, it is treated as
the -rpath option.
--whole-archive
For each archive mentioned on the command line after the
--whole-archive option, include every object file in the archive
in the link, rather than searching the archive for the required object
files. This is normally used to turn an archive file into a shared
library, forcing every object to be included in the resulting shared
library. This option may be used more than once.
Reference:
ftp://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono/ld.html
gcc -O -o tds tds.c -ldl
-ldl选项,表示生成的对象模块需要使用共享库
(1)dlopen()
第一个参数:指定共享库的名称,将会在下面位置查找指定的共享库.
-环境变量LD_LIBRARY_PATH列出的用分号间隔的所有目录.
-文件/etc/ld.so.cache中找到的库的列表,用ldconfig维护.
-目录usr/lib.
-目录/lib.
-当前目录.
第二个参数:指定如何打开共享库。
-RTLD_NOW:将共享库中的所有函数加载到内存
-RTLD_LAZY:会推后共享库中的函数的加载操作,直到调用dlsym()时方加载某函数
(2)dlsym()
调用dlsym时,利用dlopen()返回的共享库的phandle以及函数名称作为参数,返回要加载函数的入口地址。
(3)dlerror()
该函数用于检查调用共享库的相关函数出现的错误。
(4)dlclose()
该函数用于关闭动态库。