交叉编译器的信息查询

来自:http://blog.sina.com.cn/s/blog_602f87700100g6mi.html

作者:Sam(甄峰)  sam_code@hotmail.com

 

 

Sam常使用各种不同的交叉编译器编译同一份程序。发现不同的编译器在使用同样的Makefile时,会有不同的反应。一直搞不清是怎么回事。现在学习之:

 

1. 如何查看交叉编译器信息:

#gcc -v

#arm-linux-gcc -v

#mipsel-linux-gcc -v

 

 

2.  配置选项的含义:

2.1 --enable-shared, --disable-shared, --enable-static, --disable-static:

允许/禁止编译共享或静态版本的库.

但这个解释,Sam觉得也有点问题,

具体情况描述如下:

2.1.1. NXP 交叉编译器:

#mipsel-linux-gcc -v

显示为 --enable-shared.

则Sam 直接使用 -shared -fpic 就生成了.so

 

1.编译为.o:(Success)

mipsel-unknown-linux-gnu-gcc -c -D_EMBEDDED -D_NXP -D_SHOW  -D_DEBUGTIMEOUT -Wall -I../include -I/opt/tuxbuilder-1.0/mipsel-unknown-linux-gnu/cross/mipsel-unknown-linux-gnu/include BTX.c -o BTX.o

 

2. 编译share library.(Success)

mipsel-unknown-linux-gnu-gcc -D_SHOW -Wall -I../include -I../include -I/opt/tuxbuilder-1.0/mipsel-unknown-linux-gnu/cross/mipsel-unknown-linux-gnu/include -L../resource -L./ -L/opt/tuxbuilder-1.0/mipsel-unknown-linux-gnu/cross/mipsel-unknown-linux-gnu/lib  -shared -fpicbluetooth_remote.o EptCalculate.o GyroAccMouse.o -o libbluetooth_remote.so -lBTX -lbluetooth

 

3. 编译可执行文件:(错误)

mipsel-unknown-linux-gnu-gcc -D_SHOW  -D_DEBUGTIMEOUT -Wall -I../include -I/opt/tuxbuilder-1.0/mipsel-unknown-linux-gnu/cross/mipsel-unknown-linux-gnu/include -L../resource -L./ -L/opt/tuxbuilder-1.0/mipsel-unknown-linux-gnu/cross/mipsel-unknown-linux-gnu/lib  main.c -o BTX_Test  -lBTX -lbluetooth -pthread -lm

 

/opt/tuxbuilder-1.0/mipsel-unknown-linux-gnu/cross/lib/gcc/mipsel-unknown-linux-gnu/4.2.1/../../../../mipsel-unknown-linux-gnu/bin/ld: cannot find -lbluetooth

Sam注意到这个ld是错误的。不太明白为何会调用ld错误了。

 

于是又在..resource中将libbluetooth.so放好。结果竟然OK了。Sam 很惊讶。不知道怎么回事。

Sam猜测也许是这样的:当找不到库时,ld显示的地方搞错了。让人误解为ld给找错地方了。

 

 

 

2.1.2. BCM7405交叉编译器:

#mipsel-linux-gcc -v

没有显示 --enable-shared. 但也没有显示--disable-shared。

 

1. 编译为.o (Success)

mipsel-linux-gcc -c -D_EMBEDDED -D_BCM7405 -D_SHOW  -D_DEBUGTIMEOUT -Wall -I../include -I/home/sam/work/current/BCM/BCM7405/ToolChain/crosstools_hf-linux-2.6.18.0_gcc-4.2-9ts_uclibc-nptl-0.9.29-20070423_20080702/mipsel-linux/include BTX.c -o BTX.o

 

2. 编译为.so

mipsel-linux-gcc -D_SHOW  -D_DEBUGTIMEOUT -Wall -I../include -I/home/sam/work/current/BCM/BCM7405/ToolChain/crosstools_hf-linux-2.6.18.0_gcc-4.2-9ts_uclibc-nptl-0.9.29-20070423_20080702/mipsel-linux/include -shared -fpic BTX.o -o libBTX.so

 

出现错误为:

/home/sam/work/current/BCM/BCM7405/ToolChain/crosstools_hf-linux-2.6.18.0_gcc-4.2-9ts_uclibc-nptl-0.9.29-20070423_20080702/bin/../lib/gcc/mipsel-linux-uclibc/4.2.0/../../../../mipsel-linux-uclibc/bin/ld: non-dynamic relocations refer to dynamic symbol ioctl

出现原因见附录1。

 

所以需要在编译.o 时就加入-fpic -shared

mipsel-linux-gcc -c -D_EMBEDDED -D_BCM7405 -D_SHOW  -D_DEBUGTIMEOUT -Wall -fPIC -shared -I../include -I/home/sam/work/current/BCM/BCM7405/ToolChain/crosstools_hf-linux-2.6.18.0_gcc-4.2-9ts_uclibc-nptl-0.9.29-20070423_20080702/mipsel-linux/include BTX.c -o BTX.o


mipsel-linux-gcc -D_SHOW  -D_DEBUGTIMEOUT -Wall -fPIC -shared -I../include -I/home/sam/work/current/BCM/BCM7405/ToolChain/crosstools_hf-linux-2.6.18.0_gcc-4.2-9ts_uclibc-nptl-0.9.29-20070423_20080702/mipsel-linux/include -shared -fpic BTX.o -o libBTX.so

这样就OK了 。

 

2.1.3: BCM7403交叉编译器:

在这个交叉编译器的使用中,颠覆了Sam一个观点:即交叉编译器放置在何处与编译过程无关。其实还是有关系的。或者说某些做的不是非常好的交叉编译器,还是需要放置在指定位置才好。

 

具体情况如下:

当时Sam将toolchain放在/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607目录下。且export PATH=$PATH:......

 

然后makefile下如下操作:

mipsel-linux-gcc -c -D_EMBEDDED -D_BCM7403 -Wall -I../include -I/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/include BTX.c -o BTX.o


mipsel-linux-gcc -Wall -I../include -I/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/include -shared -fpic BTX.o -o libBTX.so

一切正常。

但当要将so编译进可执行文件时。出现错误:

编译可执行程序:

mipsel-linux-gcc -Wall -I../include -I/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/include -L../resource -L./ -L/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/lib main.c -o BTX_Test -lpthread -lm -lBTX -lbluetooth

 

错误如下:

/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/bin/../lib/gcc/mipsel-linux-uclibc/3.4.6/../../../../mipsel-linux-uclibc/bin/ld: warning: ld-uClibc.so.0, needed by /home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/lib/libc.so, not found (try using -rpath or -rpath-link)
/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/lib/libc.so: undefined reference to `__libc_stack_end'
collect2: ld returned 1 exit status

 

之后Sam只好修改Makefile:

mipsel-linux-gcc -Wall -I../include -I/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/include -L../resource -L./ -L/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/lib main.c -Wl,-rpath-link -Wl,/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/lib   -o BTX_Test -lpthread -lm -lBTX -lbluetooth

编译就成功了。

 

但Sam很奇怪,为什么别人不出这样的问题呢?于是mipsel-linux-gcc -v看了一下:

Configured with: ../gcc-3.4.6/configure --prefix=/opt/toolchains/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/ --target=mipsel-linux-uclibc --build=mipsel-linux --host=mipsel-linux --enable-target-optspace --disable-nls --with-gnu-ld --enable-shared --enable-languages=c,c++ --enable-threads --infodir=/opt/toolchains/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/info --mandir=/opt/toolchains/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/man --disable-__cxa_atexit --disable-checking --with-arch=mips32 --with-float=soft

Sam 突然想到:是不是ToolChain要放到=/opt/toolchains/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/ 才可以呢。于是将toolchain放到指定位置。并去掉-Wl,-rpath-link -Wl,/home/sam/work/current/BCM/BCM7403/ToolChain/crosstools_sf-linux-2.6.12.0_gcc-3.4.6-21_uclibc-0.9.28-20050817-20070607/mipsel-linux/lib

结果一切OK 。

 

结论:Toolchain最好放到其prefix指定的目录中。可以避免找不到库的错误。

 

 

2.1.4: 海思 Hi3110 toolchain:

正常写法就可以编译。

 

 

注意:编译可执行文件时,不能只用-fPIC -shared. 否则会发生段错误。

 

 

附录1:

mipsel中non-dynamic relocations refer to dynamic symbol 错误:


Linker errors
-------------

Shared-library code must be compiled with "-fpic" or "-fPIC".
However, because MIPS compilers have traditionally used (4) as the
default executable mode, the sorts of failure you get by forgetting
"-fpic" and "-fPIC" have tended to be very subtle.  For example,
suppose we have:

    void foo (void) { ... }
    void bar (void) { ... foo (); ... }

Without "-fpic" or "-fPIC", GCC will think it can inline foo() into
bar().  It will then be impossible for an executable (or for another
shared library) to override foo() properly.

The failure for other targets is more drastic, so most cross-target
build systems already do the right thing.  However, some MIPS-specific
build systems might not.

Changing the default executable mode from "(4)" to "(3) or (2)"
makes the MIPS failure mode as drastic as it is for other targets.
Unfortunately, the MIPS linker has traditionally not checked for
accidental uses of absolute code in shared libraries; it would link
the following code as a shared library without any diagnostic at all:

        lui     $4,%hi(x)
        addiu   $4,$4,%lo(x)

The resulting DSO would treat "x" as having the value 0.

This seems too dangerous, so I made the linker complain about any
relocation that it cannot resolve itself and that it cannot implement
dynamically.  The errors have the form:

    non-dynamic relocations refer to dynamic symbol FOO

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值