CMakeLists.txt 1.6 KB

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