打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
window 静态库的创建和使用

VC6.0做法:

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

以16进制转10进制为例

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

  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<math.h>  
  4. /*计算字符串是否有四个字节*/  
  5. int GetLeghtofHEX(char *p)  
  6. {  
  7.     int count=0;  
  8.     char *temp =p;  
  9.     while(*temp++!='\0')  
  10.         count++;  
  11.     return count ;  
  12. }  
  13. /*如果不足四个字节,前4-count位填0*/  
  14. char *modifbit(char *p)  
  15. {     
  16.     char tab[5];  
  17.     int i=0,j=0;  
  18.     char * temp;  
  19.     int count=GetLeghtofHEX(p);  
  20.     if (count!=4)  
  21.     {  
  22.         for(i=0;i<4-count;i++)  
  23.             tab[i]='0';  
  24.         while(i<4)  
  25.         {  
  26.             tab[i]=*(p+j);  
  27.             j++;  
  28.             i++;  
  29.         }  
  30.         tab[i]='\0';  
  31.         temp = tab;  
  32.           
  33.     }  
  34.     else  
  35.         temp=p;  
  36.     return temp;  
  37. }  
  38.   
  39. int Hexstoinit(char *p)  
  40. {  
  41.     int i,j=0;  
  42.     char string[5]="0000";  
  43.     int Dec=0,temp;  
  44.     char *tempoint=p;  
  45.     char *temp2;  
  46.     if(strstr(p,"0x"))  
  47.     {  
  48.         tempoint=strstr(p,"0x")+2;  
  49.     }  
  50.     printf("string=%s\n",tempoint);  
  51.     temp2=modifbit(tempoint);  
  52.     //printf("count=%s\n",temp2);  
  53.     tempoint=temp2;  
  54.     for(i=3;i>=0;i--)  
  55.     {  
  56.         string[j++]=*(tempoint+i);  
  57.     }  
  58.     string[4]='\0' ;  
  59.       
  60.     printf("string1=%s\n",string);  
  61.     for(j=0;j<4;j++)  
  62.     {  
  63.         if(string[j]<='9')  
  64.         {  
  65.             temp=string[j]-'0';  
  66.         }  
  67.         else if(string[j]=='a'||string[j]=='A')  
  68.             temp=10;  
  69.         else if(string[j]=='b'||string[j]=='B')  
  70.             temp=11;  
  71.         else if(string[j]=='c'||string[j]=='C')  
  72.             temp=12;  
  73.         else if(string[j]=='d'||string[j]=='D')  
  74.             temp=13;  
  75.         else if(string[j]=='e'||string[j]=='E')  
  76.             temp=14;  
  77.         else if(string[j]=='f'||string[j]=='F')  
  78.             temp=15;  
  79.           
  80.         Dec+=temp*pow(16,j);  
  81.     }  
  82.     printf("string=%d\n",Dec);  
  83.     return Dec;  
  84. }  


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

  1. #ifndef  __HEXTOINT_H  
  2. #define  __HEXTOINT_H  
  3. int GetLeghtofHEX(char *p);  
  4. char *modifbit(char *p);  
  5. int Hexstoinit(char *p);  
  6. #endif  

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

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



静态库使用方法:

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

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

  1. #include<StdAfx.h>  
  2. #include "Hextoint.h"  
  3. #pragma comment(lib, "Hextoint.lib") //程序里面声明静态库的路径和位置。静态库放在当前目录下,否则放在其他目录下,就要使用文件路径+文件名  
  4. #include<stdio.h>  
  5. int main(int argc, char* argv[])  
  6. {  
  7.    printf("The Hextoint is %d",Hexstoinit("0xFF"));  
  8.     return 0;  
  9. }  
将头文件,放在当前目录下,并添加到工程
  1. #ifndef  __HEXTOINT_H  
  2. #define  __HEXTOINT_H  
  3. int GetLeghtofHEX(char *p);  
  4. char *modifbit(char *p);  
  5. int Hexstoinit(char *p);  
  6. #endif  

VC2008  做法类似

创建一个静态库

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

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

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


使用静态库

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

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

编译后,链接,执行即可

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


在程序里面进行库链接只是其中方法之一,也可以修改编译器中库引用路径进行链接,为了方便,通常还是使用
  1. #pragma comment(lib, "..\\Clibstatic.lib")   
来引用库。

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

以C++建立库为例子

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

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


  1. #include "StdAfx.h"  
  2. #include "Hextoint.h"  
  3. #include<stdio.h>  
  4. #include<string.h>  
  5. #include<math.h>  
  6. Hextoint::Hextoint(void)  
  7. {  
  8. }  
  9.   
  10. Hextoint::~Hextoint(void)  
  11. {  
  12. }  
  13. int  Hextoint:: GetLeghtofHEX(char *p)  
  14. {  
  15.     int count=0;  
  16.     char *temp =p;  
  17.     while(*temp++!='\0')  
  18.         count++;  
  19.     return count ;  
  20. }  
  21. char * Hextoint::modifbit(char *p)  
  22. {     
  23.     char tab[5];  
  24.     int i=0,j=0;  
  25.     char * temp;  
  26.     int count=GetLeghtofHEX(p);  
  27.     if (count!=4)  
  28.     {  
  29.         for(i=0;i<4-count;i++)  
  30.             tab[i]='0';  
  31.         while(i<4)  
  32.         {  
  33.             tab[i]=*(p+j);  
  34.             j++;  
  35.             i++;  
  36.         }  
  37.         tab[i]='\0';  
  38.         temp = tab;  
  39.   
  40.     }  
  41.     else  
  42.         temp=p;  
  43.     return temp;  
  44. }  
  45.   
  46. int Hextoint::Hexstoinit(char *p)  
  47. {  
  48.     int i,j=0;  
  49.     char string[5]="0000";  
  50.     float Dec=0,temp;  
  51.     char *tempoint=p;  
  52.     char *temp2;  
  53.     if(strstr(p,"0x"))  
  54.     {  
  55.         tempoint=strstr(p,"0x")+2;  
  56.     }  
  57.     printf("string=%s\n",tempoint);  
  58.     temp2=modifbit(tempoint);  
  59.     //printf("count=%s\n",temp2);  
  60.     tempoint=temp2;  
  61.     for(i=3;i>=0;i--)  
  62.     {  
  63.         string[j++]=*(tempoint+i);  
  64.     }  
  65.     string[4]='\0' ;  
  66.   
  67.     printf("string1=%s\n",string);  
  68.     for(j=0;j<4;j++)  
  69.     {  
  70.         if(string[j]<='9')  
  71.         {  
  72.             temp=string[j]-'0';  
  73.         }  
  74.         else if(string[j]=='a'||string[j]=='A')  
  75.             temp=10;  
  76.         else if(string[j]=='b'||string[j]=='B')  
  77.             temp=11;  
  78.         else if(string[j]=='c'||string[j]=='C')  
  79.             temp=12;  
  80.         else if(string[j]=='d'||string[j]=='D')  
  81.             temp=13;  
  82.         else if(string[j]=='e'||string[j]=='E')  
  83.             temp=14;  
  84.         else if(string[j]=='f'||string[j]=='F')  
  85.             temp=15;  
  86.   
  87.         Dec += temp*pow(16.0,j);  
  88.     }  
  89.     printf("string=%d\n",Dec);  
  90.     return Dec;  
  91. }  

  1. #pragma once  
  2.   
  3. class Hextoint  
  4. {  
  5. public:  
  6.     Hextoint(void);  
  7.     ~Hextoint(void);  
  8.     int  GetLeghtofHEX(char *p);  
  9.     char * modifbit(char *p);  
  10.     int Hexstoinit(char *p);  
  11. };  


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


使用库方法:

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

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

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


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



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


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
《C语言程序设计》第三版课后答案
c++字符串赋值
以单词为最小单位翻转字符串
指针数组和数组指针
C语言的命令行参数
WinPcap 常见安装和运行错误
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服