2025年3月25日 星期二 甲辰(龙)年 月廿四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

windows 静态库的创建和使用

时间:08-15来源:作者:点击数:48

VC6.0做法:

File--新建---工程--win32 static-library 输入工程名和位置,即可进入静态库界面。

以16进制转10进制为例

建立C程序或C++库文件。对于库文件最好用C写,可移植性更强。

  • #include<stdio.h>
  • #include<string.h>
  • #include<math.h>
  • /*计算字符串是否有四个字节*/
  • int GetLeghtofHEX(char *p)
  • {
  • int count=0;
  • char *temp =p;
  • while(*temp++!='\0')
  • count++;
  • return count ;
  • }
  • /*如果不足四个字节,前4-count位填0*/
  • char *modifbit(char *p)
  • {
  • char tab[5];
  • int i=0,j=0;
  • char * temp;
  • int count=GetLeghtofHEX(p);
  • if (count!=4)
  • {
  • for(i=0;i<4-count;i++)
  • tab[i]='0';
  • while(i<4)
  • {
  • tab[i]=*(p+j);
  • j++;
  • i++;
  • }
  • tab[i]='\0';
  • temp = tab;
  • }
  • else
  • temp=p;
  • return temp;
  • }
  • int Hexstoinit(char *p)
  • {
  • int i,j=0;
  • char string[5]="0000";
  • int Dec=0,temp;
  • char *tempoint=p;
  • char *temp2;
  • if(strstr(p,"0x"))
  • {
  • tempoint=strstr(p,"0x")+2;
  • }
  • printf("string=%s\n",tempoint);
  • temp2=modifbit(tempoint);
  • //printf("count=%s\n",temp2);
  • tempoint=temp2;
  • for(i=3;i>=0;i--)
  • {
  • string[j++]=*(tempoint+i);
  • }
  • string[4]='\0' ;
  • printf("string1=%s\n",string);
  • for(j=0;j<4;j++)
  • {
  • if(string[j]<='9')
  • {
  • temp=string[j]-'0';
  • }
  • else if(string[j]=='a'||string[j]=='A')
  • temp=10;
  • else if(string[j]=='b'||string[j]=='B')
  • temp=11;
  • else if(string[j]=='c'||string[j]=='C')
  • temp=12;
  • else if(string[j]=='d'||string[j]=='D')
  • temp=13;
  • else if(string[j]=='e'||string[j]=='E')
  • temp=14;
  • else if(string[j]=='f'||string[j]=='F')
  • temp=15;
  • Dec+=temp*pow(16,j);
  • }
  • printf("string=%d\n",Dec);
  • return Dec;
  • }

并将C实现添加到工程。建立头文件,并添加到工程

  • #ifndef __HEXTOINT_H
  • #define __HEXTOINT_H
  • int GetLeghtofHEX(char *p);
  • char *modifbit(char *p);
  • int Hexstoinit(char *p);
  • #endif

头文件就是对外的接口,提供给对外使用的手册

编译后,在Debug目录下就产生了Hextoint.lib静态库。

静态库使用方法:

文件---新建---win32 application

创建C应用代码,并添加到头文件

  • #include<StdAfx.h>
  • #include "Hextoint.h"
  • #pragma comment(lib, "Hextoint.lib") //程序里面声明静态库的路径和位置。静态库放在当前目录下,否则放在其他目录下,就要使用文件路径+文件名
  • #include<stdio.h>
  • int main(int argc, char* argv[])
  • {
  • printf("The Hextoint is %d",Hexstoinit("0xFF"));
  • return 0;
  • }

将头文件,放在当前目录下,并添加到工程

  • #ifndef __HEXTOINT_H
  • #define __HEXTOINT_H
  • int GetLeghtofHEX(char *p);
  • char *modifbit(char *p);
  • int Hexstoinit(char *p);
  • #endif

VC2008 做法类似

创建一个静态库

文件--新建---工程---win32 project---根据向导选择win32 static library

然后project--add new item 添加头文件和C文件

编译后,在Debug目录下就产生了Hextoint.lib静态库。

使用静态库

文件--新建---工程---win32 project---根据向导选择win32 application

然后project--add new item 添加头文件和C文件

编译后,链接,执行即可

  • // Clibtest.cpp : Defines the entry point for the console application.
  • //
  • #include "stdafx.h"
  • #include "..\Hextoint.h"
  • #include <stdio.h>
  • #pragma comment(lib, "..\\Clibstatic.lib") /*库的路径必需使用\\而不是\\*/
  • int _tmain(int argc, _TCHAR* argv[])
  • {
  • printf("hello,This is %d\n",Hexstoinit("0xff"));
  • return 0;
  • }
  • #ifndef __HEXTOINT_H
  • #define __HEXTOINT_H
  • int GetLeghtofHEX(char *p);
  • char *modifbit(char *p);
  • int Hexstoinit(char *p);
  • #endif

在程序里面进行库链接只是其中方法之一,也可以修改编译器中库引用路径进行链接,为了方便,通常还是使用

  • #pragma comment(lib, "..\\Clibstatic.lib")

来引用库。

通常我们用C来做库,当然,也可以用C++做库

以C++建立库为例子

文件--新建---工程---win32 project---根据向导选择win32 static library

然后project--add class 和 add new item添加C++文件和头文件

  • #include "StdAfx.h"
  • #include "Hextoint.h"
  • #include<stdio.h>
  • #include<string.h>
  • #include<math.h>
  • Hextoint::Hextoint(void)
  • {
  • }
  • Hextoint::~Hextoint(void)
  • {
  • }
  • int Hextoint:: GetLeghtofHEX(char *p)
  • {
  • int count=0;
  • char *temp =p;
  • while(*temp++!='\0')
  • count++;
  • return count ;
  • }
  • char * Hextoint::modifbit(char *p)
  • {
  • char tab[5];
  • int i=0,j=0;
  • char * temp;
  • int count=GetLeghtofHEX(p);
  • if (count!=4)
  • {
  • for(i=0;i<4-count;i++)
  • tab[i]='0';
  • while(i<4)
  • {
  • tab[i]=*(p+j);
  • j++;
  • i++;
  • }
  • tab[i]='\0';
  • temp = tab;
  • }
  • else
  • temp=p;
  • return temp;
  • }
  • int Hextoint::Hexstoinit(char *p)
  • {
  • int i,j=0;
  • char string[5]="0000";
  • float Dec=0,temp;
  • char *tempoint=p;
  • char *temp2;
  • if(strstr(p,"0x"))
  • {
  • tempoint=strstr(p,"0x")+2;
  • }
  • printf("string=%s\n",tempoint);
  • temp2=modifbit(tempoint);
  • //printf("count=%s\n",temp2);
  • tempoint=temp2;
  • for(i=3;i>=0;i--)
  • {
  • string[j++]=*(tempoint+i);
  • }
  • string[4]='\0' ;
  • printf("string1=%s\n",string);
  • for(j=0;j<4;j++)
  • {
  • if(string[j]<='9')
  • {
  • temp=string[j]-'0';
  • }
  • else if(string[j]=='a'||string[j]=='A')
  • temp=10;
  • else if(string[j]=='b'||string[j]=='B')
  • temp=11;
  • else if(string[j]=='c'||string[j]=='C')
  • temp=12;
  • else if(string[j]=='d'||string[j]=='D')
  • temp=13;
  • else if(string[j]=='e'||string[j]=='E')
  • temp=14;
  • else if(string[j]=='f'||string[j]=='F')
  • temp=15;
  • Dec += temp*pow(16.0,j);
  • }
  • printf("string=%d\n",Dec);
  • return Dec;
  • }
  • #pragma once
  • class Hextoint
  • {
  • public:
  • Hextoint(void);
  • ~Hextoint(void);
  • int GetLeghtofHEX(char *p);
  • char * modifbit(char *p);
  • int Hexstoinit(char *p);
  • };

这样在Debug 目录下就会生成: .lib静态库文件

使用库方法:

文件--新建---工程---win32 project---根据向导选择win32 application

然后project--add new item 添加头文件和C文件

  • // testllib.cpp : Defines the entry point for the console application.
  • //
  • #include "stdafx.h"
  • #include "Hextoint.h"
  • #pragma comment(lib, "staticlib.lib") //lib库放在工程的当前目录下
  • int _tmain(int argc, _TCHAR* argv[])
  • {
  • Hextoint a;
  • printf("The Hextoint is %d",a.Hexstoinit("0xFF"));
  • return 0;
  • }

注意:静态库引用,就是在编译将静态库放在工程目录下,和APP,直接编译成一个.exe, 所以这个.exe 是包括APP+lib,很大的镜像,直接执行就可以了,便于程序发布。

如果不用#pragma comment指定,则可以直接在VC++中设置,如图2,依次选择tools、options、directories、library files菜单或选项,填入库文件路径。图2中加红圈的部分为我们添加的libTest.lib文件的路径。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门