1# Minimum CMake required 2cmake_minimum_required(VERSION 2.8.12) 3 4# Project 5project(protobuf-examples) 6 7# Find required protobuf package 8find_package(protobuf CONFIG REQUIRED) 9 10if(protobuf_VERBOSE) 11 message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}") 12endif() 13 14set(CMAKE_INCLUDE_CURRENT_DIR TRUE) 15 16# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F 17if(MSVC AND protobuf_MSVC_STATIC_RUNTIME) 18 foreach(flag_var 19 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 20 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 21 if(${flag_var} MATCHES "/MD") 22 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 23 endif(${flag_var} MATCHES "/MD") 24 endforeach() 25endif() 26 27foreach(example add_person list_people) 28 set(${example}_SRCS ${example}.cc) 29 set(${example}_PROTOS addressbook.proto) 30 31 #Code Generation 32 if(protobuf_MODULE_COMPATIBLE) #Legacy Support 33 protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS}) 34 list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS}) 35 endif() 36 37 #Executable setup 38 set(executable_name ${example}_cpp) 39 add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS}) 40 if(protobuf_MODULE_COMPATIBLE) #Legacy mode 41 target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS}) 42 target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES}) 43 else() 44 target_link_libraries(${executable_name} protobuf::libprotobuf) 45 protobuf_generate(TARGET ${executable_name}) 46 endif() 47 48endforeach() 49