2

I am using protobuf_generate_cpp() in order to generate *.cc and *.h files from the *.proto file but it is not getting executed. I do not get any error message, the lines are just skipped. The CMakeLists.txt file looks something like this:

cmake_minimum_required(VERSION 3.1.2)
cmake_policy(SET CMP0028 NEW)    

project(ProjectName CXX C)

execute_process(COMMAND conan install . WORKING_DIRECTORY ${CMAKE_HOME_DIRECTORY})
include(conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(Protobuf REQUIRED)

protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS /path/to/NAME.proto)

add_custom_command(
    OUTPUT NAME.pbs.h
    OUTPUT NAME.pbs.cc
    DEPENDS NAME.proto
    COMMAND protoc --cpp_out=$(CMAKE_BINARY_SOURCE_DIR) --proto_path=../path/to/NAME.proto
)

set(ProjectName-HeaderFiles
    ${ProjectName-HeaderFiles}
  <list of all header files>
)

set(ProjectName-SourceFiles
    ${ProjectName-SourceFiles}
  <list of all source files>
)

add_executable(ProjectName  ${PROTO_SRCS} ${PROTO_HDRS}
                            <list of all other executables>
)

add_definitions(
  -D_CONSOLE
  -DWIN32
  -D_DEBUG
  -DSTANDALONE_PROGRAM
)

include_directories(
  <list of all include paths>
)
target_link_libraries( ProjectName
PUBLIC
CONAN_PKG::Protobuf
LIB1
LIB2
)

My solution is created @location CMAKE_BINARY_SOURCE_DIR but NAME.pb.h and NAME.pb.cc files do not get created. Could someone let me know what the issue might be? Any suggestions are appreciated.

  • For `add_custom_command` being run, its OUTPUT artifacts (`NAME.pb.h` and/or `NAME.pb.cc`) should be listed as source files for an executable. Where/How these artifacts are listed in your code? See also this question: https://stackoverflow.com/questions/2937128/cmake-add-custom-command-not-being-run. – Tsyvarev Aug 27 '18 at 13:34
  • Also, where are the typical ``include(conanbuildinfo.cmake)`` lines? It would be useful to provide a full reproducible example, also including the consuming conanfile (specifying the exact Protobuf package, as it might be a package issue, not a conan or cmake issue). – drodri Aug 27 '18 at 13:58
  • I think there is a related issue here: https://github.com/bincrafters/community/issues/412 – uilianries Aug 28 '18 at 01:29
  • @drodri Its not a package issue. I have included conan install and find_package() and include() command. I skipped to mention it in my post. I have edited it and added now. – newbieDevloper Aug 29 '18 at 06:19
  • @Tsyvarev I did not add NAME.pb.h and NAME.pb.cc in the set(headerFiles) or set(SourceFiles) list. Is that what you are saying? that I should add the files there? – newbieDevloper Aug 29 '18 at 06:23
  • Yes, `NAME.pb.h` and `NAME.pb.cc` files should be added to set of source files for `add_executable`. BTW, the package "Protobuf" (which you include via `find_package(Protobuf)`), [defines](https://cmake.org/cmake/help/v3.9/module/FindProtobuf.html#command:protobuf_generate_cpp) macro `protobuf_generate_cpp`. This macro does the same things, which you tend to achieve with your `add_custom_command`. Note on the usage example of the macro: you need to list generated sources and headers for `add_executable` too. – Tsyvarev Aug 29 '18 at 07:10
  • @Tsyvarev I tried to do it with protobuf_generate_cpp() as well but the .cc and .h file do not get created. The CMake file still executes without any error message and my project.sln is created. I have edited my CMake file above to use protobuf_generate_cpp – newbieDevloper Aug 31 '18 at 10:35
  • 1
    "The CMake file still executes without any error message and my `project.sln` is created." - This is correct behavior. Protobuf generation is performed when you build your `.sln` file (in Visual Studio or from command line). – Tsyvarev Aug 31 '18 at 12:49
  • @Tsyvarev That's correct. I was under the impression that running CMakeLists.txt file should create the files(I have never worked with Protobuf before). Thanks. It's working as expected. I just had another doubt. I also need NAME.pbs.cc and NAME.pbs.h. How to create that? NAME.proto file should create 4 files: NAME.pb.h, NAME.pb.cc, NAME.pbs.h and NAME.pbs.cc but .pbs files are not created, – newbieDevloper Aug 31 '18 at 13:00
  • I am not familiar with protobuf. Probably, you need additional options for generate `.pbs` files. If macro `protobuf_generate_cpp` doesn't support such options, you need to resort to `add_custom_command`, as was in your original post. – Tsyvarev Aug 31 '18 at 13:14
  • @Tsyvarev I tried to use the add_custom_command() to generate NAME.pbs.h and .cc files and Cmake files executes without error. But when I compile my solution the custom command is not getting executed and I still get error for .pbs files. Also, when I add the NAME.pbs.cc file in add_executable() CMake gives an error saying file does not exist(which is correct, since file will be generated only after building the solution, but for me VS is not executing the custom command. ) – newbieDevloper Sep 03 '18 at 10:30

0 Answers0