如何使我的应用程序独立于其位置链接库? [英] How can I make my application to link libraries independent of its location?
问题描述
我已经建立了一个共享库(即libabc.so
)和一个使用我的共享库的可执行文件(即myapp
).我已经将共享库和可执行文件都放在了文件系统中,但是当我运行可执行文件时,它会出现以下错误
I have built a shared library (i.e libabc.so
) and an executable (i.e myapp
) which uses my shared library. I have placed both the shared library and my executable in my filesystem but when I run my executable it gives me the following error
error while loading shared libraries: <target_lib_path>/<mylib>.so cannot open shared object file: No such file or directory.
现在,我的开发环境是在构建共享库后将另一个目标文件系统放置在~/targetfs
上,然后将其安装在~/targetfs/usr/local/abc/lib
中.在链接我的应用程序期间,我给它
Now my dev environment is I have a different target filesystem which is placed at ~/targetfs
after building my shared library I am installing it in ~/targetfs/usr/local/abc/lib
. During linking my application I give it
LDFLAGS += -L~/targetfs/usr/local/abc/lib
我的应用程序构建良好.但是,当我在~/targetfs
是我的文件系统的环境中运行我的应用程序时,我的应用程序会抱怨
加载共享库时出错:
My application builds fine. But when I run my application in an environment where ~/targetfs
is my filesystem, then my application complains
error while loading shared libraries:
/home/user/targetfs/usr/local/abc/lib/libabc.so: can not open shared object file. No such file or directory exist.
现在,当然,我的应用程序正在搜索不存在的共享库的路径,但是我希望我的应用程序独立于该路径,而应该在/lib
,/usr/lib
中寻找我的共享库. ,/usr/local/lib
或LD_LIBRARY_PATH
位置.
Now, of course the path my application is searching for the shared library that does not exist, but I want my application to be independent of this path, rather it should look for my shared library in /lib
, /usr/lib
, /usr/local/lib
or LD_LIBRARY_PATH
location.
如何使我的应用程序独立于其位置链接库?
How can I make my application to link libraries independent of its location?
我的共享库的文件&申请如下.
Makefiles for my shared library & application is given below.
--------------共享库Makefile. (忽略不必要的信息)
-------------- Shared library makefile. (Omitting un-necessary info)
CC = $(CROSS_COMPILE)gcc
CFLAGS = -Wall -shared -fpic
LDFLAGS = -Xlinker --gc-sections --allow-shlib-undefined
LIBRARY = libabc.so
OBJ_DIR = obj
SRC_DIR = src
CHK_DIR_EXISTS = test -d
MKDIR = mkdir -p
# Project Source Files
C_SOURCES += $(SRC_DIR)/abc.c
OBJECTS += $(OBJ_DIR)/abc.o
INCLUDES += -Iinc
$(LIBRARY): $(OBJECTS)
@echo ""
@echo "Linking..." $(LIBRARY)
@$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $(OBJ_DIR)/$(LIBRARY)
----------应用程序Makefile(省略不必要的信息)
---------- Application Makefile (Omitting un-necessary info)
LDFLAGS += $(TARGETFS)/usr/local/abc/lib/libabc.so \
-lpthread -lrt
任何人都在想我的Makefile中缺少什么.
Any thoughts what's missing in my Makefiles.
推荐答案
您可以要求链接器将多个搜索路径放入二进制文件中.您可以使用-Wl,rpath = ...选项介绍这些搜索路径.
You can ask the linker to put multiple search paths into the binary. You introduce those search paths with the -Wl,rpath=... option.
gcc -o abc abc.c
-L~/targetfs/usr/local/abc/lib
-labc
-Wl,-rpath=/usr/local/abc/lib
-Wl,-rpath=...
-Wl,-rpath=...
这篇关于如何使我的应用程序独立于其位置链接库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!