The code compiles as it should, but somehow, the binary file doesn't know where the library is.
❯ ldd myteams_cli
linux-vdso.so.1 (0x00007ffcbc1b4000)
libmyteams.so => not found
libc.so.6 => /usr/lib/libc.so.6 (0x00007414e0bf3000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007414e0e1d000)
Here is the Makefile, I know that it finds the library because it doesn't compile when I intentionally misspell the library name:
CFLAGS = -Wall -Wextra -Werror -L../libs/myteams -lmyteams
NAME = myteams_cli
SRC = src/main.c \
OBJ_DIR = obj
OBJ = $(SRC:%.c=$(OBJ_DIR)/%.o)
$(OBJ_DIR)/%.o: %.c
@mkdir -p $(@D)
@$(CC) $(CFLAGS) -c $< -o $@
all: $(NAME)
$(NAME): $(OBJ)
@mkdir -p $(@D)
gcc -o $(NAME) $(OBJ) $(CFLAGS)
clean:
rm -f $(OBJ_DIR)/*.o
rm -rf $(OBJ_DIR)/src
fclean: clean
rm -f $(NAME)
re: fclean all
Also, the binary must be executable as soon as the project is compiled. I can't just add the library path to my LD_LIBRARY_PATH environment variable every time I want to launch the program (which works, by the way), nor copy the library to /usr/lib, because that would require triggering the Makefile with sudo.
The
-Loption to the linker sets the link-time search path. That is only in effect when linking the program, it doesn't change the runtime search path which is where the runtime linker looks for libraries when it starts your program. As mentioned in the comments, for that you want to use the linker option-rpath. Most compilers don't know that option so you have to pass it through with (for GCC and clang)-Wl,-rpath.If you search the net for "setting runtime search path" and/or "rpath" you'll find much useful info.
In your makefile, you really need to make a distinction between linker options and compiler options; it's not good to put them all into the same variable like
CFLAGS.CFLAGSis (by convention) for compiler flags. You should useLDFLAGSfor linker flags, andLDLIBSfor libraries to link (by convention). So try something like:(usually we pass compiler flags to the linker as well, just in case)