Note - if the linker is being invoked indirectly, via a compiler driver (eg gcc
) then all the linker command line options should be prefixed by -Wl,
(or whatever is appropriate for the particular compiler driver) like this:
gcc -Wl,--startgroup foo.o bar.o -Wl,--endgroup
This is important, because otherwise the compiler driver program may silently drop the linker options, resulting in a bad link.
实际上主要针对隐式应用LINKER的参数,用“-Wl,”来标识,,“--startgroup foo.o bar.o -Wl,--endgroup”表示一组,,-Bstatic -Bdynamic 作为关键字与-WL,不可分,在GCC连接库时,默认链接是动态链接,现在用上面的指令限制在链接sqlite库时采用静态链接。
-Bstatic 还有三个写法: -dn和
-non_shared 和
-static
-Bdynamic 还有两个写法:-dy
和-call_shared
上面参数“-L/usr/local/sqlite-arm-linux/.libs ”放不放在-Wl,...之间无所谓,因为它只是提供了sqlite动静态库的位置。可以改成下面的参数形式,更直观。
-L/usr/local/sqlite-arm-linux/.libs -L/usr/local/arm/3.3.2/lib -Wl,-dn -lsqlite -Wl,-dy
-Wl,-dn 和 -Wl,-dy成对出现才能起到标题所说的作用。
关于-Wl,后面的参数还有很多,全部明白我也不能。
还有一个问题值得注意,在-Wl,后面不能有空格,否则会出错!
关于-Wl,option 说明还有一段说明
GCC命令参数的英文原文
-Wl,option
Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas.
传递参数option作为linker的一个参数,如果option包含逗号,将在逗号处分割成几个参数。
例如:
-Wl,-dn –lsqlite
-dn 开始静态链接
-lsqlite 静态链接sqlite库
静态链接完后,然后需要动态链接
-Wl,-dy
重新开始动态链接。
-T script
Use script as the linker script. This option is supported by most systems using the GNU linker. On some targets, such as bare-board targets
without an operating system, the -T option may be required when linking to avoid references to undefined symbols.
-Xlinker option
Pass option as an option to the linker. You can use this to supply system-specific linker options which GCC does not know how to recognize.
If you want to pass an option that takes a separate argument, you must use -Xlinker twice, once for the option and once for the argument. For
example, to pass -assert definitions, you must write -Xlinker -assert -Xlinker definitions. It does not work to write -Xlinker "-assert
definitions", because this passes the entire string as a single argument, which is not what the linker expects.
When using the GNU linker, it is usually more convenient to pass arguments to linker options using the option=value syntax than as separate
arguments. For example, you can specify -Xlinker -Map=output.map rather than -Xlinker -Map -Xlinker output.map. Other linkers may not
support this syntax for command-line options.
-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.
NOTE: In Ubuntu 8.10 and later versions, for LDFLAGS, the option -Wl,-z,relro is used. To disable, use -Wl,-z,norelro.