[英]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.cmake
和libprotobuf-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
),但它们可以被更改,目前只被视为一个示例配置:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.