简体   繁体   中英

C Shared Library : No Such File or Directory Upon Execution

first off, this is a programming assignment so you are aware.

Anyhow, what I am trying to do is include a shared library I wrote (a linked list from a previous project) in with my own shell I am writing. The issue I incur is that when I compile using my Makfile, the compile is successful. Then when I try to run my executable (let's say it's called prog), I get the following:

[terminal]# ./prog
./prog: error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory

The following is my file structure for reference:

include
  |_
    common.h
    List.h
    Node.h
lib
  |_
    libmylib.a
    libmylib.so
libsrc
  |_
    Makefile // This makefile builds the library correctly and puts it in lib via 'make install'
    List.c
    List.h
    Node.c
    Node.h
    common.h
Makefile
prog.c

Here is my main Makefile

CC=gcc
CFLAGS=-g -Wall -Llib
LIBS=-lreadline -lncurses -lmylib

PROGS=library prog

all: $(PROGS)

library:
    cd libsrc; make install

prog: prog.o
    $(CC) $(CFLAGS) -o $@ $< $(LIBS)

clean:
    cd libsrc; make installclean
    /bin/rm -f *.o $(PROGS) a.out core  *.log

Any help or advice is appreciated, thanks!

The runtime dynamic linker does not know where to find your shared library.

Two options:

  1. Set the LD_LIBRARY_PATH environment variable to include the absolute path to your lib directory:

    LD_LIBRARY_PATH=/path/to/lib

    export LD_LIBRARY_PATH

  2. Hard-code the absolute path to your lib directory in the executable image by passing -R/path/to/lib to the linker (eg in your makefile, CFLAGS=... -Llib -R/path/to/lib .

The first option is flexible, in the sense that the shared library can be installed anywhere and even moved to another location, and the executable won't break as long as the environment variable is updated accordingly. But it does require the user (or system administrator) must set up the environment correctly.

The second option does not allow the shared library to be moved from its predefined installation directory, but removes the dependencies on a correctly setup environment.

Note that you won't need to do either if you install your shared library in a standard system-specific location (eg /usr/lib or /usr/lib64 on Unix/Linux) as the runtime linker will search such locations automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM