我正在尝试编译 cpp,但出现如下错误:
warning: undefined symbol: _ZN2cv3MatC1EiiiPvm (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3MatD1Ev (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v2024052113blobFromImageERKNS_11_InputArrayEdRKNS_5Size_IiEERKNS_7Scalar_IdEEbbi (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v202405213Net19setPreferableTargetEi (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v202405213Net20setPreferableBackendEi (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v202405213Net7forwardERKNSt3__212basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEE (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v202405213Net8setInputERKNS_11_InputArrayERKNSt3__212basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEdRKNS_7Scalar_IdEE (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v202405213NetD1Ev (referenced by top-level compiled C/C++ code)
warning: undefined symbol: _ZN2cv3dnn14dnn4_v202405217readNetERKNSt3__212basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEESA_SA_ (referenced by top-level compiled C/C++ code)
我的 CMakeLists 如下所示:
set(OPENCV_DIR "/usr/local/include/opencv4")
set(OPENCV_WASM_DIR "src/build_wasm")
# set(OPENCV_DIR "/Volumes/Ficheiros/Dev/opencv")
cmake_minimum_required(VERSION 3.22)
project(yolo_cpp_webassembly_video_object_detector)
# Use C++ 17 by default
set(CMAKE_CXX_STANDARD 17)
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )
# Set Release as default build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif(NOT CMAKE_BUILD_TYPE)
# Does not work
# find_package(OpenCV REQUIRED PATHS "${OPENCV_DIR}/build_wasm" NO_DEFAULT_PATH)
# Needed for opencv2/opencv.hpp
include_directories("${OPENCV_DIR}")
# Needed by opencv.hpp for opencv2/opencv_modules.hpp
include_directories("${OPENCV_WASM_DIR}")
# Needed by opencv_modules.hpp for every module
file(GLOB opencv_include_modules "${OPENCV_WASM_DIR}/modules/*/include")
include_directories(${opencv_include_modules})
# Our yolo_cpp_webassembly_video_object_detector world executable
add_executable(yolo_cpp_webassembly_video_object_detector cpp/yolo.cpp)
# Link to opencv.js precompiled libraries
file(GLOB opencv_libs "${OPENCV_WASM_DIR}/lib/*.a")
target_link_libraries(yolo_cpp_webassembly_video_object_detector ${opencv_libs})
# There is an issue regarding the order in which libraries
# are passed to the compiler - pass libopencv_core.a last
# https://answers.opencv.org/question/186124/undefined-reference-to-cvsoftdoubleoperator/
# file(GLOB opencv_lib_core "${OPENCV_DIR}/build_wasm/lib/libopencv_core.a")
file(GLOB opencv_lib_core "${OPENCV_WASM_DIR}/lib/libopencv_core.a")
target_link_libraries(yolo_cpp_webassembly_video_object_detector ${opencv_lib_core})
# Specify linker arguments
set_target_properties(yolo_cpp_webassembly_video_object_detector PROPERTIES LINK_FLAGS "-s USE_ZLIB=1 -s NO_DISABLE_EXCEPTION_CATCHING -s DYNAMIC_EXECUTION=1 -s ALLOW_MEMORY_GROWTH -s ASSERTIONS -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s EXPORTED_FUNCTIONS=\"['_detectObjects', '_malloc']\"")
有什么问题吗?
鉴于它尚未与必要的 opencv 库链接,我猜
file(GLOB opencv_libs "${OPENCV_WASM_DIR}/lib/*.a")
不知何故会出现不足。例如,_ZN2cv3MatC1EiiiPvm
表示 cv::Mat::Mat(int, int, int, void*, unsigned long)
。如果您使用 cmake 的 find_package
,应该找到正确的库并且库的顺序应该变得正确。
我会尝试这个而不是通配符:
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(yolo_cpp_webassembly_video_object_detector cpp/yolo.cpp)
target_link_libraries( yolo_cpp_webassembly_video_object_detector ${OpenCV_LIBS} )