C++ 静态库/动态库生成

C++ 生成静态库 无需 导出头

C++ 生成动态库 需要导出头,这样引用动态库时才能找到对应的方法,否则找不到该接口

1)MSVC编译器提供了一系列C/C++的扩展来指定符号的导入导出,即__declspec属性关键字。

__declspec(dllexport) 表示该符号是从本DLL导出的符号

__declspec(dllimport) 表示该符号是从别的DLL中导入的

1.类的导出方式,在生成目录下选择 生成动态库,同时在预处理里面加上 FORWARD_EXPORTS 的宏定义。(定义类时在类的前面加 FORWARD_DLL 关键字)

//类的动态导出方式

#pragma once

#ifdef FORWARD_EXPORTS
#define FORWARD_DLL __declspec(dllexport)
#else
#define FORWARD_DLL __declspec(dllimport)
#endif

#include <opencv2opencv.hpp>
#include <string>
#include <unordered_map>
#include <vector>

namespace caffe {
	struct DataBlob {
		const float* data;
		std::vector<int> size;
		std::string name;
	};
	class FORWARD_DLL FORWARD{
	public:
		FORWARD();
		int AddNet(std::string prototxt_path, std::string weights_path, int gpu_id = 0);
		std::unordered_map<std::string, DataBlob> Forward(int net_id);
        ...
		~FORWARD();
	};
}

2.命名空间内函数的导出方式于此相似,只是在所有的函数前加 FORWARD_DLL 关键字。

经验分享 程序员 微信小程序 职场和发展