- 首页 > it技术 > >
cmake|cmake - 如何生成grpc的*.pb.cc和*.pb.h文件
linuxcmakegrpc
cmake_minimum_required(VERSION 2.6)
set(EXE proto_depend)
project(${EXE})set(GNU_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "${GNU_FLAGS} ")
set(CMAKE_C_FLAGS "${GNU_FLAGS}")# Proto file
get_filename_component(hw_proto "./protos/helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)
message(---1------ "${hw_proto}")
message(---2------ "${hw_proto_path}")
message(---3------ "${CMAKE_CURRENT_SOURCE_DIR}")# Generated sources
# protoc -I ./protos --cpp_out=. ./protos/helloworld.proto
# helloworld.pb.cc
# helloworld.pb.h
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/helloworld.pb.cc" "${CMAKE_CURRENT_SOURCE_DIR}/helloworld.pb.h"
COMMAND protoc -I ${hw_proto_path} --cpp_out=. ${hw_proto}
WORKING_DIRECTORY ../
DEPENDS "${hw_proto}"
)
add_custom_target(
gen_pb
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/helloworld.pb.cc
)
# Generated sources
# protoc -I ./protos --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin ./protos/helloworld.proto
# helloworld.grpc.pb.cc
# helloworld.grpc.pb.h
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/helloworld.grpc.pb.cc" "${CMAKE_CURRENT_SOURCE_DIR}/helloworld.grpc.pb.h"
COMMAND protoc -I ${hw_proto_path} --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin ${hw_proto}
WORKING_DIRECTORY ../
DEPENDS "${hw_proto}"
)
add_custom_target(
gen_pb_grpc
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/helloworld.grpc.pb.cc
)# for python grpc
# protoc -I ./protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_python_plugin ./protos/helloworld.proto
add_custom_command(
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/helloworld.grpc.pb.cc" "${CMAKE_CURRENT_SOURCE_DIR}/helloworld.grpc.pb.h"
COMMAND protoc -I ${hw_proto_path} --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin ${hw_proto}
WORKING_DIRECTORY ../
DEPENDS "${hw_proto}"
)
add_custom_target(
gen_pb_grpc_py
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/helloworld.grpc.pb.cc
)add_custom_target(gen_all)
add_dependencies(gen_all gen_pb gen_pb_grpc)# !!!
link_directories(
/usr/local/lib
)# !!!
aux_source_directory(. SRCS)add_executable(${EXE} main.cpp ${SRCS})
add_dependencies(${EXE} gen_all)# target_link_libraries(${EXE} grpc++ grpc grpc++_reflection)
target_link_libraries(
${EXE}
-lprotobuf
-lgrpc++
-lgrpc
-lgrpc++_reflection
-ldl
-lpthread
)
推荐阅读