生成使用协议缓冲区的DLL [英] Generate a DLL which uses protocol-buffers

查看:74
本文介绍了生成使用协议缓冲区的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用我创建的protobuf类型的C ++(VS2017)项目.

I am working on a C++ (VS2017) project that uses a protobuf type I created.

但是,此项目需要上述protobuf类型的 .dll .默认情况下,每个类声明中都不存在 __ declspec(dllexport),我在线阅读了可以通过使用以下命令行生成protobuf对象来添加它们的方法:

However, this project requires a .dll of said protobuf type. The __declspec( dllexport ) in each class declaration are not there by default, and I read online that they can be added by generating the protobuf object with this command line:

--cpp_out=dllexport_decl=MY_EXPORT_MACRO:output/directory

没有人解释什么是 MY_EXPORT_MACRO 或如何定义它.当我第一次生成protobuf对象时,我使用了最基本的行,并且行得通:

No one has explained what MY_EXPORT_MACRO is or how to define it. When I first generated my protobuf objects I used the most basic line and it worked:

protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/my_file.proto

MY_EXPORT_MACRO 是什么,在哪里和/或还有其他方法可以使我的protobuf文件 .dll 兼容?

What and where is MY_EXPORT_MACRO and/or is there another way to make my protobuf files .dll compatible?

推荐答案

您也了解 __ declspec(dllimport),对吗?在构建DLL(带有 dllexport 批注)和DLL客户端(带有 dllimport 批注)时使用相同类型定义的最简单方法是什么?

You know about __declspec( dllimport ) also, correct? What's the easiest way to use the same type definition while building the DLL (with dllexport annotations) and in DLL clients (with dllimport annotations)?

在各种Win32 DLL开发中,不仅对于protobuf DLL,具有宏来切换批注是一种极为普遍的做法.

Having a macro to switch the annotation is an extremely common practice in Win32 DLL development of all sorts, not just for protobuf DLLs.

通常,定义的运行方式如下:

Usually the definition runs like this:

#if BUILD_DLLX
#  define DLLX_API __declspec(dllexport)
#else
#  pragma comment('lib', 'dllx.lib')
#  define DLLX_API __declspec(dllimport)
#endif

然后使用-cpp_out = dllexport_decl = DLLX_API:$ DST_DIR ,以便在生成的头文件中将 DLLX_API 插入正确的位置.然后使用/DBUILD_DLLX 构建DLL,以便导出类型和函数.

And then you would use --cpp_out=dllexport_decl=DLLX_API:$DST_DIR so that the generated header files have DLLX_API inserted in the right places. Then build the DLL with /DBUILD_DLLX so it exports the types and functions.

DLL的使用者可以 #include 完全相同的头文件,并且在其项目配置中没有/DBUILD_DLLX 时,它们最终将导入.

Consumers of the DLL can #include the exact same header file, and with no /DBUILD_DLLX in their project configuration, they'll end up with imports.

这篇关于生成使用协议缓冲区的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