标题
[mediapipe] google mediapipe 库的编译问题及解决
clq
浏览(559) +
2022-04-09 18:35:26 发表
编辑
关键字:
[2022-04-15 14:27:06 最后更新]
[mediapipe] google mediapipe 库的编译问题及解决
google 做的这玩意。问题确实比较多。
1.
https://blog.csdn.net/qq_21743659/article/details/121991037
return tensor;
改为
return absl::StatusOr<mediapipe::Tensor> ( mediapipe::Tensor(std::move(tensor));
"本人尝试的是以上的方法(有时候还需要改另一个文件,这取决于你的报错是怎么样的,当时我是改了两个文件的),当然 issue 中还有一种办法是将 gcc7 升级为 gcc8 也解决了问题,这个我没有去尝试。"
上面漏掉了一个括号,应该是 return absl::StatusOr<mediapipe::Tensor> ( mediapipe::Tensor(std::move(tensor)));
2.
它用的 bazel 编译系统是要用到 java 的,所以最好是下载它带 java 的版本。比较奇怪的是这个系统其实就是一个文件。linux 下我直接放到 /usr/bin 中,windows 下我设置了 path 。
guest
2022-04-11 15:25:56 发表
编辑
加入本机编译器
bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 --crosstool_top=//toolchain:arm_suite --cpu=arm mediapipe/examples/desktop/hand_tracking:hand_tracking_cpu --jobs 1 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
guest
2022-04-11 15:30:14 发表
编辑
否则生成的 protoc 是不能在本机运行的。
guest
2022-04-15 09:24:59 发表
编辑
配置交叉编译环境后的 undeclared inclusion 应当用 cxx_builtin_include_directories 写明。
参考 https://docs.bazel.build/versions/main/tutorial/cc-toolchain-config.html
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
cxx_builtin_include_directories = [ # NEW
"/usr/lib/llvm-9/lib/clang/9.0.1/include",
"/usr/include",
],
toolchain_identifier = "local",
host_system_name = "local",
target_system_name = "local",
target_cpu = "k8",
target_libc = "unknown",
compiler = "clang",
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths,
)
修改前为
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
toolchain_identifier = "local",
host_system_name = "local",
target_system_name = "local",
target_cpu = "k8",
target_libc = "unknown",
compiler = "clang",
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths, # NEW
)
guest
2022-04-15 14:19:07 发表
编辑
实测下来,写清两处关键的路径群组最为重要。参考如下:
----------------------------------------------------------------
# ok 来自 https://zhuanlan.zhihu.com/p/336907030
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"feature",
"flag_group",
"flag_set",
"tool_path",
)
all_link_actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
#"contains_objc_source",
#"has_configured_linker_path",
#"symbol_counts",
#"shared_flag",
#"linkstamps",
#"output_execpath_flags",
#"runtime_root_flags",
#"input_param_flags",
#"strip_debug_symbols",
#"linker_param_file",
#"version_min",
# "apple_env",
#"sysroot",
#"cpp_linker_flags",
]
def _impl(ctx):
#compiler_flag: "-nostdinc", #clq add //不行,但可以在命令行中加 --copt="-mfloat-abi=hard"
tool_paths = [
# tool_path(
# name = "gcc",
# #path = "/usr/bin/clang",
# path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-clang",
# ),
tool_path(
name = "ld",
#path = "/usr/bin/arm-linux-gnueabihf-ld",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-ld",
),
tool_path(
name = "cpp",
# 这个指向 gcc 的路径
#path = "/usr/bin/arm-linux-gnueabihf-gcc",
# 不能在这里直接传递参数,要想别的办法
#path = "f:/k3/qnx660-new/host/win32/x86/usr/bin/qcc -V gcc_ntoarmv7le",
#path = "f:/k3/qnx660-new/host/win32/x86/usr/bin/qcc", //ok
# path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-cpp",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-g++",
),
tool_path(
name = "gcc",
# 这个指向 gcc 的路径
#path = "/usr/bin/arm-linux-gnueabihf-gcc",
#path = "f:/k3/qnx660-new/host/win32/x86/usr/bin/qcc -V gcc_ntoarmv7le",
# path = "f:/k3/qnx660-new/host/win32/x86/usr/bin/qcc", //ok
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-gcc",
),
tool_path(
name = "dwp",
# path = "/usr/bin/arm-linux-gnueabihf-dwp",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-dwp",
),
tool_path(
name = "gcov",
#path = "/usr/bin/arm-linux-gnueabihf-gcov",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-gcov",
),
tool_path(
name = "nm",
# path = "/usr/bin/arm-linux-gnueabihf-nm",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-nm",
),
tool_path(
name = "objdump",
# path = "/usr/bin/arm-linux-gnueabihf-objdump",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-objdump",
),
tool_path(
name = "objcopy",
# path = "/usr/bin/arm-linux-gnueabihf-objcopy",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-objcopy",
),
tool_path(
name = "strip",
# path = "/usr/bin/arm-linux-gnueabihf-strip",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-strip",
),
tool_path(
name = "ar",
#path = "/usr/bin/arm-linux-gnueabihf-ar",
path = "/home/stone/Desktop/1/gcc-arm/bin/arm-linux-gnueabihf-ar",
),
]
features = [
feature(
name = "default_linker_flags",
enabled = True,
flag_sets = [
flag_set(
actions = all_link_actions,
flag_groups = ([
flag_group(
flags = [
"-lstdc++",
],
),
]),
),
],
),
##################################
#symbol_counts_feature =
# feature(
# name = "symbol_counts",
# flag_sets = [
# flag_set(
# actions = all_link_actions,
# flag_groups = [
# # flag_group(
# # flags = ["-Wl,--print-symbol-counts=%{symbol_counts_output}"],
# # expand_if_available = "symbol_counts_output",
# # ),
# ],
# ),
# ],
# ),
##################################
# cc: unknown option: '-iquote' 错误时必须重新定义 "include_paths" 节点
# 因为继承下来的 "include_paths" 中有这个参数
# include_paths_feature =
# feature(
# name = "include_paths",
# enabled = True,
# flag_sets = [
# flag_set(
# actions = [
# ACTION_NAMES.preprocess_assemble,
# ACTION_NAMES.c_compile,
# ACTION_NAMES.cpp_compile,
# ACTION_NAMES.cpp_header_parsing,
# ACTION_NAMES.cpp_module_compile,
# ACTION_NAMES.linkstamp_compile,
# ACTION_NAMES.clif_match,
# ACTION_NAMES.objc_compile,
# ACTION_NAMES.objcpp_compile,
# ],
# flag_groups = [
# # flag_group(
# # #flags = ["-iquote", "%{quote_include_paths}"],
# # flags = [],
# # iterate_over = "quote_include_paths",
# # ),
# flag_group(
# flags = ["-I", "f:/k3/qnx660-new/target/qnx6/usr/include"],
# iterate_over = "quote_include_paths",
# ),
# flag_group(
# flags = ["-I%{include_paths}"],
# iterate_over = "include_paths",
# ),
# flag_group(
# flags = ["-isystem", "%{system_include_paths}"],
# iterate_over = "system_include_paths",
# ),
# ],
# ),
# ],
# ),
##################################
# cc: unknown option: '-iquote' 错误时必须重新定义 "include_paths" 节点
# 因为继承下来的 "include_paths" 中有这个参数
# include_paths_feature =
feature(
name = "include_paths",
enabled = True,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.clif_match,
ACTION_NAMES.objc_compile,
ACTION_NAMES.objcpp_compile,
],
flag_groups = [
# flag_group(
# #flags = ["-iquote", "%{quote_include_paths}"],
# flags = [],
# iterate_over = "quote_include_paths",
# ),
flag_group(
#flags = ["-I", "f:/k3/qnx660-new/target/qnx6/usr/include"],
flags = [
#"-I", "/home/stone/Desktop/1/gcc-arm/rootfs/usr/include",
#"-I", "/home/stone/Desktop/1/gcc-arm/rootfs/usr/include/linux",
#################################################
# 交叉编译时提示未找到的 include .h 文件的路径直接放到这里就行。提示哪个放哪个
#"-I", "/home/stone/Desktop/1/gcc-arm/arm-linux-gnueabihf/libc/usr/lib/include",
#"-I", "/home/stone/Desktop/1/gcc-arm/arm-linux-gnueabihf/libc/usr/include",
"-I", "/home/stone/Desktop/test1/k3_new/bazel_t1",
"-I", "/home/stone/Desktop/test1/k2_arm/bazel-k2_arm/external/com_google_absl",
# 注意这个生成了两个平台下的文件,不对或者混乱的话要换一个
#'/home/stone/Desktop/test1/k2_arm/bazel-out/arm-opt/bin/mediapipe/framework/formats/rect.pb.h'
"-I", "/home/stone/Desktop/test1/k2_arm/bazel-out/arm-opt/bin",
"-I", "/home/stone/Desktop/test1/k2_arm/bazel-k2_arm",
#################################################
],
iterate_over = "quote_include_paths",
),
flag_group(
flags = ["-I%{include_paths}"],
iterate_over = "include_paths",
),
flag_group(
flags = ["-isystem", "%{system_include_paths}"],
iterate_over = "system_include_paths",
),
],
),
],
),
##########################################
# 类似上面的,不清楚是什么节点,总之它会产生多余的编译项
#dependency_file_feature =
# feature(
# name = "dependency_file",
# enabled = True,
# flag_sets = [
# flag_set(
# actions = [
# ACTION_NAMES.assemble,
# ACTION_NAMES.preprocess_assemble,
# ACTION_NAMES.c_compile,
# ACTION_NAMES.cpp_compile,
# ACTION_NAMES.cpp_module_compile,
# ACTION_NAMES.objc_compile,
# ACTION_NAMES.objcpp_compile,
# ACTION_NAMES.cpp_header_parsing,
# ACTION_NAMES.clif_match,
# ],
# flag_groups = [
# # flag_group(
# # flags = ["-MD", "-MF", "%{dependency_file}"],
# # expand_if_available = "dependency_file",
# # ),
# ],
# ),
# ],
# ),
##########################################
# 这个似乎也是多余的
#random_seed_feature =
# feature(
# name = "random_seed",
# enabled = True,
# flag_sets = [
# flag_set(
# actions = [
# ACTION_NAMES.c_compile,
# ACTION_NAMES.cpp_compile,
# ACTION_NAMES.cpp_module_codegen,
# ACTION_NAMES.cpp_module_compile,
# ],
# flag_groups = [
# # flag_group(
# # flags = ["-frandom-seed=%{output_file}"],
# # expand_if_available = "output_file",
# # ),
# ],
# ),
# ],
# )
##########################################
]
##########################################
##########################################
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
#features = [],
cxx_builtin_include_directories = [
# "/usr/arm-linux-gnueabihf/include",
# "/usr/lib/gcc-cross/arm-linux-gnueabihf/9",
"/home/stone/Desktop/1/gcc-arm/include",
####################################################
# 下面是暴力参数 #//注意,使用映射后的目录名才安全
"/home/stone/Desktop/test1/k2_arm/bazel-k2_arm/external/com_google_absl",
"/home/stone/Desktop/1/gcc-arm/arm-linux-gnueabihf/libc/usr/include",
"/home/stone/Desktop/1/gcc-arm/arm-linux-gnueabihf/include/c++/5.3.1",
"/home/stone/Desktop/1/gcc-arm/lib/gcc/arm-linux-gnueabihf/5.3.1/include-fixed",
"/home/stone/Desktop/1/gcc-arm/lib/gcc/arm-linux-gnueabihf/5.3.1/include",
"/home/stone/Desktop/test1/k2_arm/bazel-out/arm-opt/bin/mediapipe/calculators/util",
"/home/stone/Desktop/test1/k2_arm/bazel-out/arm-opt/bin/mediapipe/framework",
"/home/stone/Desktop/test1/k2_arm/bazel-out/arm-opt/bin/mediapipe/util",
"/home/stone/Desktop/test1/k2_arm/bazel-k2_arm/mediapipe/framework",
"/home/stone/Desktop/test1/k2_arm/bazel-k2_arm/mediapipe/calculators/tensor",
######## 使用以上参数可以轻松到达 opencv 阶段
"/home/stone/Desktop/test1/k2_arm/bazel-out/arm-opt/bin/mediapipe/calculators/internal",
"/home/stone/Desktop/test1/k2_arm/bazel-k2_arm/mediapipe/util",
"/home/stone/Desktop/test1/k2_arm/bazel-k2_arm/mediapipe/gpu",
#########################################################
# 以上这些参数都是为了通过 bazel 的 .h 文件整个路径不在范围提示而已。提示哪个就把它的路径加进来即可。
# 注意,这和 "include_paths" 处的不同, "include_paths" 实际上还是写的半截相对路径需要的绝对路径。
# 而这里是真正的绝对路径,除最后一个 / 符号后的整个完整路径都要写上。
#########################################################
],
toolchain_identifier = "arm-toolchain",
host_system_name = "local",
target_system_name = "local",
target_cpu = "armv7hf",
#target_cpu = "arm",
target_libc = "unknown",
#compiler = "clang",
# compiler = "clang2", # 这个很重要
compiler = "gnu",
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths,
)
arm_toolchain_config = rule(
implementation = _impl,
attrs = {},
# provides = [CcToolchainConfigInfo],
)
guest
2022-04-15 14:24:18 发表
编辑
对应的编译命令如下,一定要看清楚了,只要一个命令就行,并不需要其他文章所说的 cro.... 什么文件,也不用什么运行一次 config option 什么的。
就和本机编译一样的,只是参数指明用哪个 cpu 架构。另外,mediapipe 很特殊的一点在于,它还要指明本机编译环境,因为它用到了 google 的 protoc 程序来生成
协议相关的代码 c 文件,而这个显然是要编译成本机程序才能跑。设置方法也很简单,直接指定 bazel 的默认 cc 编译就可以了。
------------------------------------------------------------------------
bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 --crosstool_top=//toolchain:arm_suite --cpu=arm mediapipe/examples/desktop/hand_tracking:hand_tracking_cpu --jobs 1 --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --verbose_failures --cxxopt=-fpermissive
------------------------------------------------------------------------
guest
2022-04-15 14:27:06 发表
编辑
其中的 c++17 其实只是在 media 的某些 std::string 操作中使用到。直接修改代码就兼容了我的 gcc 5.3.1 的交叉 armv7l 编译器了。
google 的某些开发人员也是够得坑的。实际上其中引用的大量 google 基础库反而并没有用到所谓的 c++17 特性。
NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.