I wrote a library that contains a.c
and b.c
, and another program c.c
that uses this library. I have archived the library using ar
to distribute it. Here are the commands I use:
gcc -g -c -o a.o a.c # Compile a.c
gcc -g -c -o b.o b.c # Compile b.c
ar r ab.a a.o b.o # Generate library ab.a
gcc -o c c.c ab.a # Compile c.c
Everything works fine until now. But I want to strip some sections in ab.a
(e.g. debug info, internal functions), so I run strip --keep-symbol=... ab.a
on the library. Now I cannot compile c.c any more. The error I get is:
+ gcc -o c c.c ab.a
/usr/bin/ld: ab.a: error adding symbols: archive has no index; run ranlib to add one
collect2: error: ld returned 1 exit status
If I run ranlib ab.a
after strip, everything works fine. My question is: is it possible to change the arguments in the strip
command, so that I do not need to run ranlib ab.a
?
Complete example
a.c
:
int a1()
{
return 1;
}
int a2()
{
return 2;
}
b.c
:
int b1()
{
return 3;
}
int b2()
{
return 4;
}
c.c
:
#include <stdio.h>
int a1();
int a2();
int b1();
int b2();
int main()
{
printf("%d ", a1());
printf("%d ", a2());
printf("%d ", b1());
printf("%d\n", b2());
}
Bash script to the "archive has no index" error:
gcc -g -c -o a.o a.c
gcc -g -c -o b.o b.c
ar r ab.a a.o b.o
strip --keep-symbol={a1,a2,b1,b2} ab.a
# ranlib ab.a
gcc -o c c.c ab.a
./c
Run the bash script
- Run the bash script directly -> see the error
- Comment the
strip
line -> success (symbols not stripped) - Uncomment the
ranlib
line -> success
Software versions I am using:
- gcc (GCC) 10.2.1 20201125 (Red Hat 10.2.1-9)
- GNU ar version 2.35-18.fc33
- GNU strip version 2.35-18.fc33
- GNU ranlib version 2.35-18.fc33