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 小时前
【Linux网络编程】:URL(encode),HTTP协议,telnet工具
linux·服务器·网络·c++·网络协议·http
_周游1 小时前
【数据结构】_栈的结构与实现
开发语言·数据结构·c++
和风化雨2 小时前
排序算法--计数排序
c语言·数据结构·c++·算法·排序算法
我命由我123452 小时前
游戏引擎 Unity - Unity 打开项目、Unity Editor 添加简体中文语言包模块、Unity 项目设置为简体中文
c语言·开发语言·c++·unity·ue5·c#·游戏引擎
辰尘_星启2 小时前
vscode+CMake+Debug实现 及权限不足等诸多问题汇总
c++·ide·vscode·编辑器
m0_748238423 小时前
C++ 学习:深入理解 Linux 系统中的冯诺依曼架构
linux·c++·学习
Lenyiin4 小时前
2848、与车相交的点
c++·算法·leetcode·1024程序员节
mljy.4 小时前
优选算法《前缀和》
c++·算法
天堂的恶魔9464 小时前
C++六大默认成员函数
c++
Pafey4 小时前
实现一个 LRU 风格的缓存类
c++·缓存