Skip to content

Instantly share code, notes, and snippets.

@TheBurnDoc
Created October 21, 2013 11:17
Show Gist options
  • Save TheBurnDoc/7082233 to your computer and use it in GitHub Desktop.
Save TheBurnDoc/7082233 to your computer and use it in GitHub Desktop.
This Gist demonstrates how to integrate the compilation of Protocol Buffer files into your CMake project
# Protobuf setup, these should be set to your project's directories, and currently assumes
# that everything is relative to CMAKE_SOURCE_DIR (the directory CMake is run from)
set (PROTO_IN_DIR ${CMAKE_SOURCE_DIR}/proto)
set (PROTO_OUT_DIR ${CMAKE_SOURCE_DIR}/build/proto)
set (PROTOC_BIN ${CMAKE_SOURCE_DIR}/thirdparty/protobuf/bin/protoc)
set (PROTOC_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/thirdparty/protobuf/include)
set (PROTOC_LIB_DIR ${CMAKE_SOURCE_DIR}/thirdparty/protobuf/lib
# Protobuf files
set (PROTO_FILES
${PROTO_DIR}/common.proto
${PROTO_DIR}/packet.proto
${PROTO_DIR}/rpc.proto)
# Protobuf output files
foreach (PROTO_FILE ${PROTO_FILES})
get_filename_component (PROTO_NAME ${PROTO_FILE} NAME_WE)
set (PROTO_SRC_FILES ${PROTO_SRC_FILES}
${PROTOC_OUT_DIR}/${PROTO_NAME}.pb.cc)
set (PROTO_INCLUDE_FILES ${PROTO_INCLUDE_FILES}
${PROTOC_OUT_DIR}/${PROTO_NAME}.pb.h)
endforeach (PROTO_FILE ${PROTO_FILES})
# Custom protobuf command
file (MAKE_DIRECTORY ${PROTOC_SRC_DIR})
add_custom_command (
OUTPUT ${PROTO_SRC_FILES} ${PROTO_INCLUDE_FILES}
COMMAND ${PROTOC_BIN} ${PROTO_FILES}
--proto_path=${PROTO_IN_DIR}
--cpp_out=${PROTOC_OUT_DIR}
DEPENDS ${PROTO_FILES}
WORKING_DIRECTORY ${PROTO_IN_DIR})
# Add Protobuf to compiler and linker command lines
include_directotries (${PROTOC_INCLUDE_DIR})
link_directories (${PROTOC_LIB_DIR})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment