C++封装dll和lib 供C++调用

头文件interface.h

cpp 复制代码
#pragma once
#ifndef INTERFACE_H
#define INTERFACE_H
#define _CRT_SECURE_NO_WARNINGS
#define FENGZHUANG_API _declspec(dllexport)
#include <string>
namespace FengZhuang {

	class FENGZHUANG_API IInterface {
	public:
	    static IInterface* CreateInterface();
		
		virtual void Init() = 0;

		virtual void Destroy() = 0;

		virtual const char* GetName() = 0;

		virtual void Log(int index, const std::string& windowName, bool includeDate,
			const char* format, ...) = 0;
	};
}
#endif

source.cpp文件:

cpp 复制代码
#include "interface.h"
#include <iostream>
#include <ctime>
//以下是log里面需要的头文件
#include <fstream> //打开写入std::ofstream file 需要的
#include <chrono> //std::chrono 获取时间需要的
#include <cstdarg>//va_start需要的头文件

namespace FengZhuang {

	class FHello : public IInterface {
	public:
		FHello();
		void Init() override;

		void Destroy() override;

		const char* GetName() override;

		void Log(int index, const std::string& windowName, bool includeDate,
			const char* format, ...) override;
	private:
		char name[1024];
	};

	FHello::FHello()
	{
		memset(name, 0, 1024);
		strcpy(name, "hello");
	}

	void FHello::Init()
	{
		printf("FHello::Init\n");
	}
	
	void FHello::Destroy()
	{
		printf("FHello::Destroy\n");
	}

	const char* FHello::GetName() {
		
		return "FHello";
	}

	void FHello::Log(int index, const std::string& windowName, bool includeDate,
		const char* format, ...) {
		std::string filename = std::to_string(index) + windowName + ".txt";
		// 打开文件以追加写入日志
		std::ofstream file(filename, std::ios::app);
		if (!file.is_open())
		{
			std::cerr << "无法打开日志文件:" << filename << std::endl;
			return;
		}
		// 获取时间戳

		auto now = std::chrono::system_clock::now();
		std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
		std::tm timeInfo;
#ifdef _WIN32
		localtime_s(&timeInfo, &currentTime);
#else
		localtime_r(&currentTime, &timeInfo);
#endif
		char timeBuffer[128];
		if (includeDate)
		{
			//std::strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %T", &timeInfo);//%T ==%H:%M:%S
			std::strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", &timeInfo);
		}
		else
		{
			//std::strftime(timeBuffer, sizeof(timeBuffer), "%T", &timeInfo);
			std::strftime(timeBuffer, sizeof(timeBuffer), "%H:%M:%S", &timeInfo);
		}

		// 格式化日志消息
		va_list args;
		va_start(args, format);
		char buffer[256];
		vsnprintf(buffer, sizeof(buffer), format, args);
		va_end(args);

		// 在控制台和文件中输出日志消息
		std::printf("%s %s", timeBuffer, buffer);
		file << timeBuffer << " " << buffer;

	}
	IInterface* IInterface::CreateInterface()
	{
		return new FHello();
	}
} // namespace FengZhuang

下面是自己项目内测试代码:

cpp 复制代码
int main() {
	FengZhuang::IInterface* IF = FengZhuang::IInterface::CreateInterface();
	IF->Destroy();
	IF->Log(1, "窗口1", 1, " %d行 窗口:%s x:%d \n", __LINE__, "窗口1", 123);
	delete IF;
}

其他C++使用者调用方法;

设置库需要3步

1.VC++目录-外部包含目录,添加外部库目录,就是库的头文件 #include "interface.h"的位置

2.VC++目录-库目录,

3.连接器--附加依赖项,就是lib库的名称

cpp 复制代码
#include "interface.h"
int main() {
    FengZhuang::IInterface* IF = FengZhuang::IInterface::CreateInterface();
    IF->Init();
    IF->Destroy();
    IF->Log(1,"窗口1",0," %d行 窗口:%s x:%d \n",__LINE__, "窗口1",123);
    delete IF;
    return 0;
}

成功调用!!这是C++可以调用的方式,还要写个C和易语言以及python可以调用的方式。

相关推荐
俄城杜小帅1 小时前
QML与C++交互的方式
开发语言·c++·交互
Vesan,3 小时前
无人机开发分享——基于行为树的无人机集群机载自主决策算法框架搭建及开发
c++·算法·决策树·无人机
R-G-B4 小时前
【24】C++实战篇——【 C++ 外部变量】 C++多个文件共用一个枚举变量,外部变量 extern,枚举外部变量 enum
c++·c++ 外部变量·c++文件共用一个枚举变量·外部变量 extern·枚举外部变量 enum
落羽的落羽4 小时前
【C++】哈希表原理与实现详解
数据结构·c++
快乐的划水a5 小时前
表驱动法-灵活编程范式
c++·链表
疯狂的Alex5 小时前
未来20年哪几种编程语言会保持优势?哪几种编程语言会得到更广泛的应用?
java·开发语言·c++·python·c#
爱吃生蚝的于勒5 小时前
一文学会c++继承 组合
java·c语言·开发语言·数据结构·c++·算法·蓝桥杯
愿天堂没有C++6 小时前
剑指offer第2版——面试题1:赋值运算符函数
c++·面试
程序猿编码9 小时前
二进制签名查找器(Aho-Corasick 自动机):设计思路与实现原理(C/C++代码实现)
c语言·c++·网络安全·二进制·逆向工程·ac自动机
R-G-B10 小时前
【35】C++实战篇—— string字符串与int、float数据合并成一个新的字符串
c++·string字符串与int·string字符串与float·string与int合并字符串·string与float合成串