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

头文件interface.h

#pragma once
#ifndef INTERFACE_H
#define INTERFACE_H
#define _CRT_SECURE_NO_WARNINGS
#define FENGZHUANG_API _declspec(dllexport)
#include 
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文件:

#include "interface.h"
#include 
#include 
//以下是log里面需要的头文件
#include  //打开写入std::ofstream file 需要的
#include  //std::chrono 获取时间需要的
#include //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

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

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库的名称

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

你可能感兴趣的:(C++/VC++,C++,库的调用,c++)