root@bfq:~/mysock# uname -a
linux bfq 3.11.0-26-generic#45~precise1-ubuntu smp tue jul 15 04:02:35 utc 2014x86_64x86_64 x86_64 gnu/linux
root@bfq:~/mysock# vim test.c
編譯、執行:
root@bfq:~/mysock# gcc test.c
root@bfq:~/mysock# file a.out
a.out: elf64-bitlsb executable, x86-64, version 1 (sysv), dynamically linked (uses sharedlibs), for gnu/linux 2.6.24,buildid[sha1]=0x69d02236045c81b597e24cb143cfca5909987b80,
not stripped
root@bfq:~/mysock# ./a.out
sizeof(char *)=8
root@bfq:~/mysock# gcc test.c-m32
a.out: elf32-bitlsb executable, intel 80386, version 1 (sysv), dynamically linked (uses sharedlibs), for gnu/linux 2.6.24,buildid[sha1]=0x668431a373c94667857a83c2d92929e8b57cff58,
sizeof(char *)=4
使用-m32來使得gcc編譯32位程式(在x86_64系統上),使用-m elf_i386參數可以使得64位的ld能夠相容32位的庫
編譯:
root@bfq:~/mysock#ls
libmysock64.so makefile-lib32 makefile-lib64 makefile-win mysock.c mysock.h
root@bfq:~/mysock#make -f makefile-lib32
gcc-wall -d__unix -g -m32 -fpic -o mysock.o -c mysock.c
gcc -m32 -shared -o libmysock32.so mysock.o
root@bfq:~/mysock#file libmysock32.so
libmysock32.so:elf32-bit lsb shared object, intel 80386,version 1 (sysv), dynamically linked,buildid[sha1]=0x18b8a6de746679cccfb10306d6bc62955e3566f4,
root@bfq:~/mysock#
root@bfq:~/mysock#make -f makefile-lib64
gcc-wall -d__unix -g -fpic -o mysock.o -c mysock.c
gcc-shared -o libmysock64.so mysock.o
libmysock64.so makefile-lib32 makefile-lib64 makefile-win mysock.c mysock.h mysock.o
root@bfq:~/mysock#file libmysock64.so
libmysock64.so:elf64-bit lsb shared object, x86-64, version 1(sysv), dynamically linked,buildid[sha1]=0xbec5736775c33f27fe81e88c98f1c26a1edf97b9,
源碼結構:
linux
windows
各種平台的makefile
利用vs開發工具本身可以編譯出lib和dll。
mysock.h
mysock.c