本文介紹鏈接過程中sysroot
的作用。
sysroot爲何物
做過交叉編譯的同學們,一定對下面這個錯誤十分熟悉吧:
/cross-compiling/ld: cannot find crt1.o: No such file or directory
/cross-compiling/ld: cannot find crti.o: No such file or directory
在我們的pc
上,這兩個文件一般在/usr/lib
或者/usr/lib32
中,通過gcc -print-search-dirs
可以看到這兩個路徑默認就在庫的搜索路徑中,所以在pc
上編譯程序時不存在鏈接器找不到crt1.o
和crti.o
的問題。
crt1.o
負責應用程序的啓動,其中包含了程序的入口函數_start
以及兩個未定義的符號__libc_start_main
和main
,由_start
負責調用__libc_start_main
初始化libc
,然後調用我們源代碼中定義的main
函數,crti.o
負責輔助啓動這些代碼。
下面我們使用交叉編譯工具鏈來查看庫的搜索路徑/cross-compiling/gcc -print-search-dirs
,發現crt1.o
和crti.o
的所在目錄並不在庫的搜索路徑中,所以會出現上述的問題。
下面就需要sysroot
出場了。
sysroot
被稱爲邏輯根目錄,只在鏈接過程中起作用,作爲交叉編譯工具鏈搜索庫文件的根路徑,如配置--sysroot=dir
,則dir
作爲邏輯根目錄,鏈接器將在dir/usr/lib
中搜索庫文件。
只有鏈接器開啓了–with-sysroot選項,–sysroot=director才生效
Reference
微信公衆號同步更新,微信搜索"AnSwEr不是答案"或者掃描二維碼,即可訂閱。
- GitHub:AnSwErYWJ
- Blog:http://www.answerywj.com
- Email:[email protected]
- Weibo:@AnSwEr不是答案