Windows上源碼編譯多數開源軟件都很麻煩c++
編譯環境:VS2015(grpc支持2013及以上,2012上沒有Nuget,編譯起來要費勁的多)git
編譯GRPC涉及內容async
grpc代碼下載後,執行git submodule update --init初始化依賴的submoduleui
1. protobufspa
參考readme用CMAKE生成工程文件,編譯便可命令行
2. grpc,grpc_protoc_plugin:在vsprojects裏有建好的工程文件,用vs2015編譯基本不會遇到什麼問題,除了:code
grpc_cpp_plugin依賴libprotoc.lib,而protobuf生成的庫名稱爲libprotocd.lib,這塊須要手動改一下component
3. zlib 參考readmeserver
上面的幾步都是很順利的,grpc和protobuf默認都是靜態庫編譯,不會遇到連接錯誤,下面嘗試編譯example/cpp/helloworldblog
1. 修改CmakeFiles.txt,因爲helloworld依賴protobuf須要在命令行指定protobuf的protobuf-config.cmake所在位置
cmake -Dprotobuf_DIR=
grpc我是使用自帶的工程文件編譯的,沒找到對應的config.cmake,我是把對grpc的依賴去掉了,改爲:
# Minimum CMake required cmake_minimum_required(VERSION 2.8) # Project project(HelloWorld CXX) # Protobuf set(protobuf_MODULE_COMPATIBLE TRUE) find_package(protobuf CONFIG REQUIRED) message(STATUS "Using protobuf ${protobuf_VERSION}") # gRPC #find_package(gRPC CONFIG) message(STATUS "Using gRPC ${gRPC_VERSION}") # grpc相關的目錄配置 include_directories("C:/path/to/grpc/include") link_directories("C:/path/to/grpc/vsprojects/Debug") link_directories("C:/path/to/grpc/third_party/zlib") link_directories("C:/path/to/grpc/vsprojects/packages/grpc.dependencies.openssl.1.0.204.1/build/native/lib/v140/Win32/Debug/static") add_definitions(-D_WIN32_WINNT=0x0601) set(gRPC_CPP_PLUGIN_EXECUTABLE "C:/path/to/grpc/vsprojects/Debug/grpc_cpp_plugin.exe") # Proto file get_filename_component(hw_proto "../../protos/helloworld.proto" ABSOLUTE) get_filename_component(hw_proto_path "${hw_proto}" PATH) # Generated sources protobuf_generate_cpp(hw_proto_srcs hw_proto_hdrs "${hw_proto}") set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc") set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h") add_custom_command( OUTPUT "${hw_grpc_srcs}" "${hw_grpc_hdrs}" COMMAND protobuf::protoc ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" -I "${hw_proto_path}" --plugin=protoc-gen-grpc="${gRPC_CPP_PLUGIN_EXECUTABLE}" "${hw_proto}" DEPENDS "${hw_proto}") # Generated include directory include_directories("${CMAKE_CURRENT_BINARY_DIR}") # Targets greeter_[async_](client|server) foreach(_target greeter_client greeter_server greeter_async_client greeter_async_server) add_executable(${_target} "${_target}.cc" ${hw_proto_srcs} ${hw_grpc_srcs}) # 添加對應的lib庫依賴 target_link_libraries(${_target} protobuf::libprotobuf grpc++_unsecure grpc++ grpc++_reflection zlib grpc gpr ws2_32 grpc_unsecure libeay32 ssleay32 libvcruntimed) endforeach()
2. 生成出工程文件後,須要修改greeter_client/server的CRT依賴爲/MTd(由於上面的grpc等默認都是/MTd)
3. 編譯便可
4. 這時應該會遇到找不到Grpc內的某個符號,好比Channel、Complete_quque等, 如下以channel爲例說明解決方法:
GRPC編譯grpc++.lib時,生成的Channel.obj會被覆蓋(緣由沒查出來,大概是由於grpc.lib編譯時一樣會生成channel.obj)
刪掉obj目錄中C:\path\to\grpc\vsprojects\IntDir\grpc++中的channel.obj,而後在工程裏「生成」(不要從新生成)
5. 解決了上面的問題,應該就能夠生成可執行文件了