0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gcc/g++ Tips

Last updated at Posted at 2024-06-20

共有ライブラリ

/usr/lib/
├── libexample.so.1.0       # 実体ファイル
├── libexample.so.1         # バージョンリンク -> libexample.so.1.0
└── libexample.so           # 開発用リンク -> libexample.so.1

共有ライブラリの作り方

以下でlibshared.soを出力

$ g++ -fPIC -c liba.cpp
$ g++ -fPIC -c libb.cpp
$ g++ -shared liba.o libb.o -o libshared.so.1.0

gccオプション

リンカオプション

-l(小文字のエル)

リンク対象のライブラリを指定

# libexample.so or libexample.aの場合、以下のように先頭のlibや拡張子は不要
$ gcc -lexample

ディレクトリオプション

-I(大文字のアイ)

指定パスから先のヘッダーファイルを参照

$ gcc -I<directory path>

-L

指定パスから、-l(小文字のエル)で指定したライブラリを検索
※実行時、このパス先の共有ライブラリが使われるとは言ってない

gcc -L<directory path>

-rpath

実行時、指定したパス先の共有ライブラリを動的リンクする

$ gcc -rpath=<directory path>

環境変数

LD_LIBRARY_PATH

rpathが設定されていない場合、実行時、この変数で指定したパス群の左から順番に共有ライブラリを探し、最初に見つかったものを動的リンク

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?