问题
I'm trying to compile a tensorflow custom op that requires OpenFST (http://www.openfst.org/twiki/bin/view/FST/WebHome). However, I'm running into compilation errors where I run:
import tensorflow as tf
decoder_op = tf.load_op_library('./libfst_decoder.so')
and get an undefined symbol error undefined symbol: _ZN3fst21ConvertToLegalCSymbolEPSs
, so it's not able to find the linked object.
This is the CMake file I'm using:
cmake_minimum_required(VERSION 3.5)
execute_process(COMMAND python3 -c "import tensorflow; print(tensorflow.sysconfig.get_include())" OUTPUT_VARIABLE Tensorflow_INCLUDE_DIRS)
set (CMAKE_CXX_FLAGS "--std=c++11 -fPIC -O2 -D_GLIBCXX_USE_CXX11_ABI=0")
link_directories("/miniconda/envs/py36/lib/python3.6/site-packages/tensorflow")
include_directories(${Tensorflow_INCLUDE_DIRS})
include_directories("/usr/local/include")
add_library(fst_decoder SHARED fst_decoder.cc simple_decoder.cc)
target_link_libraries(fst_decoder -ltensorflow_framework -lfst -ldl -lm)
Are there any obvious issues with the CMake file to include an external library? Don't have too much experience with compiling C++.
回答1:
Solution is to recompile OpenFST with:
make CFLAGS='-D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11' CXXFLAGS='-D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11'
as tensorflow uses D_GLIBCXX_USE_CXX11_ABI=0.
来源:https://stackoverflow.com/questions/56352297/how-to-link-openfst-to-tensorflow-custom-op