"linker command fail" in MacOS 12.5

Hallo everyone,

-I have already successfully installed OpenCV 4.7.0 on my macbook air with the following test feedback:

(base) MacBook-Air-2:apple apple$ pkg-config --cflags --libs opencv
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

-However, I still received the following errors when I tried to compile my c++ code:

(base) MacBook-Air-2:Desktop apple$ g++ main.cpp
Undefined symbols for architecture arm64:
“cv::ml::RTrees::create()”, referenced from:
_main in main-87ca8e.o
“cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)”, referenced from:
cv::Mat::row(int) const in main-87ca8e.o
“cv::Mat::Mat(int, int, int)”, referenced from:
_main in main-87ca8e.o
“cv::Mat::Mat()”, referenced from:
_main in main-87ca8e.o
“cv::Mat::~Mat()”, referenced from:
_main in main-87ca8e.o
“cv::noArray()”, referenced from:
_main in main-87ca8e.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

-Is this because of my environment settings, or the linking problem during the g++ compilation?

Thanks in advance!

best regards,
Gaoyi

uhmm, pkg-config does not install anything at all !
(you probably need to build opencv libs from src, before you can use them !)
please check, if you can find the opencv libs in /usr/local/lib

incomplete cmdline, leading to linker errors, indeed.
it might be:


g++ main.cpp -I/usr/local/include/opencv -L/usr/local/lib -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_coreI/usr/local/include/opencv4 -lopencv_dnn -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

or something like

$ g++ `pkg-config opencv --cflags --libs` main.cpp -o your_program

note, that this is not supported by default (not recommended !)
it needs a special cmake flag (OPENCV_GENERATE_PKGCONFIG=ON) to generate that info during install

Hallo,
Thanks for your reply!
But I have just uninstalled everything and try to build it with source code from scratch, with the following cmake command:

cmake -DBUILD_PNG=ON -DBUILD_ZLIB=ON -DBUILD_TIFF=ON -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D WITH_GTK=ON -D CMAKE_INSTALL_PREFIX=/usr/local …

And I received the following error during make process:
[ 51%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/vkcom/vulkan/vk_functions.cpp.o

[ 51%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/vkcom/vulkan/vk_loader.cpp.o

[ 51%] Linking CXX shared library …/…/lib/libopencv_dnn.dylib

[ 51%] Built target opencv_dnn

make: *** [all] Error 2

Do you know how to solve this? Thanks!

Best regards,
Gaoyi

this is only the final result, the actual error leading to this must be a few lines up in the make output.
can you look again, please ?

Hallo,

sure, the error above is like following:
[ 30%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/opengl.cpp.o
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/ocl.cpp:7050:9: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf(buf, “convert_%s”, typestr);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(msg)))
^
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/ocl.cpp:7053:9: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf(buf, "convert
%s%s_rte", typestr, (ddepth < CV_32S ? “_sat” : “”));
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(msg)))
^
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/ocl.cpp:7055:9: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf(buf, "convert
%s_sat", typestr);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
[ 30%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/out.cpp.o

and here as well
[ 31%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/persistence_yml.cpp.o
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/persistence.cpp:168:5: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf(dt, “%d%c”, cn, symbol);
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
[ 31%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/rand.cpp.o
[ 31%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/softfloat.cpp.o
[ 31%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/split.dispatch.cpp.o
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/persistence_xml.cpp:211:25: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf( data, “#x%02x”, (uchar)c );
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/persistence_xml.cpp:312:13: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf( ptr, “”, comment );
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/persistence_xml.cpp:347:13: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf( ptr, “–>” );
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
3 warnings generated.
/Users/apple/Desktop/opencv-4.7.0/modules/core/src/persistence_yml.cpp:155:25: warning: ‘sprintf’ is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
sprintf( data, “x%02x”, c );
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/stdio.h:188:1: note: ‘sprintf’ has been explicitly marked deprecated here
__deprecated_msg(“This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.”)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro ‘__deprecated_msg’
#define __deprecated_msg(_msg) attribute((deprecated(_msg)))
^
[ 31%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/src/stat.dispatch.cpp.o

there are basically same 2 errors.

Thanks a lot!
Gaoyi

sorry to bother you again, but those are all warnings, not errors
(and should not terminate the make process)

can you look again ? maybe scan for the exact word error?

sry here is the error:

[ 42%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/dnn_utils.cpp.o

make[1]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2

make[1]: *** Waiting for unfinished jobs…

ok, still, what went wrong with the imgcodecs module ?

maybe also try with make -j1 all , so you dont get such a confusing, mixed modules output ?

this is what I have after make -j1 all :smiling_face_with_tear:
(base) eduroam-141-23-157-226:build apple$ make -j1 all
[ 0%] Built target opencv_dnn_plugins
[ 0%] Built target opencv_highgui_plugins
[ 0%] Built target gen-pkgconfig
[ 0%] Built target jsimd
[ 1%] Built target libopenjp2
[ 4%] Built target carotene_objs
[ 4%] Built target tegra_hal
[ 4%] Built target opencv_videoio_plugins
[ 5%] Built target ittnotify
[ 7%] Built target zlib
[ 13%] Built target opencv_core
[ 17%] Built target opencv_imgproc
[ 21%] Built target libjpeg-turbo
[ 24%] Built target libtiff
[ 31%] Built target libwebp
[ 32%] Built target libpng
make[2]: *** No rule to make target zlib', needed by lib/libopencv_imgcodecs.4.7.0.dylib’. Stop.
make[1]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2
make: *** [all] Error 2

1 Like

seems to be a ‘known issue’

you probably need to set BUILD_ZLIB flag to ‘OFF’ in cmake, & try again…
gl !