Skip to content

Call imwrite() on OS X error: Undefined symbols #6165

Closed

Description

@everjesse

Undefined symbols for architecture x86_64:
"cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator > const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Activity

  1. changed the title [-]Call imwrite() on OSX[/-] [+]Call imwrite() on OS X error: Undefined symbols[/+] on Feb 25, 2016
  2. alalek commented on Feb 25, 2016

    @alalek
    Member

    Describe your steps with more details. Run make VERBOSE=1 or something else to show compiler/linker command line.

  3. everjesse commented on Feb 25, 2016

    @everjesse
    Author

    Thank you for response!

    I'm running a tutorial in the official docs in Xcode. The code shows like below.

    #include <opencv2/opencv.hpp>
    using namespace cv;
    using namespace std;
    void createAlphaMat(Mat &mat)
    {
        CV_Assert(mat.channels() == 4);
        for (int i = 0; i < mat.rows; ++i) {
            for (int j = 0; j < mat.cols; ++j) {
                Vec4b& bgra = mat.at<Vec4b>(i, j);
                bgra[0] = UCHAR_MAX; // Blue
                bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
                bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
                bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
            }
        }
    }
    int main(int argv, char **argc)
    {
        // Create mat with alpha channel
        Mat mat(480, 640, CV_8UC4);
        createAlphaMat(mat);
        vector<int> compression_params;
        compression_params.push_back(IMWRITE_PNG_COMPRESSION);
        compression_params.push_back(9);
        try {
            imwrite("alpha.png", mat, compression_params);
        }
        catch (cv::Exception& ex) {
            fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
            return 1;
        }
        fprintf(stdout, "Saved PNG file with alpha data.\n");
        return 0;
    }
    

    And IDE gives me errors:

    Ld /Users/jesse/Library/Developer/Xcode/DerivedData/opencv_getstarted-ebpoccrushabqzcvprllyfegymoq/Build/Products/Debug/opencv_getstarted normal x86_64
    cd /Users/jesse/Projects/Opencv/opencv_getstarted
    export MACOSX_DEPLOYMENT_TARGET=10.11
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/Users/jesse/Library/Developer/Xcode/DerivedData/opencv_getstarted-ebpoccrushabqzcvprllyfegymoq/Build/Products/Debug -L/usr/local/lib -L/Users/jesse/Projects/Opencv/opencv_getstarted -F/Users/jesse/Library/Developer/Xcode/DerivedData/opencv_getstarted-ebpoccrushabqzcvprllyfegymoq/Build/Products/Debug -filelist /Users/jesse/Library/Developer/Xcode/DerivedData/opencv_getstarted-ebpoccrushabqzcvprllyfegymoq/Build/Intermediates/opencv_getstarted.build/Debug/opencv_getstarted.build/Objects-normal/x86_64/opencv_getstarted.LinkFileList -mmacosx-version-min=10.11 -stdlib=libstdc++ -lopencv_objdetect.3.1.0 -lopencv_shape.3.1.0 -lopencv_features2d.3.1.0 -lopencv_flann.3.1.0 -lopencv_calib3d.3.1.0 -lopencv_highgui.3.1.0 -lopencv_highgui.3.1.0 -lopencv_video.3.1.0 -lpng16.16 -lopencv_photo.3.1.0 -lopencv_superres.3.1.0 -lopencv_features2d.3.1.0 -lopencv_calib3d.3.1.0 -lopencv_videoio.3.1.0 -lopencv_objdetect.3.1.0 -lopencv_calib3d.3.1.0 -lopencv_videoio.3.1.0 -lopencv_stitching.3.1.0 -lopencv_core.3.1.0 -lopencv_videostab.3.1.0 -llept.4 -ltiff.5 -lopencv_shape.3.1.0 -lopencv_core.3.1.0 -lopencv_videoio.3.1.0 -lopencv_videostab.3.1.0 -lopencv_imgproc.3.1.0 -lopencv_objdetect.3.1.0 -llept.4 -ltesseract.3 -ltiffxx.5 -lgmp.10 -lopencv_shape.3.1.0 -lopencv_imgcodecs.3.1.0 -ljpeg.8 -lopencv_video.3.1.0 -lopencv_highgui.3.1.0 -lmpfr.4 -lgmp.10 -lopencv_superres.3.1.0 -ljpeg.8 -lopencv_imgcodecs.3.1.0 -lmpc.3 -lopencv_videostab.3.1.0 -lopencv_imgproc.3.1.0 -ltiffxx.5 -ltiff.5 -lopencv_features2d.3.1.0 -lpng16.16 -ltesseract.3 -lopencv_core.3.1.0 -lgmpxx.4 -lopencv_ml.3.1.0 -lopencv_imgcodecs.3.1.0 -lgmpxx.4 -lopencv_ml.3.1.0 -lmpc.3 -lopencv_ml.3.1.0 -lopencv_superres.3.1.0 -lopencv_flann.3.1.0 -lopencv_stitching.3.1.0 -lopencv_photo.3.1.0 -lopencv_photo.3.1.0 -lmpfr.4 -lpng16.16 -lopencv_flann.3.1.0 -lopencv_video.3.1.0 -lopencv_imgproc.3.1.0 -lopencv_stitching.3.1.0 -Xlinker -dependency_info -Xlinker /Users/jesse/Library/Developer/Xcode/DerivedData/opencv_getstarted-ebpoccrushabqzcvprllyfegymoq/Build/Intermediates/opencv_getstarted.build/Debug/opencv_getstarted.build/Objects-normal/x86_64/opencv_getstarted_dependency_info.dat -o /Users/jesse/Library/Developer/Xcode/DerivedData/opencv_getstarted-ebpoccrushabqzcvprllyfegymoq/Build/Products/Debug/opencv_getstarted

    Undefined symbols for architecture x86_64:
    "cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator > const&)", referenced from:
    _main in main.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    Actually, some other functions works well like imshow() etc..

  4. alalek commented on Feb 25, 2016

    @alalek
    Member

    Thanks for update!

    cv::imwrite should be defined in opencv_imgcodecs library. Command line shows that this library is added to libraries list (three times).

    Could you try std::cout << CV_VERSION_MAJOR << CV_VERSION_MINOR << std::endl; and add output?

    Could you try std::cout << cv::getBuildInformation().c_str() << std::endl; and attach output?

  5. everjesse commented on Feb 25, 2016

    @everjesse
    Author

    Below is the output of codes you mentioned before.

    > 31
    > 
    > General configuration for OpenCV 3.1.0 =====================================
    >   Version control:               unknown
    > 
    >   Platform:
    >     Host:                        Darwin 15.3.0 x86_64
    >     CMake:                       3.4.1
    >     CMake generator:             Unix Makefiles
    >     CMake build tool:            /usr/bin/make
    >     Configuration:               Release
    > 
    >   C/C++:
    >     Built as dynamic libs?:      YES
    >     C++ Compiler:                /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  (ver 7.0.0.7000072)
    >     C++ flags (Release):         -fsigned-char -W -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    >     C++ flags (Debug):           -fsigned-char -W -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    >     C Compiler:                  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
    >     C flags (Release):           -fsigned-char -W -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    >     C flags (Debug):             -fsigned-char -W -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    >     Linker flags (Release):
    >     Linker flags (Debug):
    >     Precompiled headers:         NO
    >     Extra dependencies:          -framework OpenCL -framework Cocoa -framework QTKit -framework QuartzCore -framework AppKit
    >     3rdparty dependencies:       libjpeg libwebp libpng libtiff libjasper IlmImf zlib
    > 
    >   OpenCV modules:
    >     To be built:                 core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab python2
    >     Disabled:                    world
    >     Disabled by dependency:      -
    >     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python3 viz
    > 
    >   GUI: 
    >     QT:                          NO
    >     Cocoa:                       YES
    >     OpenGL support:              NO
    >     VTK support:                 NO
    > 
    >   Media I/O: 
    >     ZLib:                        build (ver 1.2.8)
    >     JPEG:                        build (ver 90)
    >     WEBP:                        build (ver 0.3.1)
    >     PNG:                         build (ver 1.6.19)
    >     TIFF:                        build (ver 42 - 4.0.2)
    >     JPEG 2000:                   build (ver 1.900.1)
    >     OpenEXR:                     build (ver 1.7.1)
    >     GDAL:                        NO
    > 
    >   Video I/O:
    >     DC1394 1.x:                  NO
    >     DC1394 2.x:                  NO
    >     FFMPEG:                      NO
    >       codec:                     NO
    >       format:                    NO
    >       util:                      NO
    >       swscale:                   NO
    >       resample:                  NO
    >       gentoo-style:              NO
    >     GStreamer:                   NO
    >     OpenNI:                      NO
    >     OpenNI PrimeSensor Modules:  NO
    >     OpenNI2:                     NO
    >     PvAPI:                       NO
    >     GigEVisionSDK:               NO
    >     QuickTime:                   NO
    >     QTKit:                       YES
    >     V4L/V4L2:                    NO/NO
    >     XIMEA:                       NO
    >     gPhoto2:                     NO
    > 
    >   Parallel framework:            GCD
    > 
    >   Other third-party libraries:
    >     Use IPP:                     9.0.1 [9.0.1]
    >          at:                     /Users/jesse/Downloads/opencv-3.1.0/3rdparty/ippicv/unpack/ippicv_osx
    >     Use IPP Async:               NO
    >     Use VA:                      NO
    >     Use Intel VA-API/OpenCL:     NO
    >     Use Eigen:                   NO
    >     Use Cuda:                    NO
    >     Use OpenCL:                  YES
    >     Use custom HAL:              NO
    > 
    >   OpenCL:
    >     Version:                     static
    >     libraries:                   -framework OpenCL
    >     Use AMDFFT:                  NO
    >     Use AMDBLAS:                 NO
    > 
    >   Python 2:
    >     Interpreter:                 /usr/bin/python2.7 (ver 2.7.10)
    >     Libraries:                   /usr/lib/libpython2.7.dylib (ver 2.7.10)
    >     numpy:                       /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include (ver 1.8.0rc1)
    >     packages path:               lib/python2.7/site-packages
    > 
    >   Python 3:
    >     Interpreter:                 NO
    > 
    >   Python (for build):            /usr/bin/python2.7
    > 
    >   Java:
    >     ant:                         NO
    >     JNI:                         /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/JavaVM.framework/Headers /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/JavaVM.framework/Headers /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/JavaVM.framework/Headers
    >     Java wrappers:               NO
    >     Java tests:                  NO
    > 
    >   Matlab:                        Matlab not found or implicitly disabled
    > 
    >   Documentation:
    >     Doxygen:                     NO
    >     PlantUML:                    NO
    > 
    >   Tests and samples:
    >     Tests:                       YES
    >     Performance tests:           YES
    >     C/C++ Examples:              NO
    > 
    >   Install path:                  /usr/local
    > 
    >   cvconfig.h is in:              /Users/jesse/Downloads/opencv-3.1.0/release
    > -----------------------------------------------------------------
    > 
    

    Thanks for your comment!

  6. alalek commented on Feb 25, 2016

    @alalek
    Member

    31 is expected, so include files are good.
    -L/usr/local/lib compiler option is valid for provided build configuration.

    What is about this command nm -g /usr/local/lib/libopencv_imgcodecs.dylib | grep imwrite?
    And this: nm -g <path_to_main.o>/main.o | grep imwrite (you need to find main.o file, it is somewhere in opencv_getstarted.LinkFileList)

    And this information:
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -v

  7. everjesse commented on Feb 25, 2016

    @everjesse
    Author

    Below is the command result.

    nm -g /usr/local/lib/libopencv_imgcodecs.dylib | grep imwrite

    00000000000036b0 T __ZN2cv7imwriteERKNS_6StringERKNS_11_InputArrayERKNSt3__16vectorIiNS6_9allocatorIiEEEE

    nm -g main.o | grep imwrite

    U __ZN2cv7imwriteERKNS_6StringERKNS_11_InputArrayERKSt6vectorIiSaIiEE

    Thx!

  8. alalek commented on Feb 25, 2016

    @alalek
    Member

    They are different.
    This usually means, that there are different compilers (or compiler options, like -std=c++11) used to compile OpenCV library and to compile application.

    Try to check compiler version at first:

    • app:
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -v
    
    • library:
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -v
    

    And rebuild/reinstall OpenCV with current version of compiler.

  9. everjesse commented on Apr 25, 2016

    @everjesse
    Author

    Sorry for reply so late!

    I've solved this question by rebuild/reinstall OpenCV library and choose the same C++ complier in Xcode with libc++.

    Thank you for your patience and great help!!

  10. iKeepWalking commented on May 31, 2016

    @iKeepWalking

    I meet the exactly the same problem when I'm developing a node-webkit addon. But I don't know how to solve it ;-(. I compiled opencv with default llvm 7.3.0 and the addon with nw-gyp. Any idea? thank you so much

  11. dmsfabiano commented on Mar 31, 2018

    @dmsfabiano

    I tried the exact suggested approach and I still get a similar error:

    Undefined symbols for architecture x86_64:
      "cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)", referenced from:
          parser::parser(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) in ccO9dcvr.o
    ld: symbol(s) not found for architecture x86_64
    collect2: error: ld returned 1 exit status
    

    I did the suggested check:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -v
    

    yields to:

    Apple LLVM version 9.0.0 (clang-900.0.39.2)
    Target: x86_64-apple-darwin17.4.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    

    and

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -v
    

    outputs:

    Apple LLVM version 9.0.0 (clang-900.0.39.2)
    Target: x86_64-apple-darwin17.4.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    

    After that I did brew reinstall opencv

    But the error persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidquestion (invalid tracker)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions