[Setting]用VS2008将类封装为静态库library

原创文章,欢迎转载。转载请注明:转载自 祥的博客

原文链接:http://blog.csdn.net/humanking7/article/details/50726271


大家可能因为这样那样的原因,要将源代码封装为动态链接库(DLL)或静态链接库(Lib)。

准备工作

将你的类的声明和实现分离,h文件中只是类的声明cpp文件类的实现
我这个类是封装好了UDP。

  1. QfxClassUDP.h
  2. QfxClassUDP.cpp

准备的H文件


#ifndef _QFX_CLASS_UDP_H
#define _QFX_CLASS_UDP_H

#include <WinSock2.h> 
#pragma comment(lib,"ws2_32.lib")

//UDP Class write by Qfx 2016.2.9
//The code based on the Winsocket

//*********************************************************************************************************
//Part1.
//自定义UDP类接收和发送数据类型
//*********************************************************************************************************

//IPandPort
//该结构体用于存放IP地址和端口号
struct IPandPort
{
    char IP[20];
    unsigned short  Port;
};

//Receive Data Pack
//用于存放接收数据,使用union是便于接收和获取对应的数据
union DataReceive               
{
    char Buf[128];
    struct DataDetail
    {       
        double Test0_double;
        UINT8  Test1_u8;
        UINT8  Test2_u8[32];
        double Test3_double;
    }data;
};

//Receive Data Pack
//用于打包发送数据,使用union是便于接收和获取对应的数据
union DataSend              
{
    char Buf[128];
    struct DataDetail
    {
        double Test0_double;
        UINT8  Test1_u8;
        UINT8  Test2_u8[32];
        double Test3_double;
    }data;
};





//*********************************************************************************************************
//Part2.
//Class Declaration我的UDP类的声明
//*********************************************************************************************************
class QfxUDP
{
private:
    WSADATA         m_wsaData;  
    SOCKET          m_HostSocket;//本机对应Socket
public:
    char            m_HostIP[20];//本机绑定的IP地址
    unsigned short  m_HostPort; //本机绑定的端口号
    char            m_FromIP[20];//对方的IP地址
    unsigned short  m_FromPort;//对方的端口号

public:
    //************************************
    // Method:    初始化套接字
    // FullName:  InitSocket
    // Access:    public 
    // Returns:   BOOL [->] if success return "True", if fail return "False"
    // Qualifier: 
    // Parameter: unsigned short Port [->] Host Port for receive
    //************************************
    BOOL InitSocket(unsigned short Port);


    //************************************
    // Method:    获取本地IP地址
    // FullName:  GetHostIP
    // Access:    public 
    // Returns:   BOOL          [->] if success return TRUE. Otherwise return FALSE
    // Qualifier:
    // Parameter: char * HostIP [->] Host IP
    //************************************
    BOOL GetHostIP(char* HostIP);




    //************************************
    // Method:    关闭套接字
    // FullName:  DeletSocket
    // Access:    public 
    // Returns:   BOOL
    // Qualifier:
    //************************************
    BOOL DeletSocket(void);

    //************************************
    // Method:    UDP发送函数
    // FullName:  UDP_Send
    // Access:    public 
    // Returns:   void
    // Qualifier:
    // Parameter: char * IP         [->] 目标IP地址
    // Parameter: int Port          [->] 目标端口号
    // Parameter: char * buf        [->] 发送的数据
    // Parameter: int len           [->] 发送的数据长度
    // Parameter: int flags         [->] Indicator specifying the way in which the call is made
    //************************************
    void UDP_Send( char* IP, unsigned short Port, char* buf, int len, int flags = 0);


    //************************************
    // Method:    UDP接收函数
    // FullName:  UDP_Rece
    // Access:    public 
    // Returns:   int               [->] 成功后,返回接到的数据大小
    // Qualifier:
    // Parameter: char * buf        [->] 接收数据存储的地址
    // Parameter: int len           [->] 接收数据的大小
    // Parameter: int flags         [->] Indicator specifying the way in which the call is made
    //************************************
    int UDP_Rece( char* buf, int len, int flags = 0);


};



#endif

准备的CPP文件


#include <iostream>
using namespace std;
#include "QfxClassUDP.h"



BOOL QfxUDP::GetHostIP( char* HostIP )
{
    //代码实现
}

BOOL QfxUDP::InitSocket( unsigned short Port )
{
    //代码实现
}



BOOL QfxUDP::DeletSocket( void )
{
    //代码实现
}


void QfxUDP::UDP_Send( char* IP, unsigned short Port, char* buf, int len, int flags /*= 0*/ )
{
    //代码实现
}

int QfxUDP::UDP_Rece( char* buf, int len, int flags /*= 0*/ )
{
    //代码实现
}



VS2008配置

Step1 创建解决方案

pic1

pic2

pic3

Step2 添加代码,编译成Lib

pic4

pic5



调用Lib文件

两种方法

  1. 在在编译器中配置(但是我不喜欢,这样代码移植时候很蛋疼)
  2. 在代码中多写一句预编译代码(推荐)

代码如下:


#include <iostream>
using namespace std;
//包含库的头文件
#include "QfxClassUDP.h" 
//添加lib库
#pragma comment(lib,"ClassLibrary.lib") 

···
···

donate

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值