如何生成自己的静态库(lib)文件

       打开VS2010,建立一个win32控制台程序,然后打开项目--Tv属性(如图1),打开Tv属性页(如图2),在右边配置类型处选择静态库(lib)。

然后我们就可以写我们的头文件和cpp源文件了。写完后编译下就可以在目录下找到相应的lib文件了。





在这里我首先写一个相对复杂的库文件,头文件内容是:

  1. //tv.h
  2. #ifndef TV_H_
  3. #define TV_H_


  4. class Tv
  5. {
  6. public:
  7. friend class Remote; //可以访问Tv类的私有成员
  8. enum{Off,On};
  9. enum{MinVal,MaxVal=20};
  10. enum{Antenna,Cable};
  11. enum{TV,VCR};


  12. Tv(int s=Off,int mc=100):state(s),volume(5),maxchannel(mc),channel(2),mode(Cable),input(TV){}
  13. void onoff(){state=(state==Off)? On:Off;}
  14. bool ison()const {return state==On;}
  15. bool volup();
  16. bool voldown();
  17. void chanup();
  18. void chandown();
  19. void set_mode(){mode=(mode==Antenna)? Cable:Antenna;}
  20. void set_input(){input=(input==TV)? VCR:TV;}
  21. void settings()const;
  22. private:
  23. int state;
  24. int volume;
  25. int maxchannel;
  26. int channel;
  27. int mode;
  28. int input;


  29. };


  30. class Remote
  31. {
  32. private:
  33. int mode;
  34. public:
  35. Remote(int m=Tv::TV):mode(m){}
  36. bool volup(Tv &t){return t.volup();}
  37. bool voldown(Tv &t){return t.voldown();}
  38. void onoff(Tv &t){t.onoff();}
  39. void chanup(Tv &t){t.chanup();}
  40. void chandown(Tv &t){t.chandown();}
  41. void set_chan(Tv &t,int c){t.channel=c;}
  42. void set_mode(Tv &t){t.set_mode();}
  43. void set_input(Tv &t){t.set_input();}
  44. };


  45. #endif

源文件是:
  1. //tv.cpp
  2. #include <iostream>
  3. #include "tv.h"


  4. bool Tv::volup()
  5. {
  6. if (volume<MaxVal)
  7. {
  8. volume++;
  9. return true;
  10. }
  11. else
  12. return false;
  13. }


  14. bool Tv::voldown()
  15. {
  16. if (volume>MinVal)
  17. {
  18. volume--;
  19. return true;
  20. }
  21. else 
  22. return false;
  23. }


  24. void Tv::chanup()
  25. {
  26. if (channel<maxchannel)
  27. channel++;
  28. else
  29. channel=1;
  30. }


  31. void Tv::chandown()
  32. {
  33. if(channel>1)
  34. channel--;
  35. else
  36. channel=maxchannel;
  37. }


  38. void Tv::settings()const
  39. {
  40. using std::cout;
  41. using std::endl;
  42. cout<<"TV is "<<(state==Off ? "Off":"On")<<endl;
  43. if (state==On)
  44. {
  45. cout<<"Volume setting ="<<volume<<endl;
  46. cout<<"Channel setting ="<<channel<<endl;
  47. cout<<"Mode ="<<(mode==Antenna ? "Antenna":"Cable")<<endl;
  48. cout<<"Input ="<<(input==TV ? "TV":"VCR")<<endl;
  49. }
  50. }

编译后即可生成tv.lib(其中tv是项目的名称)。



既然我们生成了自己的库文件,那么我们怎么利用我们的库文件呢?

使用方法跟我们用其他的库文件方法是一样的,都需要三个步骤:

1.包含必要的头文件。 2.链接相应的库文件。 3.使用库文件

比如我在另外一个项目中要使用我刚写的tv.lib库文件,为了方便,我可以把tv.h头文件和tv.lib库文件复制我新建项目的目录下。


然后写如下文件:

  1. //use_tv.cpp
  2. #include <iostream>
  3. #include "tv.h"
  4. #pragma comment(lib,"tv.lib")


  5. int main()
  6. {
  7. using std::cout;
  8. Tv s27;
  9. cout<<"Initial settings for 27\" TV:\n";
  10. s27.settings();
  11. s27.onoff();
  12. s27.chanup();
  13. cout<<"\nAdjusted settings for 27\" TV:\n";
  14. s27.settings();


  15. Remote grey;


  16. grey.set_chan(s27,10);
  17. grey.volup(s27);
  18. grey.volup(s27);
  19. cout<<"\n27\" settings after using remote:\n";
  20. s27.settings();


  21. Tv s32(Tv::On);
  22. s32.set_mode();
  23. grey.set_chan(s32,28);
  24. cout<<"\n32\" settings:\n";
  25. s32.settings();


  26. return 0;
  27. }

输出结果为:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值