cc compiler can't find link library in provided path

66 Views Asked by At

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?

1

There are 1 best solutions below

1
Rylan Yancey On

Solution

The linker will always look for either a lib<name>.so or lib<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 a symlink using the ln command that will redirect the linker to the .so you want.

Another solution is to insert a colon after the -l flag. This tells the compiler to use the provided name without adding lib or so. So you could do -l:libtensorflow.so.2.

In my case, I ran ln -s libtensorflow_cc.so.2 libtensorflow_cc.so and ln -s libtensorflow_framework.so.2 libtensorflow.so, and then the linker was able to find the libs.