25

I'm trying to make it same way I made it for 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}                                                                                        
)  

(googled it somewhere) but got bad output:

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

How can I link it with cmake? or maybe I even can compile .proto file using cmake?

cnd
  • 32,616
  • 62
  • 183
  • 313

2 Answers2

47

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
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • 4
    Thank you for this answer. Instead of `${Boost_FILESYSTEM_LIBRARY}` and `${Boost_SYSTEM_LIBRARY}` you can use `${Boost_LIBRARIES}`. – Jared Burrows Jul 28 '15 at 03:35
  • The line `include(FindProtobuf)` seems to be not needed: With `find_package(Protobuf)` CMake automatically includes given file. In CMake `FindXXX` modules are not intended to be included directly (via `include`). – Tsyvarev Oct 07 '21 at 16:46
0

Spent a lot of time on this.. A. Different versions may require regeneration of cc files (obviously) B. Different versions have different naming (PROTOBUF_LIBRARY vs. Protobuf_LIBRARIES) Do note that the previous answer refers to view the FindProtobuf help which states the naming convention. Also, Use '''message(STATUS "debug protobuf lib location:${PROTOBUF_LIBRARIES} ''' to debug.

Nir Geffen
  • 66
  • 1
  • 6