cmake PROTOBUF_GENERATE_CPP未生成源文件和头文件

nzrxty8p  于 2022-11-11  发布在  其他
关注(0)|答案(4)|浏览(514)

我的cmake配置没有生成任何protobuf src和头文件。我已经检查了是否可以找到proto文件。

Cmakelists.txt

cmake_minimum_required(VERSION 3.0.2)
..
include(FindProtobuf REQUIRED)
file(GLOB PROTO_DEF "${CMAKE_CURRENT_SOURCE_DIR}/protobuf/*/*.proto")

foreach(file ${PROTO_DEF})    
    if(EXISTS ${file})
        MESSAGE("YES")
    else()
        MESSAGE("NO")
    endif()
endforeach()
SET(PROTOBUF_GENERATE_CPP_APPEND_PATH PROTOBUF)
SET(PROTOBUF_PROTOC_EXECUTABLE protoc.exe)

..
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_INCL ${PROTO_DEF})
..
add_library(${PROJECT_NAME} STATIC ${INCLUDES} ${INTERNAL_INCLUDES} ${SRC} ${PROTO_SRC} ${PROTO_INCL})
target_link_libraries(${PROJECT_NAME} ${PROTOBUF_LIBRARIES})

我已经检查了FindProtobuf.cmake,并检查了一半:

foreach(FIL ${ARGN})
    get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
    get_filename_component(FIL_WE ${FIL} NAME_WE)

    list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
    list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")

    MESSAGE(1 ${CMAKE_CURRENT_BINARY_DIR})
    MESSAGE(2 ${_protobuf_include_path})
    MESSAGE(3 ${ABS_FIL})
    MESSAGE(4 ${PROTOBUF_PROTOC_EXECUTABLE})

    add_custom_command(
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
             "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
      COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE}
      ARGS --cpp_out  ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
      DEPENDS ${ABS_FIL}
      COMMENT "Running C++ protocol buffer compiler on ${FIL}"
      VERBATIM )

  endforeach()

你可以看到我已经添加了4个消息命令,脚本到达这一点,变量显示良好的值。原型文件确实依赖于库,因此命令应该得到执行!
对这个问题有什么想法吗?

更新

将add_custom_command替换为

EXEC_PROGRAM(${PROTOBUF_PROTOC_EXECUTABLE} ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}

生成源文件和头文件,我必须手动激活custom_commmands吗?
此致奥克

6yt4nkrj

6yt4nkrj1#

add_custom_command在编译时被触发(对于我的项目)。

SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE)

为cmake提供将生成文件的信息。

vjrehmav

vjrehmav2#

如果您有外部原型文件夹,请选择:

file(GLOB PROTOBUF_FILELIST ${PROTO_INCLUDE_DIR}/*.proto)
foreach( proto_file ${PROTOBUF_FILELIST} )
   get_filename_component(proto_name ${proto_file} NAME_WE)
   get_filename_component(proto_path ${PROTO_INCLUDE_DIR} ABSOLUTE)
   set_source_files_properties("${proto_path}/${proto_name}.pb.cc"
                               "${proto_path}/${proto_name}.pb.h"
                               PROPERTIES GENERATED TRUE)
endforeach()
qco9c6ql

qco9c6ql3#

请参见:https://cmake.org/cmake/help/v3.20/module/FindProtobuf.html
示例:

find_package(Protobuf REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS foo.proto)
protobuf_generate_python(PROTO_PY foo.proto)
add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(bar ${Protobuf_LIBRARIES})

注意protobuf_generate_cpp和protobuf_generate_python函数以及add_executable()或add_library()调用只能在同一目录中正常工作。

i1icjdpr

i1icjdpr4#

此问题仅在目标为库时发生(使用add_library,add_executable似乎可以)。PROTOBUF_GENERATE_CPP将在构建库时延迟protobuf源和头文件的生成。解决方法是定义一个自定义目标,并将其作为依赖项添加到库目标。例如:

add_custom_target(proto_dep DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})
add_library(${PROJECT_NAME} STATIC ${OTHER_SRCS})
add_dependencies(${PROJECT_NAME} proto_dep)
target_link_libraries(${PROJECT_NAME} PRIVATE gRPC::grpc++ gRPC::grpc++_reflection protobuf::libprotobuf)

相关问题