微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 CMake 编译 OpenCV 项目会导致构建错误,而常规编译工作正常

如何解决使用 CMake 编译 OpenCV 项目会导致构建错误,而常规编译工作正常

我使用自制软件下载了 CMake 和 OpenCV,我正在尝试运行一个简单的 openCV 程序,该程序可以打开我的相机并开始流式传输视频。我可以使用命令行定期编译我的程序: clang++ -std=c++11 lib_cpp.cpp -o execute 'pkg-config --cflags --libs opencv4' 但是,当我尝试使用 CMake 构建和编译我的项目时,我遇到了麻烦和错误

脚本:

#!/bin/bash

SDK=/Users/David/Library/Android/sdk
CMAKE=$SDK/cmake/3.18.1/bin/cmake
NDK=$SDK/ndk/23.0.7272597
TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake
TOOLCHAIN_BINARIES=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin
LD=$TOOLCHAIN_BINARIES/ld
ABI=arm64-v8a
MIN_SDK=16
STL=c++_shared
STD=-std=c++11 -o 
CXX=$TOOLCHAIN_BINARIES/aarch64-linux-android30-clang++
CMAKE_ARGS=""

export CXX=$CXX

$CMAKE -DCMAKE_CXX_FLAGS=$STD -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -DANDROID_ABI=$ABI -DANDROID_NATIVE_API_LEVEL=$MIN_SDK -DANDROID_STL=$STL $CMAKE_ARGS

CMakeLists.txt:

cmake_minimum_required(VERSION 3.7 FATAL_ERROR)

project(shared_library_cpp VERSION 1.0.0 LANGUAGES CXX)
set(OpenCV_DIR "/usr/local/Cellar/opencv/4.5.2_1/lib/cmake/opencv4")
find_package(OpenCV required)
include_directories(${OpenCV_INCLUDE_Dirs})

add_library(shared_library_cpp SHARED lib_cpp.cpp cpp.def)
add_executable(cpp_exec lib_cpp.cpp)
target_link_libraries( shared_library_cpp ${OpenCV_LIBS} )


set_target_properties(shared_library_cpp PROPERTIES
    PUBLIC_HEADER lib_cpp.h
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
    OUTPUT_NAME "cpp"
    XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "MacOS_ID"
)

由于某种原因,我遇到了链接问题,脚本运行流畅,但在运行 make 时,我在构建过程中遇到错误

脚本输出

./create_makefile.sh: line 12: -o: command not found
CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory,but note that this warning will
  become a Fatal error in future CMake releases.


CMake Warning at /Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android-legacy.toolchain.cmake:416 (message):
  An old version of CMake is being used that cannot automatically detect
  compiler attributes.  Compiler identification is being bypassed.  Some
  values may be wrong or missing.  Update to CMake 3.19 or newer to use
  CMake's built-in compiler identification.
Call Stack (most recent call first):
  /Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android.toolchain.cmake:55 (include)
  /Users/David/Library/Android/sdk/cmake/3.18.1/share/cmake-3.18/Modules/CMakeDetermineSystem.cmake:93 (include)
  CMakeLists.txt:3 (project)


-- Detecting CXX compiler ABI info
CMake Warning at /Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android-legacy.toolchain.cmake:416 (message):
  An old version of CMake is being used that cannot automatically detect
  compiler attributes.  Compiler identification is being bypassed.  Some
  values may be wrong or missing.  Update to CMake 3.19 or newer to use
  CMake's built-in compiler identification.
Call Stack (most recent call first):
  /Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android.toolchain.cmake:55 (include)
  /Users/David/Desktop/CPP/CMakeFiles/3.18.1-g262b901/CMakeSystem.cmake:6 (include)
  /Users/David/Desktop/CPP/CMakeFiles/CMakeTmp/CMakeLists.txt:2 (project)


-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/David/Library/Android/sdk/ndk/23.0.7272597/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local/Cellar/opencv/4.5.2_1 (found version "4.5.2") 
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/David/Desktop/CPP

错误输出

Scanning dependencies of target cpp_exec
[ 25%] Building CXX object CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o
[ 50%] Linking CXX executable cpp_exec
ld: error: undefined symbol: cv::VideoCapture::VideoCapture(int,int)
>>> referenced by lib_cpp.cpp:22
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::VideoCapture::isOpened() const
>>> referenced by lib_cpp.cpp:23
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::Mat::Mat()
>>> referenced by lib_cpp.cpp:27
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::VideoCapture::operator>>(cv::Mat&)
>>> referenced by lib_cpp.cpp:28
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::imshow(std::__ndk1::basic_string<char,std::__ndk1::char_traits<char>,std::__ndk1::allocator<char> > const&,cv::_InputArray const&)
>>> referenced by lib_cpp.cpp:29
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::waitKey(int)
>>> referenced by lib_cpp.cpp:31
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::Mat::~Mat()
>>> referenced by lib_cpp.cpp:33
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
>>> referenced by lib_cpp.cpp:33
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)

ld: error: undefined symbol: cv::VideoCapture::~VideoCapture()
>>> referenced by lib_cpp.cpp:36
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
>>> referenced by lib_cpp.cpp:36
>>>               CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
clang++: error: linker command Failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpp_exec] Error 1
make[1]: *** [CMakeFiles/cpp_exec.dir/all] Error 2
make: *** [all] Error 2

解决方法

经过一番挖掘,我找到了解决此问题的方法。我试图为 arm64 编译我的程序,但 dylib 是为 x86-64 编译的。从 OpenCV 下载源代码,并提供 arm64 库的路径解决了这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。