Search code examples
androidc++protocol-buffers

Include libprotobuf.a in Android CMake file


I am trying to use Protocol Buffers in C++ under Android (x86, x86_64, armeabi-v7a, arm64-v8a). Using these build scripts, I have been able to generate libprotobuf.a static libraries for the different architectures.

However, I am struggling to make the connection between the static libraries and the test_spec.pb.cc and test_spec.pb.h files that I generated with my system's global protobuf compiler of the same version (3.19.2).

What I have tried

  • I have tried setting both the Protobuf_LIBRARY and Protobuf_LIBRARIES to the path to libprotobuf.a
  • I have tried setting Protobuf_INCLUDE_DIRS and Protobuf_INCLUDE_DIR to the include directory I generated running the extract_includes.bat.in script in protobuf's cmake folder
  • I have tried finding protobuf using find_package( Protobuf REQUIRED ) and find_package( Protobuf REQUIRED HINTS _path_to_dir_containing_lib_and_include_)
  • I have tried setting Protobuf_SRC_ROOT_FOLDER to a directory containing libprotobuf.a and the include folder

This is my current CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_PREFIX_PATH
    ${CMAKE_PREFIX_PATH}
    ${CMAKE_PREFIX_PATH}/../cpp
)

ADD_LIBRARY(protobuf STATIC IMPORTED)
SET_TARGET_PROPERTIES(protobuf PROPERTIES IMPORTED_LOCATION ../cpp/libs/${ANDROID_ARCH}/google/protobuf/libprotobuf.a)

INCLUDE(FindProtobuf)
SET(Protobuf_USE_STATIC_LIBS ON)
SET(Protobuf_SRC_ROOT_FOLDER ../cpp)
SET(Protobuf_LIBRARIES protobuf)
SET(Protobuf_INCLUDE_DIR ../cpp/include/google/protobuf)
FIND_PACKAGE(Protobuf REQUIRED)
ADD_LIBRARY(cpp
            SHARED
            ../cpp/rn-etsi-parser.cpp
            cpp-adapter.cpp
)

# Specifies a path to native header files.
INCLUDE_DIRECTORIES(
            ../cpp
)

And this is the current error I get when I run it:

CMake Error at CMakeLists.txt:18 (FIND_PACKAGE):
Could not find a package configuration file provided by "Protobuf" with any
of the following names:
  ProtobufConfig.cmake
  protobuf-config.cmake
Add the installation prefix of "Protobuf" to CMAKE_PREFIX_PATH or set      
"Protobuf_DIR" to a directory containing one of the above files.  If       
"Protobuf" provides a separate development package or SDK, be sure it has  
been installed.

Thank you for your help!


Solution

  • Thanks to David's nudge and various CMake/C++ tutorials I was able to figure out a solution that works for my use case.

    What I did:

    • I moved both libprotobuf.a as well as the respective include to a folder within my PROJECT_SOURCE_DIR
    • I cleaned up my eclectic mix of find_package, add_library, and find_protobuf
    • I added the linker flag -llog to bypass this issue
    cmake_minimum_required(VERSION 3.5)
    
    set (CMAKE_VERBOSE_MAKEFILE ON)
    set (CMAKE_CXX_STANDARD 11)
    set (CMAKE_SHARED_LINKER_FLAGS "-llog")
    list(APPEND CMAKE_PREFIX_PATH [
        ${CMAKE_PREFIX_PATH}
    ])
    
    ADD_LIBRARY(protobuf STATIC IMPORTED)
    SET_TARGET_PROPERTIES(protobuf PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/google/protobuf/${ANDROID_ABI}/libprotobuf.a)
    INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/main/jniLibs/include/)
    
    ADD_LIBRARY(cpp
                SHARED
                ../cpp/rn-etsi-parser.cpp
                cpp-adapter.cpp
    )
    
    TARGET_LINK_LIBRARIES(cpp PUBLIC protobuf)
    
    # Specifies a path to native header files.
    INCLUDE_DIRECTORIES(
                ../cpp
    )
    

    I'd be interested to know, however, if there is a way to 'inject' prebuilt protobuf libraries into CMake's find_protobuf workflow, so that I could make use of features like protobuf_generate_cpp.