Building Elf Shared Library with MinGW

2019-08-22 14:45发布

问题:

I'm trying to build an shared library on Windows with MinGW, follow this tutorial: Building_elf_shared_libraries

I run this step:

$ gcc -fPICenter code here -c libfoo.c -o libfoo.o
$ gcc -Wall -O2 -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.0 libfoo.o

When I get the .so I try to read it with readelf and get error:

$ readelf -h libfoo.so.1.0.0
readelf: Error: Not an ELF file - it has the wrong magic bytes at the start

Did I misunderstand something?

回答1:

Yes you did misunderstand something, MinGW is a port of gcc to use gcc to both target windows and run on windows natively (or if your host is linux, to target windows from linux). By default it will output files in PE format, not ELF. That's why your readelf call fails. With all that said I assume your trying to build a dynamically linked library and as such I'll provide a nice tutorial here that should get you moving in the correct direction.