我想构建一个可执行文件,在确定的相对路径中查找其依赖项。换句话说,我想分发一个包含2个元素的zip:可执行文件和带有共享库的文件夹。可执行文件必须能够在运行时找到这些共享库。
目前,我使用cmake
编译了项目。我正在使用cmake的命令find_library
来查找abs。依赖的路径。
这是CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8)
project( flowers )
find_library( VFC_LIB1 NAMES opencv_calib3d PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib )
find_library( VFC_LIB2 NAMES opencv_core PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib )
...
find_library( VFC_LIB16 NAMES opencv_videostab PATHS ${CMAKE_CURRENT_SOURCE_DIR}/lib )
SET(VFC_LIBS ${VFC_LIB1} ${VFC_LIB2} ... ${VFC_LIB16} )
SET(VFC_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/opencv)
IF (VFC_LIB1 AND VFC_LIB2 AND ... AND VFC_LIB16)
include_directories( ${VFC_INCLUDE} )
include_directories( FlowersDetector NeuronalNet )
add_executable( flowers FlowersDetector/Flowers.cpp FlowersDetector/FlowersDetector.cpp )
add_definitions(-std=c++11)
add_definitions(-D_DEBUG) # TODO remove this for release
target_link_libraries( flowers ${VFC_LIBS} )
ELSE (VFC_LIB1 AND VFC_LIB2 AND ... AND VFC_LIB16)
MESSAGE(FATAL_ERROR "OpenCV libraries not found")
ENDIF(VFC_LIB1 AND VFC_LIB2 AND ... AND VFC_LIB16)
...
表示其余部分与前一部分相同,但是会改变序列号。
例如,VFC_LIB1 AND VFC_LIB2 ... AND VFC_LIB16
表示alwais VFC_LIBx
,其中x
为3到15。
问题是find_library
用于获取绝对路径(我已经读过),因此不允许我将可执行文件复制粘贴到其他计算机上并且仍能运行它。 / p>
我需要的是找到具有相对路径的库,或其他等效解决方案。
我试图制作一个“大”的可执行文件,其中包含“内部”的所有库。为此,我已经读过我必须链接静态库。我重新编译了OpenCV构建静态库(现在我有* .a文件加* .so文件。
我更改了CMakeLists.txt以添加这些静态库:
cmake_minimum_required(VERSION 2.8)
project( flowers )
find_package( OpenCV REQUIRED )
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
SET(BUILD_SHARED_LIBRARIES OFF)
SET(CMAKE_EXE_LINKER_FLAGS "-static")
include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories( FlowersDetector NeuronalNet )
add_executable( flowers FlowersDetector/Flowers.cpp FlowersDetector/FlowersDetector.cpp )
add_definitions(-std=c++11)
add_definitions(-D_DEBUG)
target_link_libraries( flowers ${OpenCV_LIBS} )
cmake
命令运行正常,但当我尝试make
项目时,我获得了很多错误。实际上,它似乎只是一个错误,但多次回复。此错误为:statically linked applications requires at runtime the shared libraries from the glibc version used for linking
以下是错误列表,避免使用类似的错误:(如果完整列表可能有用,请告诉我,我会将其上传)
Linking CXX executable flowers
/usr/bin/ld: cannot find -latk-1.0
/usr/bin/ld: cannot find -lgdk_pixbuf-2.0
/usr/local/lib/libopencv_core.a(opencl_core.cpp.o): In function `(anonymous namespace)::opencl_fn14<41, int, _cl_command_queue*, _cl_mem*, unsigned int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long, unsigned long, unsigned long, unsigned long, void const*, unsigned int, _cl_event* const*, _cl_event**>::switch_fn(_cl_command_queue*, _cl_mem*, unsigned int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long, unsigned long, unsigned long, unsigned long, void const*, unsigned int, _cl_event* const*, _cl_event**)':
opencl_core.cpp:(.text._ZN12_GLOBAL__N_111opencl_fn14ILi41EiP17_cl_command_queueP7_cl_memjPKmS6_S6_mmmmPKvjPKP9_cl_eventPSA_E9switch_fnES2_S4_jS6_S6_S6_mmmmS8_jSC_SD_+0x1c0): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libgio-2.0.a(libgio_2_0_la-glocalfileinfo.o): In function `lookup_gid_name':
(.text+0x11a7): warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
[And many similar errors]
collect2: error: ld returned 1 exit status
CMakeFiles/flowers.dir/build.make:143: recipe for target 'flowers' failed
make[2]: *** [flowers] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/flowers.dir/all' failed
make[1]: *** [CMakeFiles/flowers.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
我已经测试了仅使用静态库(* .a)重新安装OpenCV并使用默认的OpenCV CMakeLists:
cmake_minimum_required(VERSION 2.8)
project( flowers )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories( FlowersDetector NeuronalNet )
add_executable( flowers FlowersDetector/Flowers.cpp FlowersDetector/FlowersDetector.cpp )
add_definitions(-std=c++11)
add_definitions(-D_DEBUG)
target_link_libraries( flowers ${OpenCV_LIBS} )
我可以编译所有,即使我可以执行程序,但是当程序试图显示图像时,我有以下错误:
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /media/ubuntu/67803d44-978a-493e-bdfb-5730e7e9f5a8/opencv-3.1.0/modules/highgui/src/window.cpp, line 527
terminate called after throwing an instance of 'cv::Exception'
what(): /media/ubuntu/67803d44-978a-493e-bdfb-5730e7e9f5a8/opencv-3.1.0/modules/highgui/src/window.cpp:527: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow
Aborted (core dumped)
我已安装了apt-get install
libgtk2.0-dev
和pkg-config
,并且我已下载并安装了ATK lib(来自http://www.linuxfromscratch.org/blfs/view/svn/x/atk.html)。
之后,我重新启动并重新运行cmake
和make
。当我执行程序时,错误仍然存在。
答案 0 :(得分:2)
在Linux上,可执行文件使用RPATH机制存储有关其库路径的信息。链接器将可执行文件加载到内存时,将使用此信息。
对于库 relative 的make路径,有一个特殊的路径组件$ORIGIN
,它被评估到带有可执行文件的目录。
在CMake中,对于可执行构建的set RPATH,使用变量CMAKE_INSTALL_RPATH:
# sets RPATH for *install*ed files
set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib") # Note CMake escaping aroung '$' sign.
# *built* files will also used RPATH which has been set before
set(CMAKE_BUILD_WITH_INSTALL_RPATH on)
# Use 'find_library' calls. They return absolute paths needs for *link* stage.
find_library(...)
...
set(VFC_LIBS ...)
# Create executable and link it using variables filled by 'find_library'
add_executable(flowers ...)
target_link_libraries(flowers ${VFC_LIBS})
现在,如果将可执行文件移动(或安装)到某个目录<dir>
,将所有库移动到目录<dir>/lib/
,则可以运行可执行文件。
有关CMake中RPATH处理的更多信息,请参阅CMake wiki。
答案 1 :(得分:0)
在运行可执行文件之前,您需要一种设置库搜索路径的方法。在Linux / Unix上,最简单的解决方案是shell脚本,您可以在其中相应地设置LD_LIBRARY_PATH。 Windows可能有类似的机制,但我没有相关经验。
在这两种情况下,CMake只会帮助您构建程序和库,它无法为您找到其他计算机上的内容。
答案 2 :(得分:0)
这不是你的问题的准确答案,但在你的特定场景中,我会在运行时加载库,以避免弄乱路径和加载错误的库。
在Linux / Mac上,这可以通过dlopen()在Windows上通过LoadLibrary()完成。