LoginSignup
1
1

「ld: library not found for -lSystem」が発生した場合の対策

Last updated at Posted at 2023-02-26

現象

渋谷ほととぎす通信さんのブログをもとに、Macでアセンブリファイル「helloworld.asm」をコンパイル&実行しようとした。
しかし、オブジェクトファイルとSystemライブラリをリンクするところで、ライブラリが見つからないエラーが発生してしまった。

ld -arch x86_64 -macosx_version_min 10.11 helloworld.o -lSystem
ld: library not found for -lSystem # ←見つからないエラー

日本語の解決策ページがなかなか見つからなかったので、同じ現象が発生してしまった日本人のために、解決方法をここに記しておく。

対策

「macOS Big Sur 11.0.1」からライブラリ周りの仕様が変わったっぽい?
StackOverFlowの投稿を参考に、-Lオプションで、ライブラリ検索に使用するディレクトリを指定する。

ld helloworld.o -macosx_version_min 10.11 -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem

結果

実行ファイルが出力された!
image.png

ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in _main from helloworld.o. To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie

ワーニングはとりあえずコードの問題なので無視...
無事a.outが作成できたので、これを実行すれば

/a.out  
Hello World

成功!!
あとは「helloworld.asm」の中身をみて理解を深めよう

1
1
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
1
1