QT集成Protobuf

1.定义protobuf

复制代码
package ImageRender;

message MessagePadding
{
  required int32 left = 1;
  required int32 top = 2;
  required int32 right = 3;
  required int32 bottom = 4;
}

2.通过protoc.exe编译proto文件

一般可以写一个bat来执行命令:

复制代码
%~d0
cd %~p0

rem The following adds the paths of both tf.exe and MsBuild.exe.
set PATH="E:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\cmd";C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%

rem generate cpp code
"./protoc.exe"  --proto_path =./  --cpp_out=../ProtoInclude  ./xxxxx.proto

pause

错误解决

  1. 找不到生成的.h头文件

    #设置pb生成的文件目录
    set(PROJECT_PROTO_DIR {PROJECT_SOURCE_DIR}/ProtoInclude) message(STATUS "PROJECT_PROTO_DIR is {PROJECT_PROTO_DIR}")

    #定义头文件需要寻址的路径
    include_directories(
    XXXXX
    ${PROJECT_PROTO_DIR}
    XXXXX
    )

  2. 编译过程中出现LINK2019的问题:

解决:

需要加入依赖的lib【libprotobuf64与libprotobuf-lite】,并把自动生成的cc文件引入到项目。

复制代码
set(PB_CC_FILES
    ${PROJECT_SOURCE_DIR}/ProtoInclude/xxxxx.pb.cc
)

#把目录src和inc下面的所有文件作为变量存储
file(GLOB_RECURSE SRC_FILES "src/*")
file(GLOB_RECURSE INC_FILES "include/*")

add_executable(DemoApp1 ${SRC_FILES} ${INC_FILES} ${PB_CC_FILES})
target_link_libraries(DemoApp1
         XXXXX
        libprotobuf64
        libprotobuf-lite
)
相关推荐
Mr YiRan1 天前
C++面向对象继承与操作符重载
开发语言·c++·算法
一只鹿鹿鹿1 天前
智慧水利一体化建设方案
大数据·运维·开发语言·数据库·物联网
没有医保李先生1 天前
字节对齐的总结
java·开发语言
Elastic 中国社区官方博客1 天前
使用 Elastic 进行网络监控:统一网络可观测性
大数据·开发语言·网络·人工智能·elasticsearch·搜索引擎·全文检索
Codefengfeng1 天前
Python Base环境中加包的方法
开发语言·python
清水白石0081 天前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
六件套是我1 天前
无法访问org.springframeword.beans.factory.annotation.Value
java·开发语言·spring boot
S-码农1 天前
Linux ——条件变量
linux·开发语言
清水白石0081 天前
《Python 编程全景解析:从核心精要到 Hypothesis 属性基测试的边界探索》
开发语言·python