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;
}
静态链接库编译
基本编译过程
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;
}
相关文章推荐
- Arm Linux交叉编译和连接过程分析(1)
- Arm Linux交叉编译和连接过程分析(2)
- Linux 2.6 内核编译及过程分析
- 对编译连接过程的近距离分析
- 《C编译原理》ubuntu下helloword编译连接过程分析
- gcc编译过程、C语言编译过程分析、环境变量设置、linux文件夹结构和用途介绍、常用文件和目录的操作命令、文件类型
- Red Hat 9 Linux下编译sqlite-3.3.8并在QT3.1下连接全过程详细记录
- ARM Linux编译链接过程分析
- ARM Linux编译链接过程分析
- Linux 学习笔记_13_2_LAMP环境编译(下) --编译过程及分析
- Linux 2.6 内核编译及过程分析
- linux 静态库的编译和连接过程遇到的问题
- Linux内核编译过程分析
- 代码的编译、连接与执行过程分析
- Linux程序编译执行原理之一:预处理-编译-汇编-链接过程分析
- linux编译及启动过程分析
- Linux socket编程入门及客户端服务器端通信实现 – 提高篇:TCP连接过程分析
- 基于x86体系结构分析linux-2.6.26内核编译过程
- Linux下进程的创建过程分析(_do_fork/do_fork详解)--Linux进程的管理与调度(八)
- 交叉编译场景分析(arm-linux)(五)--编译libjpeg和libpng