ubuntu22.04安装使用 protobuf 23.3静态库 您所在的位置:网站首页 pb-06cs08使用说明 ubuntu22.04安装使用 protobuf 23.3静态库

ubuntu22.04安装使用 protobuf 23.3静态库

2023-06-29 20:21| 来源: 网络整理| 查看: 265

https://github.com/protocolbuffers/protobuf/

下载最新的release版本,23.3

必须需要依赖的第三方库 abseil,utf8_range

mkdir build

cd build

cmake -DCMAKE_BUILD_TYPE=DEBUG  -DCMAKE_INSTALL_PREFIX=/usr/local -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_TESTS=OFF ..

make

sudo make install

或直接修改CmakeList.txt 添加/修改以下内容

set(protobuf_VERBOSE ON) #debug信息 set(protobuf_BUILD_TESTS OFF)#不编译test set(protobuf_WITH_ZLIB OFF) #不使用zlib set(protobuf_WITH_ZLIB_DEFAULT OFF)#不使用zlib set(protobuf_BUILD_SHARED_LIBS OFF)#静态库 set(protobuf_BUILD_SHARED_LIBS_DEFAULT OFF)#静态库 set(CMAKE_INSTALL_PREFIX /usr/local)#安装路径

如果要建立动态库,依赖的abseil 最好也是动态库.

安装后使用的方法,在target_link_libraries里,需要先写protobuf,然后rtf8_range,最后才是absl。 顺序不可以改变,因为protobuf要依赖后两个,rtf8_range要依赖absl

目录结构

 cmakeList.txt

absl把所有的都一起贴上了

cmake_minimum_required(VERSION 3.19) project(protobut_demo) set(CMAKE_CXX_STANDARD 17) include_directories(/usr/local/include) #------------------------------------------------------------------ find_package(Protobuf REQUIRED) include_directories(${Protobuf_INCLUDE_DIRS}) #${Protobuf_LIBRARIES} #检查是否有值 MESSAGE("Protobuf lib path :" ${Protobuf_LIBRARIES}) MESSAGE("Protobuf include path:" ${Protobuf_INCLUDE_DIRS}) MESSAGE("Protobuf exetuable path:" ${PROTOBUF_PROTOC_EXECUTABLE}) # proto源文件位置 SET(PROTO_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/proto) # proto 文件生成位置,包含.pb.cc 和 .pb.h SET(PROTO_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/proto_output) # proto 文件 file(GLOB proto_files ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto ) # 生成文件 FOREACH(PROTO_FILE ${proto_files}) EXECUTE_PROCESS( COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} -I${PROTO_SOURCE_DIR} --cpp_out=${PROTO_OUTPUT_DIR} ${PROTO_FILE} ) ENDFOREACH() #--------------------------------------------------------------------------------------------- include_directories(${PROTO_OUTPUT_DIR})#生成的头文件路径 # 将proto生成的.pb.cc 和 .pb.h分类 file(GLOB contacts_All ${PROTO_OUTPUT_DIR}/*.pb.h ${PROTO_OUTPUT_DIR}/*.pb.cc ) find_package(absl) find_package(utf8_range) add_executable(proto_test src/proto_test.cpp ${contacts_All}) target_link_libraries(proto_test ${Protobuf_LIBRARIES} /usr/local/lib/libutf8_range.a /usr/local/lib/libutf8_validity.a absl::bad_any_cast_impl absl::bad_optional_access absl::bad_variant_access absl::base absl::city absl::civil_time absl::cord absl::cord_internal absl::cordz_functions absl::cordz_handle absl::cordz_info absl::cordz_sample_token absl::crc32c absl::crc_cord_state absl::crc_cpu_detect absl::crc_internal absl::debugging_internal absl::demangle_internal absl::die_if_null absl::examine_stack absl::exponential_biased absl::failure_signal_handler absl::flags absl::flags_commandlineflag absl::flags_commandlineflag_internal absl::flags_config absl::flags_internal absl::flags_marshalling absl::flags_parse absl::flags_private_handle_accessor absl::flags_program_name absl::flags_reflection absl::flags_usage absl::flags_usage_internal absl::graphcycles_internal absl::hash absl::hashtablez_sampler absl::int128 absl::kernel_timeout_internal absl::leak_check absl::log_entry absl::log_flags absl::log_globals absl::log_initialize absl::log_internal_check_op absl::log_internal_conditions absl::log_internal_format absl::log_internal_globals absl::log_internal_log_sink_set absl::log_internal_message absl::log_internal_nullguard absl::log_internal_proto absl::log_severity absl::log_sink absl::low_level_hash absl::malloc_internal absl::periodic_sampler absl::random_distributions absl::random_internal_distribution_test_util absl::random_internal_platform absl::random_internal_pool_urbg absl::random_internal_randen absl::random_internal_randen_hwaes absl::random_internal_randen_hwaes_impl absl::random_internal_randen_slow absl::random_internal_seed_material absl::random_seed_gen_exception absl::random_seed_sequences absl::raw_hash_set absl::raw_logging_internal absl::scoped_set_env absl::spinlock_wait absl::stacktrace absl::status absl::statusor absl::strerror absl::str_format_internal absl::strings absl::strings_internal absl::symbolize absl::synchronization absl::throw_delegate absl::time absl::time_zone )

student.proto

syntax = "proto3"; package student; // 定义联系人message message StudentInfo { string name = 1; bool isMale = 2; int32 age = 3; }

student.pb.cc和student.pb.h 是用cmake生成的

proto_test.cpp

#include #include "student.pb.h" #include int main(int argc,char** argv) { std::string data_str; { // 使⽤ PB 进⾏序列化,并将结果打印出来。 student::StudentInfo _student; _student.set_name("小豪豪"); _student.set_ismale(true); _student.set_age(20); if (!_student.SerializeToString(&data_str)) { std::cerr


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有