问题
I'm programming using C++ and Qt Creator. I need to include the libraries I'm using inside my project folder so that the program can be implemented on any device. What should I do exactly?
I know I should modify the .pro
file and I already tried this:
LIBS+=-L"$$_PRO_FILE_PWD_/libs" \
-lvl \
But it does not work. I get this error: error while loading shared libraries: libvl.so: cannot open shared object file: No such file or directory
Any ideas?? Thanks
回答1:
First of all, on Linux qmake generates Makefiles which are used to control your build process. When you add the line
LIBS+=-L"$$_PRO_FILE_PWD_/libs" -lvl
to your .pro
file, qmake will see to it that appropriate Makefile target is generated which tells the linker to search for additional libraries in $$_PRO_FILE_PWD_/libs
and link to a library libvl.so
when linking the executable.
However what you're experiencing is a run-time problem. More specifically, ld-linux.so.2
will try to find and dynamically load shared libraries like libvl.so
at runtime. This happens using built-in, such as /usr/lib
and user-defined (and/or distribution-defined) paths when the executable is loaded. I refer you to the corresponding man page for ld-linux.so.2
and the man page for ldconfig
, which is used to configure user-defined library search paths in a standard way.
When ld-linux.so.2
tries to find libraries, it searches a well defined set of directories (all colon-separated) in the following order:
- if present, the so called
DT_RPATH
, which can be written into an executable library (deprecated). - the
LD_LIBRARY_PATH
(at least for most executables, see the man page for exceptions) - the
DT_RUNPATH
which supersedes theDT_RPATH
(if any) but delays lookup until afterLD_LIBRARY_PATH
has been processed. TheRPATH
andRUNPATH
can be used to simulate behavior like on Windows, where the path of the executable is also searched.LD_LIBRARy_PATH
, however, is much better suited for this purpose. - library names present in the cache file
/etc/ld.so.cache
which is generated byldconfig
using directories specified in/etc/ld.so.conf/
and possibly additional files either directly referred to via inclusion inld.so.conf
or otherwise specified - the trusted directories,
/lib
and/usr/lib
If none of the paths mentioned above contain the appropriate shared object, you will get an error that it could not be loaded by ld-linux.so.2
.
The solution in your case is simple and come in some variety:
- before executing the program, set the
LD_LIBRARY_PATH
environment variable usingexport LD_LIBRARY_PATH={yourSearchPaths}
. - add the
LD_LIBRARY_PATH
to the invocation of the executable on the command-line, e.g.LD_LIBRARY_PATH={yourSearchPaths} ./{executable}
- provide an executable start-up shell script, e.g.
start.sh
, which does the above for you and then simply execute the shell-script./start.sh
来源:https://stackoverflow.com/questions/18977420/include-libraries-into-my-project-folder