问题
when running
arm-none-linux-gnueabi-gcc -print-search-dirs | grep libraries | sed 's/:/\n/g'
I get the following output:
libraries
=/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/
/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/lib/arm-none-linux-gnueabi/4.4.1/
/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/lib/
/opt/codesourcery/arm-none-linux-gnueabi/libc/lib/arm-none-linux-gnueabi/4.4.1/
/opt/codesourcery/arm-none-linux-gnueabi/libc/lib/
/opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/arm-none-linux-gnueabi/4.4.1/
/opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/
and when cross-compiling some packages it would make my life MUCH easier to be able to append to these paths. I know about the -L option, and I would like to use it, but many packages don't respect it while cross compiling, moreover using it with codesourcery causes problems. (see end of this post for details). Basically I've got a whole directory tree with all the required files to compile packages, and I want to add that directory tree to the default search paths. E.g, so that -print-search-dirs will also output a path similar to:
/target/usr/lib
/target/lib
in addition to the paths already listed. I'm not looking for a codesourcey specific answer, since I have several toolchains I can work with.
For those who are curious, here is the error from a codesourcery installation:
arm-none-linux-gnueabi-gcc test.c
works
arm-none-linux-gnueabi-gcc -L/usr/arm-unknown-linux-gnueabi/usr/lib test.c
fails with:
/lib/libc.so.6: file not recognized: File format not recognized
collect2: ld returned 1 exit status
so obviously it's grabbing libc from the host. I've seen this happen with more than one toolchain, but not all.
In response to a comment: Here is the output for
arm-none-linux-gnueabi-gcc -L/usr/arm-unknown-linux-gnueabi/usr/lib test.c -v -Wl,-verbose
Using built-in specs. Target: arm-none-linux-gnueabi Configured with: /scratch/julian/2010q1-release-linux-lite/src/gcc-4.4-2010q1/configure --build=i686-pc-linux-gnu --host=i686-pc-linux-gnu --target=arm-none-linux-gnueabi --enable-threads --disable-libmudflap --disable-libssp --disable-libstdcxx-pch --enable-extra-sgxxlite-multilibs --with-arch=armv5te --with-gnu-as --with-gnu-ld --with-specs='%{funwind-tables|fno-unwind-tables|mabi=*|ffreestanding|nostdlib:;:-funwind-tables} %{O2:%{!fno-remove-local-statics: -fremove-local-statics}} %{O*:%{O|O0|O1|O2|Os:;:%{!fno-remove-local-statics: -fremove-local-statics}}}' --enable-languages=c,c++ --enable-shared --disable-lto --enable-symvers=gnu --enable-__cxa_atexit --with-pkgversion='Sourcery G++ Lite 2010q1-202' --with-bugurl=https://support.codesourcery.com/GNUToolchain/ --disable-nls --prefix=/opt/codesourcery --with-sysroot=/opt/codesourcery/arm-none-linux-gnueabi/libc --with-build-sysroot=/scratch/julian/2010q1-release-linux-lite/install/arm-none-linux-gnueabi/libc --with-gmp=/scratch/julian/2010q1-release-linux-lite/obj/host-libs-2010q1-202-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-mpfr=/scratch/julian/2010q1-release-linux-lite/obj/host-libs-2010q1-202-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-ppl=/scratch/julian/2010q1-release-linux-lite/obj/host-libs-2010q1-202-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-cloog=/scratch/julian/2010q1-release-linux-lite/obj/host-libs-2010q1-202-arm-none-linux-gnueabi-i686-pc-linux-gnu/usr --disable-libgomp --enable-poison-system-directories --with-build-time-tools=/scratch/julian/2010q1-release-linux-lite/install/arm-none-linux-gnueabi/bin --with-build-time-tools=/scratch/julian/2010q1-release-linux-lite/install/arm-none-linux-gnueabi/bin Thread model: posix gcc version 4.4.1 (Sourcery G++ Lite 2010q1-202) COLLECT_GCC_OPTIONS='-L/usr/arm-unknown-linux-gnueabi/usr/lib' '-v' '-march=armv5te' '-funwind-tables' /opt/codesourcery/libexec/gcc/arm-none-linux-gnueabi/4.4.1/cc1 -quiet -v test.c -quiet -dumpbase test.c -march=armv5te -auxbase test -version -funwind-tables -o /tmp/ccaDSo09.s ignoring nonexistent directory "/opt/codesourcery/arm-none-linux-gnueabi/libc/usr/local/include" #include "..." search starts here: #include search starts here: /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/include /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/include-fixed /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/include /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/include End of search list. GNU C (Sourcery G++ Lite 2010q1-202) version 4.4.1 (arm-none-linux-gnueabi) compiled by GNU C version 4.3.2, GMP version 4.3.1, MPFR version 2.4.2. GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 250bf78701f747da89e730786c466148 COLLECT_GCC_OPTIONS='-L/usr/arm-unknown-linux-gnueabi/usr/lib' '-v' '-march=armv5te' '-funwind-tables' /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/as -v -march=armv5te -meabi=5 -o /tmp/ccQTJKJd.o /tmp/ccaDSo09.s GNU assembler version 2.19.51 (arm-none-linux-gnueabi) using BFD version (Sourcery G++ Lite 2010q1-202) 2.19.51.20090709 COMPILER_PATH=/opt/codesourcery/libexec/gcc/arm-none-linux-gnueabi/4.4.1/:/opt/codesourcery/libexec/gcc/arm-none-linux-gnueabi/4.4.1/:/opt/codesourcery/libexec/gcc/arm-none-linux-gnueabi/:/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/:/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/:/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/bin/ LIBRARY_PATH=/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/:/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/lib/:/opt/codesourcery/arm-none-linux-gnueabi/libc/lib/:/opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/ COLLECT_GCC_OPTIONS='-L/usr/arm-unknown-linux-gnueabi/usr/lib' '-v' '-march=armv5te' '-funwind-tables' /opt/codesourcery/libexec/gcc/arm-none-linux-gnueabi/4.4.1/collect2 --sysroot=/opt/codesourcery/arm-none-linux-gnueabi/libc --eh-frame-hdr -dynamic-linker /lib/ld-linux.so.3 -X -m armelf_linux_eabi /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crt1.o /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crti.o /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/crtbegin.o -L/usr/arm-unknown-linux-gnueabi/usr/lib -L/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1 -L/opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/lib -L/opt/codesourcery/arm-none-linux-gnueabi/libc/lib -L/opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib /tmp/ccQTJKJd.o -verbose -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/crtend.o /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crtn.o GNU ld (Sourcery G++ Lite 2010q1-202) 2.19.51.20090709 Supported emulations: armelf_linux_eabi armelfb_linux_eabi using internal linker script: ================================================== /* Script for -z combreloc: combine and sort reloc sections */ OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") OUTPUT_ARCH(arm) ENTRY(_start) SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SECTIONS { /* Read-only sections, merged into text segment: */ PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x00008000)); . = SEGMENT_START("text-segment", 0x00008000) + SIZEOF_HEADERS; .interp : { *(.interp) } .note.gnu.build-id : { *(.note.gnu.build-id) } .hash : { *(.hash) } .gnu.hash : { *(.gnu.hash) } .dynsym : { *(.dynsym) } .dynstr : { *(.dynstr) } .gnu.version : { *(.gnu.version) } .gnu.version_d : { *(.gnu.version_d) } .gnu.version_r : { *(.gnu.version_r) } .rel.dyn : { *(.rel.init) *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*) *(.rel.fini) *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*) *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*) *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*) *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*) *(.rel.ctors) *(.rel.dtors) *(.rel.got) *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*) *(.rel.ifunc) } .rela.dyn : { *(.rela.init) *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) *(.rela.fini) *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) *(.rela.ctors) *(.rela.dtors) *(.rela.got) *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) *(.rela.ifunc) } .rel.plt : { *(.rel.plt) PROVIDE_HIDDEN (__rel_iplt_start = .); *(.rel.iplt) PROVIDE_HIDDEN (__rel_iplt_end = .); } .rela.plt : { *(.rela.plt) PROVIDE_HIDDEN (__rela_iplt_start = .); *(.rela.iplt) PROVIDE_HIDDEN (__rela_iplt_end = .); } .init : { KEEP (*(.init)) } =0 .plt : { *(.plt) *(.iplt)} .text : { *(.text .stub .text.* .gnu.linkonce.t.*) /* .gnu.warning sections are handled specially by elf32.em. */ *(.gnu.warning) *(.glue_7t) *(.glue_7) *(.vfp11_veneer) *(.v4_bx) } =0 .fini : { KEEP (*(.fini)) } =0 PROVIDE (__etext = .); PROVIDE (_etext = .); PROVIDE (etext = .); .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } .rodata1 : { *(.rodata1) } .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } PROVIDE_HIDDEN (__exidx_start = .); .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } PROVIDE_HIDDEN (__exidx_end = .); .eh_frame_hdr : { *(.eh_frame_hdr) } .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) } .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } /* Adjust the address for the data segment. We want to adjust up to the same address within the page on the next page up. */ . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); /* Exception handling */ .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) } .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } /* Thread Local Storage sections */ .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) } .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } .preinit_array : { PROVIDE_HIDDEN (__preinit_array_start = .); KEEP (*(.preinit_array)) PROVIDE_HIDDEN (__preinit_array_end = .); } .init_array : { PROVIDE_HIDDEN (__init_array_start = .); KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array)) PROVIDE_HIDDEN (__init_array_end = .); } .fini_array : { PROVIDE_HIDDEN (__fini_array_start = .); KEEP (*(SORT(.fini_array.*))) KEEP (*(.fini_array)) PROVIDE_HIDDEN (__fini_array_end = .); } .ctors : { /* gcc uses crtbegin.o to find the start of the constructors, so we make sure it is first. Because this is a wildcard, it doesn't matter if the user does not actually link against crtbegin.o; the linker won't look for a file to match a wildcard. The wildcard also means that it doesn't matter which directory crtbegin.o is in. */ KEEP (*crtbegin.o(.ctors)) KEEP (*crtbegin?.o(.ctors)) /* We don't want to include the .ctor section from the crtend.o file until after the sorted ctors. The .ctor section from the crtend file contains the end of ctors marker and it must be last */ KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) KEEP (*(SORT(.ctors.*))) KEEP (*(.ctors)) } .dtors : { KEEP (*crtbegin.o(.dtors)) KEEP (*crtbegin?.o(.dtors)) KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) KEEP (*(SORT(.dtors.*))) KEEP (*(.dtors)) } .jcr : { KEEP (*(.jcr)) } .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) } .dynamic : { *(.dynamic) } . = DATA_SEGMENT_RELRO_END (0, .); .got : { *(.got.plt) *(.igot.plt) *(.got) *(.igot) } .data : { __data_start = . ; *(.data .data.* .gnu.linkonce.d.*) SORT(CONSTRUCTORS) } .data1 : { *(.data1) } _edata = .; PROVIDE (edata = .); __bss_start = .; __bss_start__ = .; .bss : { *(.dynbss) *(.bss .bss.* .gnu.linkonce.b.*) *(COMMON) /* Align here to ensure that the .bss section occupies space up to _end. Align after .bss to ensure correct alignment even if the .bss section disappears because there are no input sections. FIXME: Why do we need it? When there is no .bss section, we don't pad the .data section. */ . = ALIGN(. != 0 ? 32 / 8 : 1); } _bss_end__ = . ; __bss_end__ = . ; . = ALIGN(32 / 8); . = ALIGN(32 / 8); __end__ = . ; _end = .; PROVIDE (end = .); . = DATA_SEGMENT_END (.); /* Stabs debugging sections. */ .stab 0 : { *(.stab) } .stabstr 0 : { *(.stabstr) } .stab.excl 0 : { *(.stab.excl) } .stab.exclstr 0 : { *(.stab.exclstr) } .stab.index 0 : { *(.stab.index) } .stab.indexstr 0 : { *(.stab.indexstr) } .comment 0 : { *(.comment) } /* DWARF debug sections. Symbols in the DWARF debugging sections are relative to the beginning of the section so we begin them at 0. */ /* DWARF 1 */ .debug 0 : { *(.debug) } .line 0 : { *(.line) } /* GNU DWARF 1 extensions */ .debug_srcinfo 0 : { *(.debug_srcinfo) } .debug_sfnames 0 : { *(.debug_sfnames) } /* DWARF 1.1 and DWARF 2 */ .debug_aranges 0 : { *(.debug_aranges) } .debug_pubnames 0 : { *(.debug_pubnames) } /* DWARF 2 */ .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } .debug_abbrev 0 : { *(.debug_abbrev) } .debug_line 0 : { *(.debug_line) } .debug_frame 0 : { *(.debug_frame) } .debug_str 0 : { *(.debug_str) } .debug_loc 0 : { *(.debug_loc) } .debug_macinfo 0 : { *(.debug_macinfo) } /* SGI/MIPS DWARF 2 extensions */ .debug_weaknames 0 : { *(.debug_weaknames) } .debug_funcnames 0 : { *(.debug_funcnames) } .debug_typenames 0 : { *(.debug_typenames) } .debug_varnames 0 : { *(.debug_varnames) } /* DWARF 3 */ .debug_pubtypes 0 : { *(.debug_pubtypes) } .debug_ranges 0 : { *(.debug_ranges) } .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) } /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) } } ================================================== attempt to open /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crt1.o succeeded /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crt1.o attempt to open /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crti.o succeeded /opt/codesourcery/arm-none-linux-gnueabi/libc/usr/lib/crti.o attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/crtbegin.o succeeded /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/crtbegin.o attempt to open /tmp/ccQTJKJd.o succeeded /tmp/ccQTJKJd.o attempt to open /usr/arm-unknown-linux-gnueabi/usr/lib/libgcc.so failed attempt to open /usr/arm-unknown-linux-gnueabi/usr/lib/libgcc.a failed attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/libgcc.so failed attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/libgcc.a succeeded attempt to open /usr/arm-unknown-linux-gnueabi/usr/lib/libgcc_s.so failed attempt to open /usr/arm-unknown-linux-gnueabi/usr/lib/libgcc_s.a failed attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/libgcc_s.so failed attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/libgcc_s.a failed attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/lib/libgcc_s.so failed attempt to open /opt/codesourcery/lib/gcc/arm-none-linux-gnueabi/4.4.1/../../../../arm-none-linux-gnueabi/lib/libgcc_s.a failed attempt to open /opt/codesourcery/arm-none-linux-gnueabi/libc/lib/libgcc_s.so succeeded -lgcc_s (/opt/codesourcery/arm-none-linux-gnueabi/libc/lib/libgcc_s.so) attempt to open /usr/arm-unknown-linux-gnueabi/usr/lib/libc.so succeeded opened script file /usr/arm-unknown-linux-gnueabi/usr/lib/libc.so opened script file /usr/arm-unknown-linux-gnueabi/usr/lib/libc.so attempt to open /lib/libc.so.6 succeeded opened script file /lib/libc.so.6 /lib/libc.so.6: file not recognized: File format not recognized collect2: ld returned 1 exit status
回答1:
Your cross-compiling glibc is configured in a wrong way.
In -Wl,-verbose
linker says:
attempt to open /usr/arm-unknown-linux-gnueabi/usr/lib/libc.so succeeded
opened script file /usr/arm-unknown-linux-gnueabi/usr/lib/libc.so
So, it used your -L
option and was able to open libc.so. But this is not a real .so
dynamic library, it is a script (linker script).
Telepathic mode on: Last line of this libc.so
script is
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a AS_NEEDED ( /lib/ld-linux.so.3 ) )
Telepathic mode off
This line contains absolute pathes hardcoded and redirects linker from arm-...abi to the system's libc ignoring most of search-dirs (may be there is some secret option of ld
).
Just edit this file (with doing a backup) with any text editor and replace all /lib
with /usr/arm-unknown-linux-gnueabi/lib
. (My good cross-compiler environment has all pathes in this script replaced with prefix.)
The original script should be used on native compile, native run, but not on cross compiling.
UPDATE: bytesex says http://linux.bytesex.org/cross-compiler.html that there is the same problem with /usr/lib/libpthread.so
, do the replace in this file too.
To check other libs, do file -skL *.so|grep text
in the lib dir.
回答2:
As explained by @osgx, gcc is looking in the wrong path for its libraries. I hit a similar issue when trying to cross-compile a program for arm on x86_64. As editing the root-owned files was not an option, I searched further and discovered the --sysroot
option:
--sysroot=dir
Use dir as the logical root directory for headers and libraries. For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.
The GNU linker (beginning with version 2.16) has the necessary support for this option. If your linker does not support this option, the header file aspect of --sysroot still works, but the library aspect does not.
Example usage:
./configure --host=arm-unknown-linux-gnueabi \
CFLAGS='-g -O2 --sysroot=/tmp/rpi-root'
来源:https://stackoverflow.com/questions/7476625/set-global-gcc-default-search-paths