gpt4 book ai didi

c++ - CMake 共享(或静态?)库无法链接

转载 作者:行者123 更新时间:2023-12-02 17:25:26 24 4
gpt4 key购买 nike

我目前正在尝试使用 CMake 在 ARM 平台上使用一些共享和静态预编译库来编译项目。这是项目结构的概述。

<main project directory>
|-- CMakeLists.txt
|-- face_detect.cpp
|-- face_detect.hpp
|-- video_test.cpp
|-- build
| |-- <various CMake build files>

这是我要链接的库所在的目录结构。
</usr/share/ti>
|-- opencl
| |-- <various header files for TI OpenCL including dsp.h>
| |-- dsp.out
| |-- dsp.syms
| |-- dsp_syms.obj
|-- tidl
| |-- tidl_api
| | |-- inc
| | | |-- congfiguration.h
| | | |-- execution_object.h
| | | |-- execution_object_internal.h
| | | |-- execution_object_pipeline.h
| | | |-- executor.h
| | | |-- imgutil.h
| | |-- tidl_api.a
| | |-- tidl_imgutil.a
| | |-- tidl.so

现在解释一下:

我现在正在使用带有 dnn 模块的 OpenCV 在 BeagleBoard AI(TI Sitara 处理器)上进行面部检测(并最终识别)。代码在没有 TI 深度学习 API (TIDL) 的情况下编译并运行得很好,但由于某种原因,当我尝试 make 时它引发以下错误的项目(构建良好,在链接阶段失败):
face_detect.cpp:(.text+0x16): undefined reference to `tidl::Executor::GetNumDevices(tidl::DeviceType)'
collect2: error: ld returned 1 exit status
CMakeFiles/face_detect.dir/build.make:142: recipe for target 'face_detect' failed
make[2]: *** [face_detect] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/face_detect.dir/all' failed
make[1]: *** [CMakeFiles/face_detect.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
nm tidl_api.a | grep GetNumDevices显示符号确实在库中,所以我认为这一定是CMake的问题。此外,开发板附带的示例利用 TIDL API 可以正常工作,因此我知道 API 可以正常工作。

什么可能导致链接器失败?我将把我的 CMakeLists.txt 文件放在下面。我在那里尝试了一些不必要的线路以使其工作。感谢您的关注!
project( hello_face )

cmake_minimum_required( VERSION 3.7 )

set( CMAKE_CXX_STANDARD 14 )
set( CMAKE_CXX_STANDARD_REQUIRED True )
set( OpenCV_DIR /opt/opencv-4.2.0/lib/cmake/opencv4 )

find_package( OpenCV REQUIRED )

include_directories( "/usr/share/ti/tidl/tidl_api/inc" )
include_directories( "/usr/share/ti/tidl/tidl_api" )
include_directories( "/usr/share/ti/opencl" )
link_directories( "/usr/share/ti/tidl/tidl_api" )
link_directories( "/usr/share/ti/tidl/tidl_api/inc" )
link_directories( "/usr/share/ti/opencl" )

add_library( tidl SHARED IMPORTED )
set_property( TARGET tidl PROPERTY IMPORTED_LOCATION "/usr/share/ti/tidl/tidl_api/tidl.so" )

add_library( tidl_api STATIC IMPORTED )
set_property( TARGET tidl_api PROPERTY IMPORTED_LOCATION "/usr/share/ti/tidl/tidl_api/tidl_api.a" )

add_library( tidl_imgutil STATIC IMPORTED )
set_property( TARGET tidl_imgutil PROPERTY IMPORTED_LOCATION "/usr/share/ti/tidl/tidl_api/tidl_imgutil.a" )

add_executable( video_test video_test.cpp )
target_link_libraries( video_test ${OpenCV_LIBS} )

add_executable( face_detect face_detect.cpp )
target_link_libraries( face_detect ${OpenCV_LIBS} ${tidl_LIBS} ${tidl_api_LIBS} ${tidl_imgutil_LIBS} )

编辑:根据 squareskittles 的建议,我将 CMakeLists.txt 的最后一行更改为 target_link_libraries( face_detect PUBLIC ${OpenCV_LIBS} tidl tidl_api tidl_imgutil ) .我现在得到一长串未定义的 Python 方法/对象引用列表,例如 tidl.so 中的“PyFloat_Type”和“PyList_New”。 .不知道为什么它依赖于纯 c++ 项目的 Python 类型。

最佳答案

add_library() CMake 中的行定义 IMPORTED tidl 的目标图书馆。您可以直接链接到这些。因为这些是 目标 ,而不是 变量 , 你不需要使用 ${}变量取消引用的语法。此外,无需附加 _LIBS后缀。 _LIBS后缀通常用于在调用 find_package() 期间定义的变量。 ,但您只使用 find_package()在这里使用 OpenCV。为您的 target_link_libraries() 尝试类似的操作称呼:

target_link_libraries(face_detect PUBLIC ${OpenCV_LIBS} tidl tidl_api tidl_imgutil)

关于c++ - CMake 共享(或静态?)库无法链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59671634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com