リンクとリンクオプション-Lダイナミックライブラリ、-rpath-link、-rpath



Links Link Options L Dynamic Libraries



ソースコードのオープンソースライブラリを読むときに、デバッグ分析用のデモを作成する必要がある場合があります。これはより直感的です。ソースリンクでコンパイルされたDLLを通過する必要があり、このダイナミックライブラリは通常インストールを行いません。これはデフォルトのシステムライブラリ検索パスにインストールされていません。リンク時にmakefileでリンクライブラリ検索パスを指定します:- Lpath -lmylib、コンパイルに合格しましたが、ライブラリを見つけるように求められて実行されています。システムのダイナミックライブラリ検索パスを変更したくありません。今回は-Wl、-rpathが機能しました。

使用法:



gcc -Wl,--start-group foo.o bar.o -Wl,--end-group

どのsopathダイナミックライブラリ検索パス、libmyso.soダイナミックライブラリ。 -L -lは、リンクオプション、-Wl、-rpath by mangccに精通しています。

gcc demo.c -o demo -Lsopath -lmyso -Wl,-rpath=sopath

-Wlオプション:コネクタオプションとしての転送オプション。カンマで区切られたその他のオプション。



gccオプションの場合は-Wlですが、rpathldオプションは引き続きmanldを実行します。

 -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ライブラリ検索パス指定状態移動操作時、ランタイムライブラリ検索パス環境変数LD_RUN_PATHを指定する別の方法、この方法の欠点は他のプログラムに影響を与えることです。

-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------



より詳細な説明:https://my.oschina.net/shelllife/blog/115958

gccでldlinkオプションを使用する場合、コンパイラがオプションではないことを区別するために、オプション-Wl(1ではなく文字l、私は繰り返し間違えました)の前にプレフィックスを追加する必要があります。

リンカがコンパイラドライバ(gccなど)を介して間接的に呼び出されている場合、すべてのリンカコマンドラインオプションの前に-Wl(または特定のコンパイラドライバに適切なもの)を付ける必要があります。

 -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 file systems. 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.

これは重要です。そうしないと、コンパイラドライバプログラムがリンカーオプションをサイレントにドロップして、リンクが不良になる可能性があるためです。