Compile error : undefined reference to‘__atomic_fetch_add_4’

Viewed 6717
#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
  Mat img=imread("cornea.jpg");
  imshow("src",img);
  waitKey(0);
  return 0;
}

And I compile it with:

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

or

g++ main.cpp -o main -I/usr/local/opencv-3.1.0/include/opencv -I/usr/local/opencv-3.1.0/include -L/usr/local/opencv-3.1.0/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lpng -lz -ltiff -ljasper -ljpeg -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lfreetype -lgthread-2.0 -lglib-2.0 -ldc1394 -lv4l1 -lv4l2 -lavcodec -lavformat -lavutil -lswscale -ldl -lm -lpthread -lrt

Which gives me:

/tmp/ccoZCMRO.o:in function‘cv::Mat::release()’:main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x22):undefined reference to‘__atomic_fetch_add_4’
collect2: error: ld returned 1 exit status

I am using opencv 3.1.0 in the terminal of ubuntu 14.04 in VM!

3 Answers

On i386, you need to add -latomic as GCC cannot use assembler instructions but has to fallback on the libatomic library implementation.

Starting with i586, atomic instructions are available and linking against libatomic is no longer required. That means, the alternative of -latomic is to use -march=i586.

You may need to link with atomic library. Try with

-latomic

in your GCC compilation command line.

NOTE: i had to do it compiling with CLang 8 on a ARMv7 device (Android).

Related