2011年3月30日星期三

在Linux下如何创建自己的函数库

想建个函数库,把自己常用的一些函数包括进去,在使用时include一下就行了,应该怎么构建?另外,我看到常用的一些函数库,如math.h,里面只有一些函数的声明,那么执行部分在哪里?又是如何实现调用的?初学编程,谢谢各位了! 

我自己以前写的一点东西.可能对你有用吧! 

如何为Linux增加库

一. 静态库
在Linux下的静态库是以.a为后缀的文件。
1. 建静态库
h1.c 源文件
#include <stdio.h>
void hello1()
{
printf(“the first hello!\n”);
}
h2.c 源文件
#include <stdio.h>
void hello2()
{
printf(“the second hello!\n”);
}
2.主程序
hello.c 源文件
int main()
{
hello1();
hello2();
return 0;
}
输入命令:
gcc –c h1.c
gcc –c h2.c
ar –r libhello.a h1.o h2.o
ar –s libhello.a
ranlib libhello.a
最后再
gcc –static hello.c –L. –lhello –o hello即可生成可执行文件。注意要使用-static参数,否则生成的仍然是动态类型的文件,不过对于hello这个库则是采用静态方式来使用的而已。

二. 动态库
1.建动态库
#include <stdio.h>
void hello1()
{
printf(“the first hello!\n”);
}
h2.c 源文件
#include <stdio.h>
void hello2()
{
printf(“the second hello!\n”);
}
2. 主程序
hello.c 源文件
int main()
{
hello1();
hello2();
return 0;
}
输入命令:
gcc –fPIC –g –c h1.c –o libh1.o
gcc –fPIC –g –c h2.c –o libh2.o
gcc –g –shared –W1 –o libh1.so libh1.o –lc
gcc –g –shared –W1 –o libh2.so libh2.o –lc
然后再将主程序与库相连进行编译
gcc –g hello.c –o hello –L. –lh1 –lh2
最后再将当前路径放到库查找路径中去
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATY
最后再执行./hello即可运行。 

三. 动态库的查找过程
一旦连接器完成了自己的初始化工作,就查找程序所需要的库的名字。程序头中有一个指针指向dynamic段,它包含了动态连接的信息,dynamic段中的DT_STRTAB指向一个字符表,而其中的DT_NEEDED包含了一个相对于字符表的偏移,指向所需要的库名。
对于每一个库,连接器首先查找库文件位置,从本质上说是一个相当复杂的过程。DT_NEEDED所描叙的库文件名一般类似libXt.so.6 (Xt开发包, 版本6),库文件可能在任意的库文件目录中,还也可能有重名的文件。在我的系统中,这个库的实际文件名是/usr/X11R6/lib/libXt.so.6.0,最后的“.0”表示次版本号。

连接器查找下列几个地方:

l 首先查看 .dynamic 段是否包含了一个叫DT_RPATH的项(它是一个以冒号分隔的库文件搜索目录列表)。这个项是在程序被连接器连接时,由命令行开关或者环境变量添加上去的。它常应用于子系统中,比如像数据库应用,我们要装载一些程序集合以及支持库到一个目录中去的时候。

l 查看是否存在环境变量 LD_LIBRARY_PATH(它是一个以冒号分隔的库文件搜索目录列表)。这个项可以帮助开发者建立一个新版本的库,把他的路径添加到LD_LIBRARY_PATH中,把它和现存的可连接程序一同使用,用来测试新的库,

l 连接器查看库高速缓存文件 /etc/ld.so.conf ,它包含了库名和路径的一个对应列表,如果库名存在,连接器就使用它对应的路径,用这个查找方法能够找到大部分的库(文件名不需要和要求完全符合,这点可以参考接下来的“库的版本”)。

l 如果上叙的查找都失败,连接器就查找默认路径 /usr/lib ,如果库文件依旧没有找到,则显示一个错误然后退出。

连接器找到了库文件后,先打开它,然后读取ELF头,找到指向各个段的指针。连接器为库的代码段和数据段分配空间并映射到内存,随后是bss(不分配空间)。.通过库的 .dynamic 段,连接器添加这个库的符号表到符号表链,如果库所依赖的其它库没有装载的话,则添加那个库到装载队列中。

完成这个过程后,所有的库都已经被映射,loader在逻辑上拥有了一个全局的符号表,它是全部程序和被映射库的符号表的联合。

2010年12月1日星期三

enum类型的本质

自从C语言开始enum类型就被作为用户自定义分类有限集合常量的方法被引入到了语言
当中,而且一度成为C++中定义编译期常量的唯一方法(后来在类中引入了静态整型常量)。
    根据上面对enum类型的描述,到底enum所定义出来的类型是一个什么样的类型呢?作为
一个用户自定义的类型其所占用的内存空间是多少呢?使用enum类型是否真的能够起到有限
集合常量的边界约束呢?大家可能都知道enum类型和int类型具有隐示(自动)转换的规则,
那么是否真的在任何地方都可以使用enum类型的变量来代替int类型的变量呢?下面会逐一
回答这些问题。
    1. 到底enum所定义出来的类型是一个什么样的类型呢?
       在C++中大家都知道仅仅有两种大的类型分类:POD类型和类类型(不清楚的可以参
       见我的其他文章)。enum所定义的类型其实属于POD类型,也就是说它会参与到POD
       类型的隐示转换规则当中去,所以才会出现enum类型与int类型之间的隐示转换现象。
       那么也就是说enum所定义的类型不具备名字空间限定能力(因为不属于类类型),
       其所定义的常量子具备和enum类型所在名字空间相同的可见性,由于自身没有名字
       限定能力,所以会出现名字冲突现象。如:
           struct CEType
           {
               enum EType1 { e1, e2 };
               enum EType2 { e1, e2 };
           };
       上面的例子会出现e1、e2名字冲突编译时错误,原因就在于枚举子(e1、e2)是
       CEType名字空间中的名字,同样在引用该CEType中的枚举子时必须采用CEType::e1
       这样的方式进行,而不是CEType::EType1::e1来进行引用。
    2. 作为一个用户自定义的类型其所占用的内存空间是多少呢?
       该问题就是sizeof( EType1 )等于多少的问题,是不是每一个用户自定义的枚举类
       型都具有相同的尺寸呢?在大多数的32位编译器下(如:VC++、gcc等)一个枚举类
       型的尺寸其实就是一个sizeof( int )的大小,难道枚举类型的尺寸真的就应该是int
       类型的尺寸吗?其实不是这样的,在C++标准文档(ISO14882)中并没有这样来定义,
       标准中是这样说明的:“枚举类型的尺寸是以能够容纳最大枚举子的值的整数的尺寸”,
       同时标准中也说名了:“枚举类型中的枚举子的值必须要能够用一个int类型表述”,
       也就是说,枚举类型的尺寸不能够超过int类型的尺寸,但是是不是必须和int类型
       具有相同的尺寸呢?上面的标准已经说得很清楚了,只要能够容纳最大的枚举子的
       值的整数就可以了,那么就是说可以是char、short和int。例如:
           enum EType1 { e1 = CHAR_MAX };
           enum EType2 { e2 = SHRT_MAX };
           enum EType3 { e3 = INT_MAX  };
       上面的三个枚举类型分别可以用char、short、int的内存空间进行表示,也就是:
           sizeof( EType1 ) == sizeof( char  );
           sizeof( EType2 ) == sizeof( short );
           sizeof( EType3 ) == sizeof( int   );
       那为什么在32位的编译器下都会将上面三个枚举类型的尺寸编译成int类型的尺寸呢?
       主要是从32位数据内存对其方面的要求进行考虑的,在某些计算机硬件环境下具有对
       齐的强制性要求(如:sun SPARC),有些则是因为采用一个完整的32位字长CPU处理
       效率非常高的原因(如:IA32)。所以不可以简单的假设枚举类型的尺寸就是int类
       型的尺寸,说不定会遇到一个编译器为了节约内存而采用上面的处理策略。
    3. 使用enum类型是否真的能够起到有限集合常量的边界约束呢?
       首先看一下下面这个例子:
           enum EType { e1 = 0, e2 };
           void func1( EType e )
           {
               if ( e == e1 )
               {
                   // do something
               }
               // do something because e != e1 must e == e2
           }
           void func2( EType e )
           {
               if ( e == e1 )
               {
                   // do something
               }
               else if ( e == e2 )
               {
                   // do something
               }
           }
           
           func1( static_cast<EType>( 2  ) );
           func2( static_cast<EType>( -1 ) );
       上面的代码应该很清楚的说明了这样一种异常的情况了,在使用一个操出范围的整
       型值调用func1函数时会导致函数采取不该采取的行为,而第二个函数可能会好一些
       他仅仅是忽略了超出范围的值。这就说明枚举所定义的类型并不是一个真正强类型
       的有限常量集合,这样一种条件下和将上述的两个函数参数声明成为整数类型没有
       任何差异。所以以后要注意标准定义中枚举类型的陷阱。(其实只有类类型才是真
       正的强类型)
       
    4. 是否真的在任何地方都可以使用enum类型的变量来代替int类型的变量呢?
       通过上面的讨论,其实枚举类型的变量和整型变量具有了太多的一致性和可互换性,
       那么是不是在每一个可以使用int类型的地方都可以很好的用枚举类型来替代呢?
       其实也不是这样的,毕竟枚举类型是一个在编译时可区分的类型,同时第2点的分析
       枚举类型不一定和int类型具有相同的尺寸,这两个差异就决定了在某些场合是不可
       以使用枚举类型来代替int类型的。如:
           第一种情况:
               enum EType { e1 = 0, e2, e3 };
               EType val;
               std::cin >> val;
           第二种情况:
               enum EType { e1 = 0, e2, e3 };
               EType val;
               std::scanf( "%d", &val );
       上面的两种情况看是基本上属于同一种类型的问题,其实不然。第一种情况会导致
       编译时错误,会因为std::cin没有定义对应的枚举类型的重载>>运算符而出错,这
       就说明枚举类型是一种独立和鉴别的类型;而第二种情况不会有任何编译时问题,
       但是可能会导致scanf函数栈被破坏而使得程序运行非法,为什么会这样呢?上面
       已经分析过了枚举类型变量的尺寸不一定和int类型相同,这样一来我们采用%d就
       是说将枚举类型变量val当作4字节的int变量来看待并进行参数压栈,而在某些编
       译器下sizeof( val )等于1字节,这样scanf函数就会将val变量地址中的后续的三
       字节地址也压入栈中,并对其进行赋值,也许val变量后续的三个字节的地址没有
       特殊含义可以被改写(比如是字节对齐的空地址空间),可能会认为他不会出现错
       误,其实不然,在scanf函数调用结束后会进行栈清理,这样一来会导致scanf函数
       清理了过多的地址空间,从而破坏了外围函数的栈指针的指向,从而必然会导致程
       序运行时错误。

    由上面的说明枚举类型有那么多的缺点,那我们怎样才能够有一个类型安全的枚举类型
呢?其实可以采用类类型来模拟枚举类型的有限常量集合的概念,同时得到类型安全的好处,
具体参见后续的文章。   

2010年10月18日星期一

131 Linux Error Codes for C Programming Language using errno

Programmers should handle all kinds of errors to protect the program from failure.

In C programming language, there is no direct support for error handling. You have to detect the failure and handle the error. In C programming language, return values represents success or failure. Inside a C program, when a function fails, you should handle the errors accordingly, or at least record the errors in a log file.

When you are running some program on Linux environment, you might notice that it gives some error number. For example, “Error no is : 17″, which doesn’t really say much. You really need to know what error number 17 means.

This article shows all available error numbers along with it descriptions. This article might be a handy reference for you, when you encounter an error number and you would like to know what it means.

  • In C programming language, there is an external variable called “errno”.
  • From this errno variable you can use some error handling functions to find out the error description and handle it appropriately.
  • You have to include errno.h header file to use external variable errno.
  • perror function prints error description in standard error.
  • The strerror function returns a string describing the error code passed in the argument errnum.

The following C code snippet tries to open a file through open system call. There are two flags in the open call. O_CREAT flag is to create a file, if the file does not exist. O_EXCL flag is used with O_CREAT, if the file is already exist open call will fail with the proper error number.

 $ cat fileopen.c #include <stdio.h> #include <fcntl.h> #include <errno.h> #include <string.h> main() { // Declaration of a file descriptor int fd;  // Opening a file fd = open("/root/sasikala/testing",O_CREAT|O_EXCL); // If Open is failed if ( fd < 0 ) {         printf("Opening file : Failed\n");         printf ("Error no is : %d\n", errno);         printf("Error description is : %s\n",strerror(errno)); } // If Open is success else         printf("Opening file : Success\n");  }  $ cc -o fileopen fileopen.c $ ./fileopen  Opening file : Success  $ ./fileopen Opening file : Failed Error no is : 17 Error description is : File exists

At first execution, open got executed successfully, and it created the file since the file was not available. In next execution, it throws an error number 17, which is “File already exist”.

The following table shows list of error numbers and its descriptions in Linux operation system

ERROR CODE TABLE
Error number Error Code Error Description
1 EPERM Operation not permitted
2 ENOENT No such file or directory
3 ESRCH No such process
4 EINTR Interrupted system call
5 EIO I/O error
6 ENXIO No such device or address
7 E2BIG Argument list too long
8 ENOEXEC Exec format error
9 EBADF Bad file number
10 ECHILD No child processes
11 EAGAIN Try again
12 ENOMEM Out of memory
13 EACCES Permission denied
14 EFAULT Bad address
15 ENOTBLK Block device required
16 EBUSY Device or resource busy
17 EEXIST File exists
18 EXDEV Cross-device link
19 ENODEV No such device
20 ENOTDIR Not a directory
21 EISDIR Is a directory
22 EINVAL Invalid argument
23 ENFILE File table overflow
24 EMFILE Too many open files
25 ENOTTY Not a typewriter
26 ETXTBSY Text file busy
27 EFBIG File too large
28 ENOSPC No space left on device
29 ESPIPE Illegal seek
30 EROFS Read-only file system
31 EMLINK Too many links
32 EPIPE Broken pipe
33 EDOM Math argument out of domain of func
34 ERANGE Math result not representable
35 EDEADLK Resource deadlock would occur
36 ENAMETOOLONG File name too long
37 ENOLCK No record locks available
38 ENOSYS Function not implemented
39 ENOTEMPTY Directory not empty
40 ELOOP Too many symbolic links encountered
42 ENOMSG No message of desired type
43 EIDRM Identifier removed
44 ECHRNG Channel number out of range
45 EL2NSYNC Level 2 not synchronized
46 EL3HLT Level 3 halted
47 EL3RST Level 3 reset
48 ELNRNG Link number out of range
49 EUNATCH Protocol driver not attached
50 ENOCSI No CSI structure available
51 EL2HLT Level 2 halted
52 EBADE Invalid exchange
53 EBADR Invalid request descriptor
54 EXFULL Exchange full
55 ENOANO No anode
56 EBADRQC Invalid request code
57 EBADSLT Invalid slot
59 EBFONT Bad font file format
60 ENOSTR Device not a stream
61 ENODATA No data available
62 ETIME Timer expired
63 ENOSR Out of streams resources
64 ENONET Machine is not on the network
65 ENOPKG Package not installed
66 EREMOTE Object is remote
67 ENOLINK Link has been severed
68 EADV Advertise error
69 ESRMNT Srmount error
70 ECOMM Communication error on send
71 EPROTO Protocol error
72 EMULTIHOP Multihop attempted
73 EDOTDOT RFS specific error
74 EBADMSG Not a data message
75 EOVERFLOW Value too large for defined data type
76 ENOTUNIQ Name not unique on network
77 EBADFD File descriptor in bad state
78 EREMCHG Remote address changed
79 ELIBACC Can not access a needed shared library
80 ELIBBAD Accessing a corrupted shared library
81 ELIBSCN .lib section in a.out corrupted
82 ELIBMAX Attempting to link in too many shared libraries
83 ELIBEXEC Cannot exec a shared library directly
84 EILSEQ Illegal byte sequence
85 ERESTART Interrupted system call should be restarted
86 ESTRPIPE Streams pipe error
87 EUSERS Too many users
88 ENOTSOCK Socket operation on non-socket
89 EDESTADDRREQ Destination address required
90 EMSGSIZE Message too long
91 EPROTOTYPE Protocol wrong type for socket
92 ENOPROTOOPT Protocol not available
93 EPROTONOSUPPORT Protocol not supported
94 ESOCKTNOSUPPORT Socket type not supported
95 EOPNOTSUPP Operation not supported on transport endpoint
96 EPFNOSUPPORT Protocol family not supported
97 EAFNOSUPPORT Address family not supported by protocol
98 EADDRINUSE Address already in use
99 EADDRNOTAVAIL Cannot assign requested address
100 ENETDOWN Network is down
101 ENETUNREACH Network is unreachable
102 ENETRESET Network dropped connection because of reset
103 ECONNABORTED Software caused connection abort
104 ECONNRESET Connection reset by peer
105 ENOBUFS No buffer space available
106 EISCONN Transport endpoint is already connected
107 ENOTCONN Transport endpoint is not connected
108 ESHUTDOWN Cannot send after transport endpoint shutdown
109 ETOOMANYREFS Too many references: cannot splice
110 ETIMEDOUT Connection timed out
111 ECONNREFUSED Connection refused
112 EHOSTDOWN Host is down
113 EHOSTUNREACH No route to host
114 EALREADY Operation already in progress
115 EINPROGRESS Operation now in progress
116 ESTALE Stale NFS file handle
117 EUCLEAN Structure needs cleaning
118 ENOTNAM Not a XENIX named type file
119 ENAVAIL No XENIX semaphores available
120 EISNAM Is a named type file
121 EREMOTEIO Remote I/O error
122 EDQUOT Quota exceeded
123 ENOMEDIUM No medium found
124 EMEDIUMTYPE Wrong medium type
125 ECANCELED Operation Canceled
126 ENOKEY Required key not available
127 EKEYEXPIRED Key has expired
128 EKEYREVOKED Key has been revoked
129 EKEYREJECTED Key was rejected by service
130 EOWNERDEAD Owner died
131 ENOTRECOVERABLE State not recoverable

When you see an error number thrown by a C program on a Linux environment, you might find the above table handy to identify what those error number means. Make sure to bookmark this article for future reference.