【问题标题】:Compiling OpenCV project using CMake results in build errors while regular compilation works fine使用 CMake 编译 OpenCV 项目会导致构建错误,而常规编译工作正常
【发布时间】:2021-08-08 21:31:42
【问题描述】:

我使用自制软件下载了 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

【问题讨论】:

  • 您是否阅读了CMake 开发人员方便地放在那里的输出,您是否听从了它的建议?你添加了cv 库吗?
  • 你用 OpenCV 链接 library mvu_shared_library_cpp。但是错误信息是关于executable mvu_cpp_exec,没有链接。
  • 我已经阅读了 cmets,但没有发现可以遵循的建议,我可能无法从所写的内容中理解该做什么。我已经添加了 cv 库。
  • 关于链接库而不是可执行文件,当链接到可执行文件时,我得到不同的错误。如:ld:错误:/usr/local/Cellar/opencv/4.5.2_1/lib/libopencv_gapi.4.5.2.dylib: 未知文件类型

标签: c++ opencv c++11 cmake ld


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    相关资源
    最近更新 更多