简体   繁体   English

在CMake中使用protobuf作为ExternalProject

[英]Using protobuf in CMake as ExternalProject

Originally I had a copy of protobuf in VCS with which I completely disagree. 最初我在VCS中有一份protobuf副本,我完全不同意。 I would like to use protobuf as an external dependency to my project. 我想使用protobuf作为我的项目的外部依赖。

Only libprotobuf.cmake and libprotobuf-lite.cmake are required for the library. 库中只需要libprotobuf.cmakelibprotobuf-lite.cmake These files can be included in a custom CMakeLists.txt like this: 这些文件可以包含在自定义CMakeLists.txt如下所示:

cmake_minimum_required(VERSION 2.8.12)

project(protobuf C CXX)
# set protobuf_source_dir
# set protobuf_SHARED_OR_STATIC to "SHARED" or "STATIC"
include_directories(${protobuf_source_dir}/src)

include(${protobuf_source_dir}/cmake/libprotobuf-lite.cmake)
include(${protobuf_source_dir}/cmake/libprotobuf.cmake)

I've heard about CMake's ExternalProject , but CMake complains that it can't find any CMakeLists.txt in that project. 我听说过CMake的ExternalProject ,但是CMake抱怨它在该项目中找不到任何CMakeLists.txt

CMake's ExternalProject assumes that a CMakeLists.txt exists in the root directory. CMake的ExternalProject假定根目录中存在CMakeLists.txt However, protobuf as of version 3.4 has some examples and a CMakeLists.txt in folder cmake . 但是, 3.4版本的protobuf在文件夹cmake有一些示例和CMakeLists.txt

An example usage without modifying the external library: 不修改外部库的示例用法:

set(PROTOBUF_TAR_GZ https://github.com/google/protobuf/archive/v3.4.0.tar.gz)

ExternalProject_Add(
  protobuf-external
  PREFIX protobuf
  URL ${PROTOBUF_TAR_GZ}
  BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/protobuf
  CMAKE_CACHE_ARGS
    "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
    "-Dprotobuf_BUILD_TESTS:BOOL=OFF"
    "-Dprotobuf_BUILD_EXAMPLES:BOOL=OFF"
    "-Dprotobuf_WITH_ZLIB:BOOL=OFF"
    "-DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}"
    # other project specific parameters
  SOURCE_SUBDIR cmake
  BUILD_ALWAYS 1
  STEP_TARGETS build
  INSTALL_COMMAND ""
)

EXternalProject_Get_Property(protobuf-external source_dir)
include_directories(${source_dir}/src)
link_directories(${CMAKE_CURRENT_BINARY_DIR}/protobuf)

The important part is to set SOURCE_SUBDIR to folder cmake . 重要的是将SOURCE_SUBDIR设置为文件夹cmake

Anyway I made the following assumptions (reflected in CMAKE_CACHE_ARGS ), but they can be changed and currently perceived just as an example configuration: 无论如何,我做了以下假设(反映在CMAKE_CACHE_ARGS ),但它们可以被更改,目前只被视为一个示例配置:

  • Setting build type according to the project. 根据项目设置构建类型。 Debug may be useful when inspecting inherited messages. 在检查继承的消息时, 调试可能很有用。 I'd prefer Release for external libraries. 我更喜欢Release for external libraries。
  • I don't want to test protobuf. 我不想测试protobuf。
  • I don't need examples and compressed messages. 我不需要示例和压缩消息。
  • Optionally allow setting the compiler from the parent project if cross-compiling. (可选)允许在交叉编译时从父项目中设置编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM