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可以调用的方式。

相关推荐
莹莹学编程—成长记41 分钟前
string的模拟实现
服务器·c++·算法
喵先生!4 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
xMathematics5 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
yuanManGan7 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
梁下轻语的秋缘7 小时前
每日c/c++题 备战蓝桥杯(P1049 [NOIP 2001 普及组] 装箱问题)
c语言·c++·学习·蓝桥杯
逐光沧海7 小时前
STL常用算法——C++
开发语言·c++
wuqingshun3141597 小时前
蓝桥杯 5. 交换瓶子
数据结构·c++·算法·职场和发展·蓝桥杯
球求了8 小时前
C++:继承机制详解
开发语言·c++·学习
超爱笑嘻嘻8 小时前
shared_ptr八股收集 C++
c++
我想进大厂9 小时前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论