C/C++ TensorFlow hello_tf.c:(.text+0xa):对"TF_Version"的未定义引用

C/C++ TensorFlow hello_tf.c:(.text+0xa): undefined reference to `TF_Version'

本文关键字:TF Version 引用 未定义 TensorFlow C++ hello tf text+0xa      更新时间:2023-10-16

你好,我正在尝试将TensorFlow用于C/C++。目前,我正在尝试让他们在安装说明中提供的基本程序进行编译。

https://www.tensorflow.org/install/install_c

我正在运行Ubuntu的x86_64机器上工作。我相信我把图书馆安装得很好。当我列出/usr/local/文件夹中的目录时,我会在其中看到tensorflow。

$:ls/usr/local/include/

tensorflow

然而,当我尝试编译他们提供的基本程序时,它在gcc和g++中都失败了

$:gcc-o测试hello_tf.cpp

/tmp/cczK3WZs.o:在函数"main"中:

hello_tf.cpp:(.text+0x5):对`tf_Version'的未定义引用

collect2:错误:ld返回1退出状态

$:g++-o测试hello_tf.cpp

/tmp/cl7FitR.o:在函数"main"中:

hello_tf.cpp:(.text+0x5):对`tf_Version'的未定义引用

collect2:错误:ld返回1退出状态

除了按照建议运行ldconfig以安装到系统目录之外,安装时一切都很顺利。

$sudo ldconfig/sbin/ldconfig.real:

/usr/lib/libusbredirparser.so.1不是符号链接

/sbin/ldconfig.real:/usr/lib/libid3tag.so.0不是符号链接

/sbin/ldconfig.real:/usr/lib/libusbg.so.0不是符号链接

/sbin/ldconfig.real:/usr/lib/libusbmuxd.so.2不是符号链接

/sbin/ldconfig.real:/usr/lib/libmtp.so.9不是符号链接

/sbin/ldconfig.real:/usr/lib/libmad.so.0不是符号链接

/sbin/ldconfig.real:/usr/lib/libusb-1.0.so.0不是符号链接

/sbin/ldconfig.real:/usr/lib/libusbredirhost.so.1不是符号链路

尽管如此,这些文件似乎不会影响相关程序的编译。当我运行疑难解答编译命令时,它似乎可以工作。我不知道为什么这是

g++-I/usr/local/include-L/usr/local/lib hello_tf.cpp-lensorflow

hello_tf.cpp文件被编译为对象。因为在您的程序内部,您调用的函数名称编译器无法在您的代码链接器内部找到,所以需要查找其他地方来找到它们,以允许对象调用这些函数的代码。选项L(-L/usr/local/lib)告诉它应该在哪些目录中查找,L(-L tensorflow)告诉它应该检查哪些模块。请记住,-l应该出现在翻译单元(hello_tf.cpp)之后。


在这里发布问题之前,你应该做一些研究。您链接的页面将"-L"替换为导出LIBRARY_PATH。。。但我不认为你的程序在没有-l的情况下编译。

我遇到了同样的问题,但我的情况不同。我不明白为什么,但g++的顺序或参数在CentOSUbuntu上不同。

这在CentOS上没有错误,但在Ubuntu:上失败

#source file comes last
g++ -ltensorflow -o prog prog.cpp

在Ubuntu:上它必须按以下顺序

#source file comes first
g++ prog.cpp -o prog -ltensorflow

Ubuntu中的订单很重要。这条线给我一个错误

gcc-I~/include-L~/lib hello_tf.c-o hello_tf

/tmp/ccAXMBn1.o:在函数main': hello_tf.c:(.text+0x5): undefined reference toTF_Version'中

解决方案:

gcc-I~/include-L~/lib hello_tf。c-lensorflow-o hello_tf

/hello_tf