如何在linux上通过cmake链接googleprotobuf库

How to link google protobuf libraries via cmake on linux?

本文关键字:cmake 链接 googleprotobuf linux      更新时间:2023-10-16

我正试图以与boost相同的方式制作:

find_package(Boost COMPONENTS system filesystem REQUIRED)                                                    
find_package(ProtocolBuffers)                                                                                
## Compiler flags                                                                                            
if(CMAKE_COMPILER_IS_GNUCXX)                                                                                 
    set(CMAKE_CXX_FLAGS "-O2")                                                                               
    set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")                                                   
endif()                                                                                                      
target_link_libraries(complex                                                                                
  ${Boost_FILESYSTEM_LIBRARY}                                                                                
  ${Boost_SYSTEM_LIBRARY}                                                                                    
  ${PROTOBUF_LIBRARY}                                                                                        
)  

(在谷歌上搜索了一下)但输出不好:

CMake Warning at complex/CMakeLists.txt:18 (find_package):
  Could not find module FindProtocolBuffers.cmake or a configuration file for
  package ProtocolBuffers.
  Adjust CMAKE_MODULE_PATH to find FindProtocolBuffers.cmake or set
  ProtocolBuffers_DIR to the directory containing a CMake configuration file
  for ProtocolBuffers.  The file will have one of the following names:
    ProtocolBuffersConfig.cmake
    protocolbuffers-config.cmake

如何将其与cmake联系起来?或者我甚至可以使用cmake编译.proto文件?

您可以尝试CMake的FindProtobuf模块:

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


有关更多信息,请运行

cmake --help-module FindProtobuf

在这方面花了很多时间。。A.不同版本可能需要重新生成cc文件(显然)B.不同的版本有不同的命名(PROTOBUF_IBRARY与PROTOBUF_LIBRARIES)请注意,前面的答案指的是查看FindProtobuf帮助,其中说明了命名约定。此外,请使用"消息(STATUS"调试protobuf库位置:${protobuf_BRARIES}"以进行调试。