CMake with Google Protocol Buffers

后端 未结 1 1916
后悔当初
后悔当初 2021-02-01 06:45

I\'m trying to use cmake to build my little project using protocol buffers.

There\'s a root directory with a number of subdirectories with a number of libraries and exec

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 07:22

    I think the problem here is that the PROTOBUF_GENERATE_CPP function sets up the .pb.h and .pb.cc files to exist in the build tree, not in the source tree.

    This is good practice (not polluting the source tree), but it means that your call include_directories(../messages) is adding the wrong value to the search paths. This is adding the source directory "root/messages", whereas you want "[build root]/messages".

    You could probably just replace that line with:

    include_directories(${CMAKE_BINARY_DIR}/messages)
    

    However, a more robust, maintainable way might be to set the required include path inside the messages/CMakeLists.txt. To expose this value to the parent scope, this would need to either use set(... PARENT_SCOPE) or:

    set(ProtobufIncludePath ${CMAKE_CURRENT_BINARY_DIR}
        CACHE INTERNAL "Path to generated protobuf files.")
    

    Then in the top-level CMakeLists.txt, you can do:

    include_directories(${ProtobufIncludePath})
    

    If your messages library itself needs to #include the generated protobuf files (this would be normal), then it too should have a similar include_directories call.

    Having said all that, if you can specify CMake v2.8.12 as the minimum, you can use the target_include_directories command instead.

    In messages/CMakeLists.txt after the add_library call, you'd simply do:

    target_include_directories(messages PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
    

    Then any other target which depends on messages automatically has the appropriate "messages" include dirs added to its own - you don't need to explicitly call include_directories at all.

    0 讨论(0)
提交回复
热议问题