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

相关推荐
rjszcb23 分钟前
一文说完c++全部基础知识,IO流(二)
c++
小字节,大梦想1 小时前
【C++】二叉搜索树
数据结构·c++
吾名招财1 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn
我是哈哈hh2 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
憧憬成为原神糕手2 小时前
c++_ 多态
开发语言·c++
郭二哈2 小时前
C++——模板进阶、继承
java·服务器·c++
挥剑决浮云 -2 小时前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
丶Darling.2 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5202 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
小柯J桑_3 小时前
C++:STL简介
c++·stl