您的位置:首页 > 运维架构 > Linux

linux连接库编译过程分析

2010-03-25 22:47 190 查看
一、


静态链接库编译


基本编译过程


gcc -Wall -g -c -o mylib.o mylib.c

ar rcs mylib.a mylib.o #
修改为
libmylib.a

gcc -Wall -g -c test.c -o test.o

gcc -g -o test test.o -L. –lmylib

san@san-desktop:/work/mylib/libtest$.

|--
lib

|
|-- Makefile

|
|-- libmy.a

|
|-- mylib.c

|
|-- mylib.h

|
`-- mylib.o

`--
src

|-- Makefile

|-- test

`-- test.c

Lib

目录文件之

Makefile


CC
= gcc

CFLAGS
= -Wall -g -c

src
= mylib.c

dir
= $(notdir $(src))

obj
= $(patsubst %.c,%.o,$(dir))

libs
= libmy.a

test:

@echo "--src------------"

@echo $(src)

@echo "--dir------------"

@echo $(dir)

@echo "--obj------------"

@echo $(obj)

all:$(libs)

%.o:%.c

$(CC) $(CFLAGS) -o $@ $^

$(libs):$(obj)

$(AR) rcs $@ $^

clean:

rm -rf *.o

Lib

目录文件之

mylib.c


#include
<stdio.h>

void
hello()

{

printf("This is my lib./n");

}

Lib

目录文件之

mylib.h


extern
void hello();

src

目录之

Makefile


CC
= gcc

CFLAGS
= -g -I../lib

LDFLAGS
= -L../lib -lmy #
注意:
-lmy
对应
libmy.a
静态库文件

src
= test.c

dir
= $(notdir $(src))

obj
= $(patsubst %.c,%.o,$(dir))

target
= test

test:

@echo "--src------------"

@echo $(src)

@echo "--dir------------"

@echo $(dir)

@echo "--obj------------"

@echo $(obj)

all:$(target)

#%.o:%.c

#
$(CC) $(CFLAGS) -o $@ $^

$(target):$(src)

$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:

rm -rf *.o

src

目录之

test.c


#include
"mylib.h"

int
main()

{

hello();

return 0;

}

二、


动态连接库编译


基本编译过程

gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so

gcc test.c -L. -ltest -o test

.

|--
lib

|
|-- Makefile

|
|-- libtest.so

|
|-- so_test.h

|
|-- test_a.c

|
|-- test_b.c

|
`-- test_c.c

`--
src

|-- Makefile

|-- test

`-- test.c

Lib

目录文件之

Makefile


CC
= gcc

CFLAGS
= -Wall -g -fPIC -shared

src
= test_a.c test_b.c test_c.c

dir
= $(notdir $(src))

obj
= $(patsubst %.c,%.o,$(dir))

libs
= libtest.so

test:

@echo "--src------------"

@echo $(src)

@echo "--dir------------"

@echo $(dir)

@echo "--obj------------"

@echo $(obj)

all:$(libs)

$(libs):$(src)

$(CC) $(CFLAGS) -o $@ $^

clean:

rm -rf *.o

Lib

目录文件之

*.c


#include
<stdio.h>

#include
"so_test.h"

void
test_a()

{

printf("This is in
test_a.../n");

}

src

目录文件之

Makefile


CC
= gcc

CFLAGS
= -Wall -g -fPIC -I../lib

LDFLAGS
= -L../lib -ltest

src
= test.c

dir
= $(notdir $(src))

obj
= $(patsubst %.c,%.o,$(dir))

target
= test

test:

@echo "--src------------"

@echo $(src)

@echo "--dir------------"

@echo $(dir)

@echo "--obj------------"

@echo $(obj)

all:$(target)

$(target):$(src)

$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:

rm -rf *.o

src

目录文件之

test.c


#include
"so_test.h"

int
main()

{

test_a();

test_b();

test_c();

return 0;

}

三、


动态加载


基本编译过程

gcc
-Wall -g -c dynamic.c

gcc
-g -o dynamic dynamic.o -ldl

dynamic/

|--
Makefile

|--
dynamic

|--
dynamic.c

`--
libtest.so

Makefile

分析


CC = gcc

CFLAGS =
-Wall -g -fPIC -I../lib

LDFLAGS =
-ldl

src =
dynamic.c

dir =
$(notdir $(src))

obj =
$(patsubst %.c,%.o,$(dir))

target =
dynamic

test:

@echo "--src------------"

@echo $(src)

@echo "--dir------------"

@echo $(dir)

@echo "--obj------------"

@echo $(obj)

all:$(target)

$(target):$(src)

$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:

rm -rf *.o

dynamic.c

分析


#include
<stdio.h>

#include
<dlfcn.h>

#include
<stdlib.h>

typedef
void (*simple_demo_funct)(void);

int main()

{

const char * error;

void * module;

simple_demo_funct demo_function;

module =
dlopen("libtest.so",RTLD_LAZY);

if(!module)

{

printf("Could not open
libtest.so:%s/n",dlerror());

exit(1);

}

dlerror();

demo_function = dlsym(module,
"test_a");

if((error = dlerror()) != NULL)

{

printf("Couldn't find
test_a:%s/n",error);

exit(1);

}

(*demo_function)();

dlclose(module);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: