现在的位置: 首页 > 综合 > 正文

在Linux环境下编写和使用静态函数库

2014年03月06日 ⁄ 综合 ⁄ 共 3916字 ⁄ 字号 评论关闭

在libsth.h中声明函数定义:

/*

 * libsth.h

 * Declarations for simple error-handling library

 */

#ifndef _LIBSTH_H

#define _LIBSTH_H

 

#include "stdarg.h"

 

/*

 * ADD

 */

int ADD(int a, int b);

 

/*

 * SUBSTRACT

 */

int SUBSTRACT(int a, int b);

 

/*

 * TIMES

 */

int TIMES(int a, int b);

 

/*

 * DIVIDE

 */

float DIVIDE(int a, int b);

#endif

 

 

在libsth.C中实现libsth.h中定义的函数:

/*

 * libsth.C

 * Implementation of the functions defined in libsth.h

 */

 

#include "stdarg.h"

#include "stdlib.h"

#include "stdio.h"

#include "string.h"

#include "libsth.h"

 

int ADD(int a, int b)

{

         return a + b;

}

 

int SUBSTRACT(int a, int b)

{

         return a - b;

}

 

int TIMES(int a, int b)

{

         return a * b;

}

 

float DIVIDE(int a, int b)

{

         return (float)((float)a / (float)b);

}

 

 

在teststh.C中使用函数库(libsth.a)中的函数

/*

 * teststh.C

 * Testing program for libsth library

 */

#include "stdio.h"

#include "stdlib.h"

#include "libsth.h"

#include "iostream.h"

 

int main(void)

{

         int a = ADD(1, 2);

         int b = SUBSTRACT(2, 1);

         int c = TIMES(1, 2);

         float d = DIVIDE(3, 2);

         cout << a << "  " << b << "  " << c << "  " << d << endl;

         //printf("%d  %d  %d  %f/n", a, b, c, d);

         exit(EXIT_SUCCESS);

}

 

下面是c++版的makefile:

teststh:teststh.o

         g++ teststh.o -static -L. -lsth -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3 -lstdc++ -o teststh

teststh.o:teststh.C

         g++ -c teststh.C -Wno-deprecated -o teststh.o

libsth.a:libsth.o

         ar rcs libsth.a libsth.o

libsth.o:libsth.C libsth.h

         g++ -c libsth.C -o libsth.o

all:libsth.a teststh

clean:

         rm -f *.o *.a teststh

 

说明:

1、  -L. -lsth -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3 -lstdc++显示地使用了两个静态库,即libsth.a和libstdc++.a,因为使用g++ -static编译时它不会自动搜索到这个库,因此必须用 –L选项显式指定;

2、            -Wno-deprecated是为了屏蔽在用g++ make程序时产生的一些警告信息,这些警告信息认为“iostream.h”中所定义的某些函数是过时的,但是还可以用;

3、 ar rcs libsth.a libsth.o是使用ar命令的rcs选项将libsth.o打包成libsth.a

 

下面是c版的makefile:

teststh:teststh.o

         gcc teststh.o -static -L. -lsth -o teststh

teststh.o:teststh.c libsth.h

         gcc -c teststh.c -o teststh.o

libsth.a:libsth.o

         ar rcs libsth.a libsth.o

libsth.o:libsth.C libsth.h

         g++ -c libsth.C -o libsth.o

all:libsth.a teststh

clean:

         rm -f *.o *.a teststh

在libsth.h中声明函数定义:

/*

 * libsth.h

 * Declarations for simple error-handling library

 */

#ifndef _LIBSTH_H

#define _LIBSTH_H

 

#include "stdarg.h"

 

/*

 * ADD

 */

int ADD(int a, int b);

 

/*

 * SUBSTRACT

 */

int SUBSTRACT(int a, int b);

 

/*

 * TIMES

 */

int TIMES(int a, int b);

 

/*

 * DIVIDE

 */

float DIVIDE(int a, int b);

#endif

 

 

在libsth.C中实现libsth.h中定义的函数:

/*

 * libsth.C

 * Implementation of the functions defined in libsth.h

 */

 

#include "stdarg.h"

#include "stdlib.h"

#include "stdio.h"

#include "string.h"

#include "libsth.h"

 

int ADD(int a, int b)

{

         return a + b;

}

 

int SUBSTRACT(int a, int b)

{

         return a - b;

}

 

int TIMES(int a, int b)

{

         return a * b;

}

 

float DIVIDE(int a, int b)

{

         return (float)((float)a / (float)b);

}

 

 

在teststh.C中使用函数库(libsth.a)中的函数

/*

 * teststh.C

 * Testing program for libsth library

 */

#include "stdio.h"

#include "stdlib.h"

#include "libsth.h"

#include "iostream.h"

 

int main(void)

{

         int a = ADD(1, 2);

         int b = SUBSTRACT(2, 1);

         int c = TIMES(1, 2);

         float d = DIVIDE(3, 2);

         cout << a << "  " << b << "  " << c << "  " << d << endl;

         //printf("%d  %d  %d  %f/n", a, b, c, d);

         exit(EXIT_SUCCESS);

}

 

下面是c++版的makefile:

teststh:teststh.o

         g++ teststh.o -static -L. -lsth -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3 -lstdc++ -o teststh

teststh.o:teststh.C

         g++ -c teststh.C -Wno-deprecated -o teststh.o

libsth.a:libsth.o

         ar rcs libsth.a libsth.o

libsth.o:libsth.C libsth.h

         g++ -c libsth.C -o libsth.o

all:libsth.a teststh

clean:

         rm -f *.o *.a teststh

 

说明:

1、  -L. -lsth -L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3 -lstdc++显示地使用了两个静态库,即libsth.a和libstdc++.a,因为使用g++ -static编译时它不会自动搜索到这个库,因此必须用 –L选项显式指定;

2、            -Wno-deprecated是为了屏蔽在用g++ make程序时产生的一些警告信息,这些警告信息认为“iostream.h”中所定义的某些函数是过时的,但是还可以用;

3、 ar rcs libsth.a libsth.o是使用ar命令的rcs选项将libsth.o打包成libsth.a

 

下面是c版的makefile:

teststh:teststh.o

         gcc teststh.o -static -L. -lsth -o teststh

teststh.o:teststh.c libsth.h

         gcc -c teststh.c -o teststh.o

libsth.a:libsth.o

         ar rcs libsth.a libsth.o

libsth.o:libsth.C libsth.h

         g++ -c libsth.C -o libsth.o

all:libsth.a teststh

clean:

         rm -f *.o *.a teststh

抱歉!评论已关闭.