我正在尝试使用安装了pip的二进制文件来编译自定义tensorflow op,但是出现很多undefined reference
错误。我不熟悉c ++,因此不知道为什么会收到这些错误,因为我#include
一切,甚至在必要时添加了using ...
行。据我了解,错误与链接有关,我可能缺少-L
文件夹,但the TF guide for adding a new op建议的tf.sysconfig.get_link_flags()
只是返回一个空列表。
这是我用来编译的命令:
"D:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\g++.exe"
-std=c++11 -shared soft_hot_encode.cc -o soft_hot_encode.so -fPIC
-I "D:\Python\Python36\lib\site-packages\tensorflow\include"
-I "D:\Python\Python36\Lib\site-packages\tensorflow\include\google\protobuf_archive\src"
-I "D:\Python\Python36\Lib\site-packages\tensorflow\include\tensorflow\include\external\com_google_absl"
-I "D:\Python\Python36\Lib\site-packages\tensorflow\include\tensorflow\include\external\eigen_archive"
-D_GLIBCXX_USE_CXX11_ABI=0 -O2 -w
错误看起来像这样:
D:\AppData\Local\Temp\ccZAntyd.o:soft_hot_encode.cc:(.text+0x2f): undefined reference to `tensorflow::OpKernel::OpKernel(tensorflow::OpKernelConstruction*)'
D:\AppData\Local\Temp\ccZAntyd.o:soft_hot_encode.cc:(.text+0x85): undefined reference to `tensorflow::shape_inference::InferenceContext::MakeDimForScalarInput(int, tensorflow::shape_inference::DimensionHandle*)'
D:\AppData\Local\Temp\ccZAntyd.o:soft_hot_encode.cc:(.text+0xf5): undefined reference to `tensorflow::shape_inference::InferenceContext::Subshape(tensorflow::shape_inference::ShapeHandle, long long, long long, tensorflow::shape_inference::ShapeHandle*)'
D:\AppData\Local\Temp\ccZAntyd.o:soft_hot_encode.cc:(.text+0x138): undefined reference to `tensorflow::shape_inference::InferenceContext::Subshape(tensorflow::shape_inference::ShapeHandle, long long, tensorflow::shape_inference::ShapeHandle*)'
D:\AppData\Local\Temp\ccZAntyd.o:soft_hot_encode.cc:(.text+0x187): undefined reference to `tensorflow::shape_inference::InferenceContext::Vector(tensorflow::shape_inference::DimensionOrConstant)'
...
编辑:
我通过使用bazel和tensorflow源进行编译解决了此问题。为了使其能够正确编译,我需要通过在先前克隆的存储库文件夹中调用git checkout 8e423e3d56390671f0d954c90f4fd163ab02a9c1
来使用适当版本的源(在我的情况下为v2.0.0-beta1,提交:8e423e3)。之后,我按照tensorflow guide to compiling from source设置了边框。