c++ 类文件的动态库生成及调用例子

首先,创建一个简单的类,类头文件的名称与项目工程的名称应该一致,
控制台项目工程名:testClass,要导出的类文件名称也应为:testClass.h,这样创建后会生成名称一致的testClass.lib,testClass.dll.

#ifndef TEST_CLASS_H_
#define TEST_CLASS_H_
#include <iostream>
#include <string>

using namespace std;
#ifdef SERVERDLL_EXPORTS
#define SERVERDLL_API __declspec(dllexport)
#else
#define SERVERDLL_API __declspec(dllimport)
#endif
class SERVERDLL_API TestClass
{
public:
    virtual void VirtualFunction(const string& model_file, const string& trained_file, const string& mean_file);
    void NormalFunction(void);
};
SERVERDLL_API void func(void);

#endif
#include "testClass.h"
void TestClass::VirtualFunction(const string& model_file, const string& trained_file, const string& mean_file)
{
    std::cout << "modelfile=" << model_file << std::endl;
    std::cout << "modelfile=" << trained_file << std::endl;
    std::cout << "modelfile=" << mean_file << std::endl;

}
void TestClass::NormalFunction(void)
{
    std::cout << "this is NormalFunction()!" << std::endl;
    return;
}
void func(void)
{
    cout << "xixihaha" << endl;
    return;
}

右键属性-》配置属性=》常规=》配置类型选为(动态库(.dll)),目标文件扩展名:.dll。
预处理定义加上:SERVERDLL_EXPORTS
选择:Relase x64 或者Debug x64平台。调用的项目也应该与生成动态库的平台保持一致。点击生成即可得到testClass.lib,testClass.dll.
第二部分:dll的调用。
新建一个测试工程如testClassdll,加入头文件testClass.h

#ifndef TEST_CLASS_H_
#define TEST_CLASS_H_
#include <iostream>
#include <string>
using namespace std;
#ifdef SERVERDLL_EXPORTS
#define SERVERDLL_API __declspec(dllexport)
#else
#define SERVERDLL_API __declspec(dllimport)
#endif
class SERVERDLL_API TestClass
{
public:
    virtual void VirtualFunction(const string& model_file, const string& trained_file, const string& mean_file);

    void NormalFunction(void);
};
SERVERDLL_API void func(void);

#endif

main.cpp代码如下:

#include "testClass.h"
#include <iostream>
//#include <Windows.h>
using namespace std;
#pragma comment(lib,"F:\\VcProject\\2017\\testClassdll\\testClassdll\\testClass.lib")

int main()
{
    TestClass object;
    std::string model_file("C:\\Caffedllcpu\\MultiClassificationChen\\model\\deploy.prototxt");
    std::string trained_file("C:\\Caffedllcpu\\MultiClassificationChen\\model\\Train.caffemodel");
    std::string mean_file("C:\\Caffedllcpu\\MultiClassificationChen\\model\\MeanFile.binaryproto");

    object.NormalFunction();
    object.VirtualFunction(model_file, trained_file, mean_file);
    func();


    return 0;

}

连接器输入:testClass.lib

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

josiechen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值