cmake - 如何生成grpc的*.pb.cc和*.pb.h文件

2 篇文章 0 订阅
1 篇文章 0 订阅
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
)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值