14

How to create a shared object file from a static library? I am using Cygwin.

Is the following syntax correct?

gcc -shared -o libexample.so libexample.a
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
Sureshkumar Menon
  • 1,165
  • 7
  • 27
  • 49

2 Answers2

16
gcc -shared -o libexample.so -Wl,--whole-archive libexample.a

Pay attention that often you'll want the objects combined in your .so to be compiled as PIC, something you don't often want for a static library.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
  • Is it also possible to go the other way around an create a static library from a shared object resp. collection of dependent shared objects? – white_gecko Oct 09 '16 at 14:00
  • Yes -- at least of Unix, a static library is an archive (thus the name .a) of object files with an index. – AProgrammer Oct 10 '16 at 13:10
  • @AProgrammer and how to do the reverse process then (especially the index part)? – vesperto Aug 16 '21 at 16:55
6

It might not work but you can always try:

ar -x libexample.a
gcc -shared *.o -o libexample.so

If it complains about -fPIC, then it probably won't work.

synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91