linux连接库编译过程分析

一、 静态链接库编译

基本编译过程

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;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值