|
|
| TopPage > C言語関係 > C言語関係[03] |
[hoge]$ which ls
/bin/ls
[hoge]$ ldd /bin/ls
linux-gate.so.1 => (0x008ef000)
libselinux.so.1 => /lib/libselinux.so.1 (0x00cb7000)
librt.so.1 => /lib/librt.so.1 (0x00b8d000)
libcap.so.2 => /lib/libcap.so.2 (0x008c8000)
libacl.so.1 => /lib/libacl.so.1 (0x062fa000)
libc.so.6 => /lib/libc.so.6 (0x0098e000)
libdl.so.2 => /lib/libdl.so.2 (0x00b70000)
/lib/ld-linux.so.2 (0x0096c000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00b53000)
libattr.so.1 => /lib/libattr.so.1 (0x00949000)
|
./
+ Makefile
+ stlib_test.c
stlib/
+ stlib1and2.h
+ stlib1.c
+ stlib2.c
|
stlib1.c
|
stlib2.c
|
stlib1and2.h
|
[hoge]$ gcc -c ./stlib1.c [hoge]$ gcc -c ./stlib2.c [hoge]$ ls stlib1.c stlib1.o stlib1and2.h stlib2.c stlib2.o |
[hoge]$ ar rcv stlib.a stlib1.o stlib2.o a - stlib1.o a - stlib2.o [hoge]$ ls -l 合計 24 -rw-rw-r--. 1 xxxxxxxx xxxxxxxx 2996 mm月 dd 17:11 2015 stlib.a -rw-rw-r--. 1 xxxxxxxx xxxxxxxx 705 mm月 dd 16:53 2015 stlib1.c -rw-rw-r--. 1 xxxxxxxx xxxxxxxx 1360 mm月 dd 16:58 2015 stlib1.o -rw-rw-r--. 1 xxxxxxxx xxxxxxxx 158 mm月 dd 16:58 2015 stlib1and2.h -rw-rw-r--. 1 xxxxxxxx xxxxxxxx 705 mm月 dd 16:53 2015 stlib2.c -rw-rw-r--. 1 xxxxxxxx xxxxxxxx 1360 mm月 dd 16:58 2015 stlib2.o |
CC = gcc CFLAGS = -Wall SRCS = stlib_test.c OBJS = $(SRCS:.c=.o) LIBS = ./stlib/stlib.a PROGRAM = stlib_test all: $(PROGRAM) $(PROGRAM): $(OBJS) $(CC) $(OBJS) $(LIBS) -o $(PROGRAM) clean:; rm -f *.o *~ $(PROGRAM) ### End of Makefile |
|
stlib_test.c (コンパイル後サイズ:5279ytes)
|
stlib_test.c (コンパイル後サイズ:5750ytes)
|
stlib_test.c (コンパイル後サイズ:5766ytes)
|
stlib_test.c (コンパイル後サイズ:5766ytes)
|
// test_func.c
#include <stdio.h>
void hello_1 (void)
{
printf("Hello_1 in test_lib.so\n");
}
void hello_2 (int num)
{
int ii = 0;
for ( ii = 0 ; ii < num ; ii++ ) {
printf("line[%d/%d] ", (ii+1) , num);
printf("Hello_2 in test_lib.so\n");
}
}
|
[hoge]$ ls test_func.c [hoge]$ gcc -c -fPIC -o test_func.o test_func.c [hoge]$ ls test_func.c test_func.o [hoge]$ gcc -shared -o test_lib.so test_func.o [hoge]$ ls test_func.c test_func.o test_lib.so |
[hoge]$ ls test_func.c [hoge]$ gcc -shared -fPIC -o test_func.so test_func.c [hoge]$ ls test_func.c test_lib.so |
| -fPIC |
コード生成規約オプション position-independent code 動的リンクとグローバルオフセットテーブルのサイズの制限を回避する gcc.gnu.orgのサイトには、 "This option makes a difference on the m68k, PowerPC and SPARC." と記載されているのでx86_64マシンには必要なさそうな気がするんだが、殆どの共有ライブラリに関するサイトには -fPICをoptionに入れるように記載している。-fPICを付けたときとつけなかったときとではコンパイル後の ファイルサイズは-fPICを付けたときの方が大きかったのでx86_64マシンでもコード生成内容が異るようなので 一応つける方向で。 |
| -shared | shared objectの生成 |
void hello_1 (void);
void hello_2 (int a);
void main (void)
{
hello_2 (3);
hello_1 ();
}
|
[hoge]$ gcc -o hello_prn hello_prn.c test_lib.so [hoge]$ ls hello_prn hello_prn.c test_func.c test_lib.so |
[hoge]$ LD_LIBRARY_PATH=./ ./hello_prn line[1/3] Hello_2 in test_lib.so line[2/3] Hello_2 in test_lib.so line[3/3] Hello_2 in test_lib.so Hello_1 in test_lib.so |
| TopPage > C言語関係 > C言語関係[03] |