Search code examples
c++cmakeprotocol-buffers

Compile protobuf to c++, cmake connot find the dir


This is my cmakelist.txt

cmake_minimum_required(VERSION 3.25)
 SET(MESSAGE_DIR "/home/dbn/work/seal/cmd/protogen")
 if(EXISTS "${CMAKE_BINARY_DIR}cmd/proto" AND IS_DIRECTORY "${CMAKE_BINARY_DIR}/cmd/pro
         SET(PROTO_META_BASE_DIR ${MESSAGE_DIR})
 else()
     file(MAKE_DIRECTORY ${MESSAGE_DIR})
        SET(PROTO_META_BASE_DIR ${MESSAGE_DIR})
 endif()
 LIST(APPEND PROTO_FLAGS -I${CMAKE_SOURCE_DIR}/proto)
 file(GLOB_RECURSE MSG_PROTOS ${CMAKE_SOURCE_DIR}/proto/*.proto)
 
 set(MESSAGE_SRC "")
 set(MESSAGE_HDRS "")
 
 foreach(msg ${MSG_PROTOS})
        get_filename_component(FIL_WE ${msg} NAME_WE)                                
                                                                                     
        list(APPEND MESSAGE_SRC "${CMAKE_SOURCE_DIR}/proto/${FIL_WE}.pb.cc")         
        list(APPEND MESSAGE_HDRS "${CMAKE_SOURCE_DIR}/proto/${FIL_WE}.pb.h")     
                                                                                                                                        
       add_custom_command(                                                      
          OUTPUT "${CMAKE_SOURCE_DIR}/proto/${FIL_WE}.pb.cc"                     
                "${CMAKE_SOURCE_DIR}/proto/${FIL_WE}.pb.h"                      
          COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}                                  
          ARGS --cpp_out=${PROTO_META_BASE_DIR} -I${CMAKE_SOURCE_DIR} ${msg}          
          DEPENDS ${msg}
          COMMENT "Running C++ protocol buffer compiler on ${msg}"
          VERBATIM                                                
         )                                                         
 endforeach() 

when i compile it , it turn out this error

[ 33%] Running C++ protocol buffer compiler on /home/dbn/work/seal/cmd/proto/test.proto
/bin/sh: -I/home/dbn/work/seal/cmd: No such file or directory
[ 66%] Running C++ protocol buffer compiler on /home/dbn/work/seal/cmd/proto/person.proto
/bin/sh: -I/home/dbn/work/seal/cmd: No such file or directory
[100%] generate message target

but actually ,the dir '/home/dbn/work/seal/cmd/proto' exist. pwd

pwd
/home/dbn/work/seal/cmd

I have tried to ARGS --cpp_out=${PROTO_META_BASE_DIR} -I${CMAKE_SOURCE_DIR} ${msg} to ARGS --cpp_out=${PROTO_META_BASE_DIR} -I=${CMAKE_SOURCE_DIR} ${msg} but it's no use.


Solution

  • the dir '/home/dbn/work/seal/cmd/proto' exist.

    The issue is not the non existing directory /home/dbn/work/seal/cmd/proto. The issue is the non existing file -I/home/dbn/work/seal/cmd that the shell wants to run as an executable file. It happens because of the empty variable ${PROTOBUF_PROTOC_EXECUTABLE} and the shown CMakeLists.txt is likely not what you run. Check where you setup the variable.