--sysroot=dir 的作用
如果在編譯時指定了-sysroot=dir 就是為編譯時指定了邏輯目錄。編譯過程中需要引用的庫,頭文件,如果要到/usr/include目錄下去找的情況下,則會在前面加上邏輯目錄。
如此處我們指定 -sysroot=/home/shell.albert/tools/toolschain_arm/4.4.3/arm-none-linux-gnueabi/sys-root
將dir作為邏輯根目錄(搜索頭文件和庫文件)。比如編譯器通常會在 /usr/include 和 /usr/lib 中搜索頭文件和庫,使用這個選項后將在 dir/usr/include 和 dir/usr/lib 目錄中搜索。如果使用這個選項的同時又使用了 -isysroot 選項,則此選項僅作用於庫文件的搜索路徑,而 -isysroot 選項將作用於頭文件的搜索路徑。這個選項與優化無關,但是在 CLFS 中有着神奇的作用。
-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.
-rpath-link=dir
When using ELF or SunOS, one shared library may require another. This happens when an ld -shared link includes a shared library as one of the input files. When the linker encounters such a dependency when doing a non-shared, non-relocatable link, it will automatically try to locate the required shared library and include it in the link, if it is not included explicitly. In such a case, the -rpath-link option specifies the first set of directories to search. The -rpath-link option may specify a sequence of directory names either by specifying a list of names separated by colons, or by appearing multiple times.
This option should be used with caution as it overrides the search path that may have been hard compiled into a shared library. In such a case it is possible to use unintentionally a different search path than the runtime linker would do.
The difference between -rpath and -rpath-link is that directories specified by -rpath options are included in the executable and used at runtime, whereas the -rpath-link option is only effective at link time. Searching -rpath in this way is only supported by native linkers and cross linkers which have been configured with the --with-sysroot option.
-L: “鏈接”的時候,去找的目錄,也就是所有的 -lFOO 選項里的庫,都會先從 -L 指定的目錄去找,然后是默認的地方。
-rpath: “運行”的時候,去找的目錄。運行的時候,要找 .so 文件,會從這個選項里指定的地方去找。對於交叉編譯,只有配合 --sysroot 選項才能起作用。
-rpath_link (或者 -rpath-link):這個也是用於“鏈接”的時候的,例如你顯示指定的需要 FOO.so,但是 FOO.so 本身是需要 BAR.so 的,后者你並沒有指定,而是 FOO.so 引用到它,這個時候,會先從 -rpath-link 給的路徑里找。
也就是說,-rpath指定的路徑會被記錄在生成的可執行程序中,用於運行時。
-rpath-link 則只用於鏈接時。
rpath-link的格式:
./configure --cross-prefix=/home/zhou/android/android_adt/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- --sysroot=/home/zhou/android/android_adt/ndk/platforms/android-9/arch-arm --disable-shared --disable-symver --disable-everything --enable-gpl --enable-runtime-cpudetect --enable-decoder=h263 --enable-encoder=h263 --enable-encoder=libx264 --enable-parser=h264 --enable-libx264 --enable-decoder=h264 --prefix=/home/hzh/temp/vpu/build/ffmpeg/ --extra-cflags= -DANDROID -D__thumb__ -mthumb -I../build/x264//include --extra-ldflags= -L../build/x264//lib -Wl,-rpath-link,../build/x264//lib --extra-cxxflags=-Wno-multichar -fno-exceptions -fno-rtti
如果有多個rpath, 則:
-Wl,-rpath=/home/oracle/db_1/lib -Wl,-rpath=/home/app/oracle
也可以簡寫,注意是”:”,和PATH的路徑分隔符一樣,還要注意中英文字符:
-Wl,-rpath=/home/oracle/db_1/lib:/home/app/oracle
另外,如果有多個rpath,路徑最好用絕對路徑,不然有些時候configure 那一步過不了, 如ffmpeg。
bash 腳本里的到絕對路徑的方法:
# bash腳本里 = 前后不能有空格,等號前后不能有空格,賦值
VPU_WRAPPER=../../vpu-helper/vpu_wrapper pushd $VPU_WRAPPER VPU_WRAPPER_DIR=`pwd` popd EXTRA_CFLAGS="$EXTRA_CFLAGS -I$VPU_WRAPPER_DIR" EXTRA_LDFLAGS="$EXTRA_LDFLAGS -L$VPU_WRAPPER_DIR/lib"
echo $EXTRA_LDFLAGS
# pushd 和 popd 會改變當前文件專屬的目錄路徑,只對當前文件有效,退出當前文件的執行后立即失效
makefile里
CROSS_DIR = ../arm-2012.09/bin CROSS_PREFIX = arm-none-linux-gnueabi- CC = $(shell cd $(CROSS_DIR) && pwd)/$(CROSS_PREFIX)gcc AR = $(shell cd $(CROSS_DIR) && pwd)/$(CROSS_PREFIX)ar STRIP = $(shell cd $(CROSS_DIR) && pwd)/$(CROSS_PREFIX)strip RANLIB= $(shell cd $(CROSS_DIR) && pwd)/$(CROSS_PREFIX)ranlib
@echo "CC is: '$(CC)'"
$(warning "CC is: $(CC)")
# $(shell cd $(CROSS_DIR)),改變目錄只針對當前語句有效,退出當前語句后目錄改變結果立即失效,即這一句不影響文件里的目錄路徑,只影響自己這一句。