【发布时间】:2016-04-29 08:05:10
【问题描述】:
我想构建一个可执行文件,在确定的相对路径中查找其依赖项。换句话说,我想分发一个包含 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 表示始终为VFC_LIBx,其中x 是从 3 到 15。
探测
问题是find_library 用于获取绝对路径(我已阅读),因此我无法将可执行文件复制粘贴到其他计算机并仍然能够运行它。
我需要的是找到具有相对路径的库,或其他等效解决方案。
编辑
我试图制作一个“大”可执行文件,其中包含“里面”的所有库。为此,我读到我必须链接静态库。我已经重新编译 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
编辑 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 installlibgtk2.0-dev和pkg-config,并且我已经下载并安装了ATK库(来自http://www.linuxfromscratch.org/blfs/view/svn/x/atk.html)。
之后,我重新启动并重新运行cmake 和make。当我执行程序时,错误仍然存在。
【问题讨论】:
-
“编辑”部分的错误很容易用谷歌搜索。这意味着,静态链接不适合使用
getgrgid_r的应用程序,因为此功能是目标机器特定的(例如,它可以由该机器的系统管理员更改)。跨度> -
这是一个警告。我想错误是缺少
atk-1.0和gdk-pixbuf。我已经尝试安装它们,但错误仍然存在。 -
只是为了确定:您的构建机器上是否安装了
-dev版本的atk-1.0和gdk-pixbuf?
标签: c++ linker cmake static-linking