Tips:如果我的文章被vip可看,请私信联系我修改。
下面的两个链接,是参考文章
虚幻引擎随笔:接入 Google Protobuf 库 - 知乎https://zhuanlan.zhihu.com/p/556147044
在protobuf v22.0+之后的版本,官方不再提供对应语言的分支,需要下载完整版进行构建。这里使用了vcpkg进行构建(会落后于gihub版本),操作相对简单,会把相关依赖导入。
因为22.0+后面abseil被整体分离,需要单独导入到unreal里。即使是把abseil导入到虚幻内,在代码编译时,会因为内部函数报错。
同时代码编译器会在cpp下产生如下警告:(这个问题可使用编辑器提示修复)
这两个问题会导致虚幻无法正确编译。到这,其实可以使用22.10版本,这个版本不需要复杂的处理,仍然可用。或者修改abseil文件,使其支持。
上面的原因,在protobuf的issue中提交过一个。但是没有解决方案ABSEIL-CPP、ABSL 与 ABSEIL-CPP、UNREAL5 项目中的 ABSL 发生第三方冲突 ·问题 #16450 ·协议缓冲区/protobufhttps://github.com/protocolbuffers/protobuf/issues/16450最终,通过google,在虚幻论坛上搜索到了:abseil-cpp, absl in thirdparty redefinition error - Programming & Scripting - Epic Developer Community Forumsi am trying to include protobuf lastest version 3.26.1 (ver 2024-04) in unreal 5. (i am using vs2022 community) when i include protobuf using thirdparty abseil-cpp, i face compile error C2373(redefinition). like below …
https://forums.unrealengine.com/t/abseil-cpp-absl-in-thirdparty-redefinition-error/1794996
需要修改两个文件: btree.h和
btree_container.h文件。原因是函数verify()定义在absl/container/internal/btree中。H和absl/container/internal/btree。h文件与UE5中定义的验证(expr)宏冲突。需要修改代码,在相关行上下移除相关定义
搜索相关错误代码所在行段,在前后文加上(不同版本行号可能不同)
btree.h
#ifdef verify
#undef verify
#endif
// Verifies the structure of the btree.
void verify() const;
#ifndef verify
#define verify(expr) UE_CHECK_IMPL(expr) // copy from line 221 of /Engine/Source/Runtime/Core/Public/Misc/AssertionMacros.h
#endif
#ifdef verify
#undef verify
#endif
template <typename P>
void btree<P>::verify() const {
assert(root() != nullptr);
assert(leftmost() != nullptr);
assert(rightmost() != nullptr);
assert(empty() || size() == internal_verify(root(), nullptr, nullptr));
assert(leftmost() == (++const_iterator(root(), -1)).node_);
assert(rightmost() == (--const_iterator(root(), root()->finish())).node_);
assert(leftmost()->is_leaf());
assert(rightmost()->is_leaf());
}
#ifndef verify
#define verify(expr) UE_CHECK_IMPL(expr) // copy from line 221 of /Engine/Source/Runtime/Core/Public/Misc/AssertionMacros.h
#endif
btree_container.h
#ifdef verify
#undef verify
#endif
void verify() const { tree_.verify(); }
#ifndef verify
#define verify(expr) UE_CHECK_IMPL(expr) // copy from line 221 of /Engine/Source/Runtime/Core/Public/Misc/AssertionMacros.h
#endif
至此,修复完成。
修改后的仓库参考
https://github.com/RSJWY/ProtobufAndAbseil-Unreal5https://github.com/RSJWY/ProtobufAndAbseil-Unreal5