这是我的项目树:
proto-build
|—— build/ #empty, used for cmake building
|—— include/
|—— google/ #protobuf headers
|—— lib/
|—— libprotobuf-lite.lib
|—— proto/
|—— test.proto
|—— CMakeLists.txt
这是CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_VERBOSE_MAKEFILE ON)
SET(ROOT_PATH ".")
SET(PROTO_DIR ${ROOT_PATH}/proto)
SET(PROTOBUF_LIBRARY ${ROOT_PATH}/lib)
SET(PROTOBUF_INCLUDE_DIR ${ROOT_PATH}/include)
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_DIR}/test.proto)
add_library(foo ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(foo ${PROTOBUF_LIBRARIES})
然后我打开cmd并找到proto-build / build /,运行命令:
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..
它工作正常。然后运行命令:
nmake
发生错误:
NMAKE : fatal error U1073: don't know how to make 'E:\Source\proto-build\PROTOBUF_PROTOC_EXECUTABLE-NOTFOUND'
Stop.
NMAKE : fatal error U1077: '"D:\Program_Filesx86\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"D:\Program_Filesx86\Microsoft Visual Studio 14.0\VC\BIN\amd64\nmake.exe"' : return code '0x2'
Stop.
答案 0 :(得分:0)
找不到Protobuf的执行者。因此,错误的路径E:\Source\proto-build\PROTOBUF_PROTOC_EXECUTABLE-NOTFOUND
被写入Makefile,这会导致错误。
答案 1 :(得分:0)
对于usr1234567的建议,我已经弄明白,这是新的CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_VERBOSE_MAKEFILE ON)
SET(ROOT_PATH ".")
SET(PROTO_DIR ${ROOT_PATH}/proto)
#must define these tow variables:PROTOBUF_INCLUDE_DIR & PROTOBUF_LIBRARY, even the value is invalid.
SET(PROTOBUF_INCLUDE_DIR .)
SET(PROTOBUF_LIBRARY .)
SET(PROTOBUF_PROTOC_EXECUTABLE ../protoc.exe)
#in this directory, contains protobuf includes: top directory is <google>
SET(THIRD_INCLUDE_DIR ${ROOT_PATH}/include)
#if set PROTOBUF_INCLUDE_DIR to the directory of protobuf includes instead of using include_directories, errors occurred when nmake,I don't know why
include_directories(${THIRD_INCLUDE_DIR})
file(GLOB PROTO_LIST ${PROTO_DIR}/*.proto)
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_LIST})
add_library(foo ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(foo)
使用此CMakeLists.txt进行camke和nmake,构建将获得成功。 有几个细节需要注意,我在CMakeLists.txt中发表评论。
存在一个奇怪的问题:你必须在目录中放置两个protoc.exe文件:一个是proto-build /,一个是proto-build /,否则出错会发生:找不到protoc.exe。