Compile error : undefined reference to‘__atomic_fe

2020-04-08 15:01发布

问题:

#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!

回答1:

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.



回答2:

You seem to be mixing Debug and Release versions of the OpenCV libraries. https://github.com/Itseez/opencv/issues/5581 You should only include and link against the normal Release library, or againt a Debug library you built yourself.

In your case, this may come from the double include -cflags -I /usr/local/include/opencv. I would try remove all the manual paths and just write pkg-config --cflags --libs opencv (to the packaged Release version).

Note that /usr/local/include is a standard include path and will always be searched. You could try to exclude standard paths with -nostdinc https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

Another option is to uninstall one of the two versions, or remove one version away from the standard paths.



回答3:

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).