13

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
  • 19,426
  • 9
  • 77
  • 140
Sureshkumar Menon
  • 1,115
  • 7
  • 26
  • 49

2 Answers2

15
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
  • 49,582
  • 8
  • 87
  • 140
  • 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
  • 26,249
  • 5
  • 72
  • 89