I am trying to link against the Tensorflow library. Here is my makefile:
TF_PATH=/home/wade/.local/lib/python3.10/site-packages/tensorflow
CFLAGS=-I$(TF_PATH)/include/ -L$(TF_PATH)/
CC=cc
artifaction : artifaction.o
$(CC) -o artifaction artifaction.o $(CFLAGS) -ltensorflow_cc
artifaction.o :
$(CC) -c artifaction.cc $(CFLAGS)
Located in $(TF_PATH)/ is libtensorflow_cc.so.2. I want to link against this shared library, so I provided cc with -L$(TF_PATH)/. Currently, I get this error:
cc -o artifaction artifaction.o -I/home/wade/.local/lib/python3.10/site-packages/tensorflow/include/ -L/home/wade/.local/lib/python3.10/site-packages/tensorflow/ -ltensorflow_cc
/usr/bin/ld: cannot find -ltensorflow_cc: No such file or directory
collect2: error: ld returned 1 exit status
make: *** [Makefile:7: artifaction] Error 1
OS: Ubuntu 20.04 LTS.
TF Version: 2.15.
I'm using cc here, but the same error occurs with g++ and clang.
How do I fix this?
Solution
The linker will always look for either a
lib<name>.soorlib<name>.a. If the library has a extension like.so.2, then the linker won't be able to find the library. The solution is to create asymlinkusing thelncommand that will redirect the linker to the.soyou want.Another solution is to insert a colon after the
-lflag. This tells the compiler to use the provided name without addingliborso. So you could do-l:libtensorflow.so.2.In my case, I ran
ln -s libtensorflow_cc.so.2 libtensorflow_cc.soandln -s libtensorflow_framework.so.2 libtensorflow.so, and then the linker was able to find the libs.