【问题标题】:Opencv Undefined symbols for architecture cv:imwrite Mac osX high SierraOpencv 架构的未定义符号 cv:imwrite Mac osX high Sierra
【发布时间】:2018-09-23 00:19:48
【问题描述】:

我通过自制软件在我的 Mac OS X high Sierra(10.13.3) 中下载了 GCC 和 opencv(3.4.1_2)。

$ g++ --version
g++-7 (Homebrew GCC 7.3.0_1) 7.3.0

在我的“opencvtest.cpp”中,cv::imwrite 被视为未定义的架构符号,但其他函数,如 cv::imread、cv::namedWindow完美运行。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include <iostream>

using namespace cv;
int main( int argc, char** argv ) {
   Mat img = imread( argv[1], -1 );
   if( img.empty() ) return -1;
   namedWindow( "Example1", cv::WINDOW_AUTOSIZE );
   imshow( "Example1", img );
   imwrite("Example1.jpg", img);         //<-- error
   waitKey( 0 );
   destroyWindow( "Example1" );
}

Makefile 写成:

CC = g++
CFLAGS = -g -Wall -std=c++11
OPENCV = `pkg-config --cflags --libs opencv`

test: opencvtest.o
   $(CC) -o $@ opencvtest.o $(CFLAGS) $(OPENCV)

opencvtest.o: opencvtest.cpp
   $(CC) -c opencvtest.cpp $(CFLAGS) $(OPENCV)

在终端中使用 g++ 编译产生:

In file included from /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/core.hpp:54:0,
                 from /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/highgui.hpp:46,
                 from /usr/local/Cellar/opencv/3.4.1_2/include/opencv2/highgui/highgui.hpp:48,
                 from opencvtest.cpp:2:
/usr/local/Cellar/opencv/3.4.1_2/include/opencv2/core/base.hpp:381:35: warning:unknown option after '#pragma GCC diagnostic' kind [-Wpragmas]
 #   pragma GCC diagnostic ignored "-Winvalid-noreturn"
                                   ^~~~~~~~~~~~~~~~~~~~
g++ -o test opencvtest.o -g -Wall -std=c++11 `pkg-config --cflags --libs opencv`
Undefined symbols for architecture x86_64:
  "cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)", referenced from:
      _main in opencvtest.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [test] Error 1

如果我评论了有问题的imwrite("Example1.jpg", img);,程序可以完美运行并且可以显示图像。我已经多次重新安装opencv3,这个问题似乎无法解决。有聪明人能告诉我下一步该怎么做吗?

编辑:这是我的 opencv 个人资料。它说默认的C++编译器是clang++而不是g++-7。

$opencv_version -v

