问题
Question
How should I install the TensorFlow C library on MacOS or otherwise address the errors:
dyld: Library not loaded: @rpath/libtensorflow.1.dylib
Referenced from: /Users/Me/./hello_tf
Reason: image not found
Abort trap: 6
and
dyld: Symbol not found: __cg_DGifCloseFile
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libGIF.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Abort trap: 6
Context
I followed the installation instructions for MacOS using the “macOS CPU only” library verbatim until the sudo ldconfig
bit. This being Mac, I substituted update_dyld_shared_cache
for ldconfig
and restarted, then built the example program provided.
#include <stdio.h>
#include <tensorflow/c/c_api.h>
int main() {
printf("Hello from TensorFlow C library version %s\n", TF_Version());
return 0;
}
I got:
~$ gcc hello_tf.c -ltensorflow -o hello_tf
~$ ./hello_tf
dyld: Library not loaded: @rpath/libtensorflow.1.dylib
Referenced from: /Users/Me/./hello_tf
Reason: image not found
Abort trap: 6
Following the instructions’ advice, I rebuild with gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf
and got the same result.
The installation instructions say to set LIBRARY_PATH
and DYLD_LIBRARY_PATH
for installs in non-system directories. I decided to give it a shot:
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/lib
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/lib
Compiling with both methods (that is, without and with -I/usr/local/include -L/usr/local/lib
) now give me:
dyld: Symbol not found: __cg_DGifCloseFile
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libGIF.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Abort trap: 6
(Well, at least it’s a different error.)
My /usr/local/lib
contains the TensorFlow files:
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
libtensorflow.dylib
and libtensorflow.1.dylib
are links that point to libtensorflow.1.14.0.dylib
, and libtensorflow_framework.dylib
and libtensorflow_framework.1.dylib
likewise point to libtensorflow.1.14.0.dylib
, as in the provided .tar.gz
archive.
Desired Outcome
The primary desired effect is that the following occurs:
~$ gcc hello_tf.c -ltensorflow -o hello_tf
~$ ./hello_tf
Hello from TensorFlow C library version 1.14.0
(That’s the version I’m using.)
A secondary desired effect is understanding of why the mentioned errors occurred.
回答1:
I fixed it by installing with Homebrew, adding /usr/local/lib
to LD_LIBRARY_PATH
, and compiling with the -ltensorflow
flag as the last of the GCC arguments. It seems dyld is rather picky; if possible, I’d still like to have a better explanation than that.
~$ brew install libtensorflow
# Homebrew output omitted
~$ export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib"
~$ gcc hello_tf.c -o hello_tf -ltensorflow
~$ ./hello_tf
TensorFlow C lib version 1.14.0
This also didn’t work with the modifications to DYLD_LIBRARY_PATH
and LIBRARY_PATH
mentioned in the question.
It might be wise to add /usr/local/lib
to LD_LIBRARY_PATH
in ~/.bash_profile
or some build script.
来源:https://stackoverflow.com/questions/57916950/faulty-tensorflow-c-install-on-macos-dyld-errors-library-not-loaded-rpath-l