将Google protobuf lib静态链接到dll库 [英] Statically link google protobuf lib into a dll library
问题描述
在说明页上,用于安装和使用google protobuf库(安装Windows上的Protobuf )指出:
On the instructional page for installing and using the google protobuf library (Install Protobuf On Windows) it states:
如果您的项目本身是供第三方软件使用的DLL,则我们建议您不要在库的公共接口中公开协议缓冲区对象,并且将协议缓冲区静态链接到库中。
我想知道如何实现这一目标。据我所知,您可以通过两种方式构建Google protobuf:静态和动态。
I am wondering how this can be accomplished. As far as i know, you can build google protobuf in 2 ways: Statically and dynamically.
如果动态地构建它,将会面临上述问题。如果以静态方式构建它,那么您将在Visual Studio中使用多线程(/ MT)的Code生成类型。这意味着在我的dll库(使用多线程DLL(/ MD)构建的dll)中,您将收到以下链接器错误:
If you build it dynamiclly you will face the aformentioned problems. If you build it statically then you are using the Code generation type in Visual studio of Multi-threaded (/MT). Which means in my dll lib (where it is built with Multi-threaded DLL (/MD)) you will get the following linker error:
错误LNK2038:检测到 RuntimeLibrary的不匹配:Emulator.obj中的值 MTd_StaticDebug与值 MDd_DynamicDebug不匹配。obj
error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in Emulator.obj
现在有一些问题涉及如何解决此问题:
Now there are a few questions covering how to solve this:
- 错误LNK2038:检测到 _MSC_VER不匹配:值 1600没有在CppFile1.obj中匹配值'1700'
- 检测到 RuntimeLibrary不匹配
- (非常相似)在MSVC 2013上链接到protobuf 3时出错
- error LNK2038: mismatch detected for '_MSC_VER': value '1600' doesn't match value '1700' in CppFile1.obj
- Mismatch Detected for 'RuntimeLibrary'
- (Very similar) Errors when linking to protobuf 3 on MSVC 2013
但是答案通常是更改您的lib以匹配其他库的构建类型。问题是,我不想这样做,我想要一个DLL。而且我想按照其文档中的说明静态链接google protobuf。 我该如何实现?
But the answer is commonly, change your lib to match the build type of the other library. The issue is, I don't want to do that, I want a DLL. And i want to link google protobuf statically, as described in their documentation. How can i achieve this?
推荐答案
@MSalters指出,答案为此处代码生成的配置并不表示所构建的lib的类型,而是 c ++的类型使用的std lib
。为了在命令行构建中对此进行修改,要求您使用 -Dprotobuf_MSVC_STATIC_RUNTIME
开关(有关此参数的建议,信用来自@Ation答案此处)。为了将构建设置为使用多线程DLL(/ MD)) 专用于google protobuf ,从CMAKE生成makefile时,您必须执行以下操作:调试:
As pointed out by @MSalters, in the answer here the configuration of code generation does not indicate the type of lib that is built, but the type of c++ std lib
that is used. In order to modify this on the command line build, it is required that you use the -Dprotobuf_MSVC_STATIC_RUNTIME
switch (Credit for advice about this parameter comes from @Ation answer here). In order to set up the build to use Multi-threaded DLL (/MD)) specifically for google protobuf, when generating the makefiles from CMAKE you must do the following, for debug:
cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Debug -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ^
-DCMAKE_INSTALL_PREFIX=../../../install/debug ^
../..
或发布:
cmake -G "NMake Makefiles" ^
-DCMAKE_BUILD_TYPE=Release -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ^
-DCMAKE_INSTALL_PREFIX=../../../install/release ^
../..
以下 nmake
命令生成的代码将具有多线程DLL(/ MD)类型的代码生成)。
The code generated by the following nmake
command will have code generation of type Multi-threaded DLL (/MD)).
这篇关于将Google protobuf lib静态链接到dll库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!