在Windows,Visual Studio 2013的cmake中将Google Protobuf与库一起使用 [英] Using google protobuf with libraries in cmake on Windows, Visual Studio 2013
问题描述
我有一个CMake项目,该项目带有几个子目录和库模块,这些子目录和库模块最终内置在客户端和服务器库中,这些库可用于可执行文件以对其进行测试。我正在尝试在其中一个库中使用Google的protobuf库。我正在按如下方式创建子库:
I have a CMake project with several sub-directories and library modules that eventually get built into client and server libraries, and those libraries are used from executable to test them. I am trying to use Google's protobuf library inside one of those libraries. I am creating my sub-library as follows:
include_directories(
.
../rendering_backend
../shared
${MY_THIRDPARTY_DIR}/protobuf/include
)
add_library(Client STATIC
client.cpp
client.hpp
../common.hpp
../shared/protobufs/my_bufs.pb.cc
../shared/protobufs/my_bufs.pb.h
)
target_link_libraries(Client
RenderingBackend
${MY_THIRDPARTY_DIR}/protobuf/lib/libprotobufd.lib
)
可执行文件声明如下:
add_executable(TEST_Client
client_tester.cpp
../src/rendering_backend/rendering_backend.hpp)
target_link_libraries(TEST_Client Client)
所有这些文件似乎都正确生成,但是在我构建时,出现以下错误(以及许多类似的错误):
All these files seem to be generating properly, but when I build, I get the following error (and many just like it):
libprotobufd.lib(zero_copy_stream_impl.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in client_tester.obj
我回到了protobuf解决方案,并验证了所有项目在其运行时库设置中均设置为使用/ MDd。它是根据 https://github.com/google/protobuf/tree/上的指南生成的master / cmake 。
I have gone back to the protobuf solution and verified that all projects are set to use /MDd in their Runtime Library setting. It was generated following the guide at https://github.com/google/protobuf/tree/master/cmake.
我见过很多有关protobuf的问题和答案,但似乎没有一个能解决这个问题。我不知道在Visual Studio中将所有内容都设置为/ MDd时,可能会尝试构建/ MTd。
I have seen many questions and answers about protobufs, but none seem to address this problem. I don't know what could be trying to build /MTd when everything is set to /MDd inside Visual Studio.
假设如果我这样做,此过程将起作用正确地,我只能假设我做错了什么。
Assuming that this process would work if I were doing it correctly, I can only assume I'm doing something wrong. What is the intended way to do something like this?
我试图在此处使用protobuf 3.0.0。
I was trying to use protobuf 3.0.0 here.
推荐答案
为了使 find_package
标识正确的路径,必须编辑 CMAKE_PREFIX_PATH
变量来搜索库。假设您已正确设置 THIRDPARTY_DIR
并下载了protobuf的发行版(撰写本文时为2.6.1),则此代码可使其正确找到路径:
In order for find_package
to identify the correct paths, you must edit the CMAKE_PREFIX_PATH
variable to search for libraries. This code allows it to find the path correctly, assuming that you have correctly set up THIRDPARTY_DIR
and you downloaded the release version of protobuf (2.6.1 as of this writing):
set(CMAKE_PREFIX_PATH
${CMAKE_PREFIX_PATH}
${THIRDPARTY_DIR}/protobuf-2.6.1
)
Protobuf3也可以使用,但是除了在 CMAKE_PREFIX_PATH $ c $中设置安装路径外c>,还必须向
find_package
提供 CONFIG
参数,如下所示:
Protobuf3 can also be used but in addition to setting the install path in CMAKE_PREFIX_PATH
, you must also supply the CONFIG
argument to find_package
as follows:
find_package(Protobuf CONFIG REQUIRED)
...假设需要Protobuf。
...assuming Protobuf is required.
就我而言,我将Protobuf 3.0.0 beta 2配置为安装到文件夹 install中。在其自己的目录中:
In my case, I configured Protobuf 3.0.0 beta 2 to install to a folder "install" within its own directory:
set(CMAKE_PREFIX_PATH
${CMAKE_PREFIX_PATH}
${THIRDPARTY_DIR}/protobuf-3.0.0-beta-2/install
)
find_package(Protobuf CONFIG REQUIRED)
(感谢tamas.kenez对此进行了纠正。)
请注意,我必须在 find_package(Protobuf )
会返回无错误。我最初只构建调试库。
Note that I had to build the release library before find_package(Protobuf)
would return without errors. I originally only build the debug libraries.
我还必须注意构建哪个MSVC运行时。动态似乎是大多数项目的默认设置,但是Protobuf3会使用静态运行时进行构建,除非您将其配置为其他方式(如果使用GUI,则有一个复选框)。将静态运行时库链接到动态运行时可执行文件会导致各种链接器问题和重新定义,因此请注意这一点。
I also had to be cautious of which MSVC runtime I build with. Dynamic seems to be the default for most projects, but Protobuf3 builds with the static runtime unless you configure it to do otherwise (there's a checkbox if you're using the GUI). Linking static runtime libraries to dynamic runtime executables causes all kinds of linker problems and redefinitions, so be careful of that.
最后,CMake GUI似乎保留了有关目录结构的某些信息,直到您重新加载目录结构为止。项目(或可能完全重启程序);因此,即使在清除整个构建目录后,当CMake继续查找我已删除的目录结构时,我还是试图更改我使用的protobuf版本时遇到了麻烦。 (通过清除生成目录并重新启动CMake GUI并重新加载基础项目来解决。)
Lastly, CMake GUI seems to retain some information about directory structures until you reload the project (or possibly restart the program entirely); so I hit a snag in trying to change which version of protobuf I used when CMake kept finding a directory structure I had already deleted, even after clearing the whole build directory. (Solved by clearing the build directory and relaunching CMake GUI and reloading the base project.)
这篇关于在Windows,Visual Studio 2013的cmake中将Google Protobuf与库一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!