How to Link Google Protobuf Libraries via Cmake on Linux

How to link google protobuf libraries via cmake on linux?

You could try CMake's FindProtobuf module:

include(FindProtobuf)
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
...
target_link_libraries(complex
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${PROTOBUF_LIBRARY}
)



For further info, run

cmake --help-module FindProtobuf

CMake can't find protobuf when compiling Google's protobuf example

Fair warning I am no expert.

I ran in to a similar problem in my own build trying to get Boost working and I think it has to do with your environment variables and how you have your Visual Studio setup. While you are setting crucial things such as

SET(PROTOBUF_INCLUDE_DIR "d:/vcpkg/packages/protobuf_x64-windows/include/")

The actual find_package(protobuf CONFIG REQUIRED) throws these settings out the window. Once it finds that config file it only cares about what the config file is finding, I think this is the cause of how your first MESSAGE has the right one, and then your 2nd one doesn't find one.

Are you positive you only have one installation of protobuf on your machine?

  • It appears to be finding this "used as include directory in directory D:/protobuf-3.12.2/examples"
  • yet you are trying to find "D:/vcpkg/packages/protobuf_x64-windows" no?

Try adding a "-DCMAKE_PREFIX_PATH="d:/vcpkg/packages/protobuf_x64-windows" to your CMake options in Visual Studio

Good luck and sorry if this doesn't help, I'm relatively new to programming but its worth a try.

CMake with Google Protocol Buffers

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.



Related Topics



Leave a reply



Submit