问题
I am trying to deal with a problem like the following one:
Assume that I have a library libxyz.a created from:
/* main.c */
int main(void)
{
int a;
}
compiled and archived with:
gcc -c main.c -o abc.o && ar cr libxyz.a abc.o
How do I have to write linker script in order to put abc.o exactly where it is expected to be?
I was trying to handle it in such way:
/* script.ld */
SEARCH_DIR(.)
INPUT(-lxyz)
SECTIONS
{
.text : { xyz:abc(.text) }
.data : { xyz:abc(.data) }
.bss : { xyz:abc(.bss) }
}
but after running:
ld -T script.ld
I get:
ld: cannot find xyz:abc
I couldn't find any example of extracting archives in linker files on forums. The only thing I have found was the linker's documentation, which only contains information about archive:file
construction.
回答1:
Just had to deal with same problem today, so if someone else bumps into this question: some file extensions are missing. Something like works fine:
SECTIONS
{
.text : { libxyz.a:abc.o(.text) }
.data : { libxyz.a:abc.o(.data) }
.bss : { libxyz.a:abc.o(.bss) }
}
One also needs to deal with paths (if library is actually somewhere/libxyz.a).
回答2:
Probably there is problem with your ld because I tried your script on my machine it ran correctly without error. my ld version is GNU ld (GNU Binutils for Ubuntu) 2.22
来源:https://stackoverflow.com/questions/14215958/extracting-archive-file-in-linker-script