linux下动态库与静态库
体验一下linux下编写和使用动态库与静态库,范例:helloworld程序。
首先编写静态库:
hellos.h
#ifndef _HELLO_S_H
#define _HELLO_S_H
void prints(char *str);
#endif
hellos.c
#include "hellos.h"
#include <stdio.h>
void prints(char *str)
{
printf("print in static way:%s",str);
}
开始编译成静态库:
gcc -c -o hellos.o hellos.c
ar cqs libhellos.a hellos.o
main.c
#include "hellos.h"
int main(void)
{
char *text = "hello,world\n";
prints(text);
}
使用静态库编译:gcc -o hello main.c -static -L. -lhellos
然后运行hello,输出:
print in static way: Hello World!
删除
libhellos.a和
hellos.*后, 程序仍然正常运行。
编写动态库
:
hellod.h
#ifndef _HELLO_D_H
#define _HELLO_D_H
void printd(char *str);
#endif
hellod.c
#include "hellod.h"
#include <stdio.h>
void printd(char *str)
{
printf("print in dynamic way:%s",str);
}
编译生成动态库:gcc -shared -o libhellod.so hellod.c
这样,libhellod.so就是生成的动态库。
export LD_LIBRARY_PATH=/root/program/link/dynamic/:$LD_LIBRARY_PATH,指定库文件路径。
如果系统装了SELINUX的话,要执行chcon -t textrel_shlib_t /root/program/link/dynamic/libhellod.so改变权限。
main.c
#include "hellod.h"
int main(void)
{
char *text = "hello,world\n";
printd(text);
}
gcc -o hello main.c -L./ -lhellod
然后运行hello可以看到输出
print in dynamic way: Hello World!
如果删除库文件,会出现:./hello: error while loading shared libraries: libhellod.so: cannot open shared object file: No such file or directory
相关文档:
简而言之,产生段错误就是访问了错误的内存段,一般是你没有权限,或者根本就不存在对应的物理内存,尤其常见的是访问0地址.
一般来说,
段错误就是指访问的内存超出了系统所给这个程序的内存空间,通常这个值是由gdtr来保存的,他是一个48位的寄存器,其中的32位是保存由它指向的
gdt表,后13位保存相应于gdt的下标,最后3 ......
sed 编辑指令的格式如下 :
sed option [address1[,address2]]function[argument] ***.c
位址参数 address1/2 为行数或匹配字串 , 表示所执行编辑的行 ;
函数参 数 function[argument] 为 sed 的内定函数 , 表示执行的编辑动作。
匹配字串:(reguler expression 字串)
& : 代表其前 pattern 字串
例:s ......
最开始时,ram芯片中包含的是随机数据。当开始启动时,cpu的一个引脚上会产生一个reset逻辑值。此后处理器的一些寄存器设置成固定的数值,并执行在物理地址0xfffffff0处找到的代码。硬件把这个地址映射到某个只读、持久的存储芯片中,该芯片通常称为rom(read-only memory只读内存)。rom所存放的程序集在80x86体系中通常叫做 ......
由于Linux不是中国人开发的,开发过程中也不是以中文为母语,虽有中文语言包,在软件兼容性上出现中文乱码也在所难免。我抽取网络上前人的经验,结合自己的理解,将他们加以总结,希望对后来者有所帮助。
1、html文件乱码;
&nbs ......
能被独立调度的每个执行上下文都必须拥有自己的进程描述符。进程和进程描述符之间有严格的对应关系,使用32位进程描述符地址标识,进程描述符指针指向这些地址,内核对进程的大部分引用是通过进程描述符指针进行的。
可以使用pid(进程标识符)来标记进程,存放在字段pid中,PID被顺序编号,新创建的进程PID通常是前一个 ......