General configuration for OpenCV 3.4.1 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /tmp/opencv-20180307-60086-ryy1b3/opencv-3.4.1/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2018-03-07T08:15:55Z
    Host:                        Darwin 17.3.0 x86_64
    CMake:                       3.10.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/local/Homebrew/Library/Homebrew/shims/super/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
      disabled:                  SSE4_1 SSE4_2 AVX AVX2
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (3 files):          + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (9 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (1 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++11:                       YES
    C++ Compiler:                /usr/local/Homebrew/Library/Homebrew/shims/super/clang++  (ver 9.0.0.9000039)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/local/Homebrew/Library/Homebrew/shims/super/clang
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      
    Linker flags (Debug):        
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          /usr/local/lib/libtbb.dylib
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    js world
    Disabled by dependency:      -
    Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv dnn_modern freetype hdf java matlab ovis sfm ts viz
    Applications:                apps
    Documentation:               NO
    Non-free algorithms:         YES

  GUI: 
    Cocoa:                       YES

  Media I/O: 
    ZLib:                        /usr/lib/libz.dylib (ver 1.2.11)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         /usr/local/lib/libpng.dylib (ver 1.6.34)
    TIFF:                        /usr/local/lib/libtiff.dylib (ver 42 / 4.0.9)
    OpenEXR:                     /usr/local/lib/libImath.dylib /usr/local/lib/libIlmImf.dylib /usr/local/lib/libIex.dylib /usr/local/lib/libHalf.dylib /usr/local/lib/libIlmThread.dylib (ver 2.2.0)

  Video I/O:
    FFMPEG:                      YES
      avcodec:                   YES (ver 57.107.100)
      avformat:                  YES (ver 57.83.100)
      avutil:                    YES (ver 55.78.100)
      swscale:                   YES (ver 4.8.100)
      avresample:                YES (ver 3.7.0)
    AVFoundation:                YES

  Parallel framework:            TBB (ver 2018.0 interface 10002)

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2017.0.3 [2017.0.3]
           at:                   /tmp/opencv-20180307-60086-ryy1b3/opencv-3.4.1/build/3rdparty/ippicv/ippicv_mac
    Intel IPP IW:                sources (2017.0.3)
              at:                /tmp/opencv-20180307-60086-ryy1b3/opencv-3.4.1/build/3rdparty/ippicv/ippiw_mac
    Lapack:                      YES (/System/Library/Frameworks/Accelerate.framework /System/Library/Frameworks/Accelerate.framework)
    Eigen:                       YES (ver 3.3.4)
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (no extra features)
    Include path:                NO
    Link libraries:              -framework OpenCL

  Python 2:
    Interpreter:                 /usr/local/opt/python@2/libexec/bin/python (ver 2.7.14)
    Libraries:                   /usr/local/Cellar/python@2/2.7.14_1/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib (ver 2.7.14)
    numpy:                       /usr/local/lib/python2.7/site-packages/numpy/core/include (ver 1.14.1)
    packages path:               lib/python2.7/site-packages

  Python 3:
    Interpreter:                 /usr/local/opt/python/bin/python3 (ver 3.6.4)
    Libraries:                   /usr/local/opt/python/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin/libpython3.6.dylib (ver 3.6.4)
    numpy:                       /usr/local/lib/python3.6/site-packages/numpy/core/include (ver 1.14.1)
    packages path:               lib/python3.6/site-packages

  Python (for build):            /usr/local/opt/python@2/libexec/bin/python

  Java:                          
    ant:                         NO
    JNI:                         /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/include /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/include/darwin /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        NO

  Install to:                    /usr/local/Cellar/opencv/3.4.1_2
-----------------------------------------------------------------

因此只使用 clang++ 而不是 g++ 有或没有CFLAG = -g 并且编译运行良好。我想知道是否可以将 opencv 默认编译器从 clang++ 更改为 g++?

【问题讨论】:

  • 尝试运行 opencv_version -v 并将输出粘贴到您的问题中 - 单击 edit 而不是 Add Comment。如果找不到该程序,请使用find /usr/local -name opencv_version 获取完整路径以供使用。
  • 在您的Makefile 中,尝试从您的CFLAGS 中删除-g 并尝试设置CC=clang++。如果这不起作用,请尝试所有 4 种排列。
  • @MarkSetchell :使用 clang++ 而不是 g++ 确实有效!谢谢!
  • @MarkSetchell:但我想知道为什么 gcc 无法在 cv::imwrite 等函数中找到合适的架构符号?当我仍然使用 g++ 而不是 clang++ 时,这个问题可以解决吗?
  • 那么它是否适用于clang++?有/没有-g 怎么样。请点击edit 并使用任何新信息进行更新。

标签: c++ opencv opencv3.0 macos-high-sierra


【解决方案1】:

正如我在 cmets 中建议的那样,如果您使用 clang++ 编译器代替 GNU g++,这可能会起作用,这就是我建议您运行的原因:

opencv_version -v

如果您查看输出,特别是以 C++ Compiler: 开头的行,您可以看到 OpenCV 是使用 clang++ 的派生类编译的。

我通常“随波逐流”并使用与构建 OpenCV 相同的编译器,但我想还有其他选择。

我想你可以:

  • 强制 OpenCV 使用 g++ 编译,或者
  • 强制g++ 编译您的应用程序,以便它可以与使用clang++ 编译的OpenCV 链接。

我已经测试了上面的第一个建议,即使用 GNU 编译器编译 OpenCV,它的工作原理如下:

cmake -D CMAKE_CXX_COMPILER=/usr/local/bin/g++-7 -D CMAKE_C_COMPILER=/usr/local/bin/gcc-7 ...
make -j8
make install 

我猜后者是通过以下方式完成的:

g++ YourApp.cpp -std=C++11 -stdlib=libstdc++ $(pkg-config ...) -o YourApp

但如果有人知道更好,请发表评论,我会更新建议!

【讨论】:

    【解决方案2】:

    几乎是因为没有链接正确的库。自己找很难。 我解决了这个问题

    g++ HelloWorld.cpp -o hello `pkg-config --cflags --libs opencv`
    

    我的源文件是HelloWorld.cpp,你可以单独运行pkg-config --cflags --libs opencv。它只是打印有关包含头和链接库的参数:

    $ pkg-config --cflags --libs opencv
    -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
    

    我的源文件是

    #include <iostream>
    #include "opencv2/opencv.hpp"
    
    using namespace cv;
    using namespace std;
    
    int main( int argc, char** argv )
    {
        if( argc != 2)
        {
         cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
         return -1;
        }
    
        Mat image;
        image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file
    
        if(! image.data )                              // Check for invalid input
        {
            cout <<  "Could not open or find the image" << std::endl ;
            return -1;
        }
    
        namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
        imshow( "Display window", image );                   // Show our image inside it.
    
        waitKey(0);                                          // Wait for a keystroke in the window
        return 0;
    }
    

    我通过将pkg-config --cflags --libs opencv 产品的参数一一放入来测试真正的问题。最后我发现我的问题没有解决

    -I/usr/local/include/opencv -I/usr/local/include
    

    。那真是要了我的命。忘记设置头目录了……

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 2021-09-08
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      相关资源
      最近更新 更多