caffe fails with error undefined reference to `cv::imread(cv::String const&, int)' and undefined reference to `cv::imdecode(cv::_InputArray const&, int)

Created on 22 Feb 2016  路  26Comments  路  Source: BVLC/caffe

I am trying to compile and use the latest version of caffe , but I'm getting these errors

AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
CXX tools/caffe.cpp
CXX/LD -o .build_release/tools/caffe.bin
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/caffe.bin] Error 1

I downloaded caffe almost a month a go and that compiled just fine! but this one that I downloaded today! gives these errors:

I tried the hongzhenwang solution here but that didnt work for me!
Whats wrong and how can I get rid of these errors?

by the way I installed opencv using this command on ubuntu 14.04 :

sudo apt-get install libopencv-dev

Thanks in advance

Most helpful comment

I think you may be using OpenCV 3. You need to set the OPENCV_VERSION variable in Makefile.config.

All 26 comments

make clean the caffe, try sudo ldconfig before make. Will this help?

I think you may be using OpenCV 3. You need to set the OPENCV_VERSION variable in Makefile.config.

@wk910930 : no luck yet.
doing the sudo ldconfig gave me this error :

/sbin/ldconfig.real: /usr/lib/libcudnn.so.7.0 is not a symbolic link

@liquidmetal : How can I be sure about that?
I mean when I installed anaconda, It did install opencv3 for itself, however, before installing anadonda, I had installed opencv using this command :
sudo apt-get install libopencv-dev
which I believe installs opencv 2.4.8!

How can I make sure its openCV3 or not ?
by the way thank you guys for your time , I really appreciate it

I changed the version to OpenCV3, make all, make test ran successfully, but
make runtest fails with this error message :

.build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libopencv_core.so.3.1: cannot open shared object file: No such file or directory
make: *** [runtest] Error 127

Ok, I added the opencv libs from anacondas Lib directory to the bashrc like this:

export LD_LIBRARY_PATH=/home/hossein/anaconda2/lib:$LD_LIBRARY_PATH

So far the make runtest is going just well, I hope what I am doing is Ok and doesnt create any more issues!
Update:
Thanks to dear God, I could successfully train mnist .:)
Thank you everyone

@liquidmetal Thanks for the simple fix!

@Coderx7
Please, what is anaconda2 path?

@hana9090 : this is where I insalled anaconda2
/home/hossein/anaconda2/

@liquidmetal Thanks. I found I installed opencv 3.0. and I change the Makefile.config. It works.

I am using a older version of caffe in another repo(actually, RstarCNN) and there isn't the parameter OPENCV_VERSION in that version of caffe. I just uncommented # USE_PKG_CONFIG := 1 and the make command passed. Hope this info can be helpful for someone.

I had opencv 2.4.5 installed using yum and opencv 3.0 by cmake.
On uninstalling opencv 2.4.5 and adding /lib64/ to LIBRARY_DIRS, in the Makefile.config is was able to overcome the issue.

Check the Caffe configuration summary for OpenCV version being accepted.

@lifematrix @liquidmetal Thanks. I found I installed opencv 3.0. and I change the Makefile.config. It works. 2

In my case I was using OpenCV 2.4 then installed OpenCV 3.0 using conda install -c menpo opencv3=3.2.0 and it finally compiled.

I know this is an old thread, but I came across @Coderx7's problem today (.build_release/tools/caffe: error while loading shared libraries: libopencv_core.so.3.x: cannot open shared object file: No such file or directory) and found a solution that someone else might find useful. Basically, adding your conda lib-folder to LD_LIBRARY_PATH works, but (at least for me) it breaks/affects other software (_e.g._ programs that rely on libssl, such as ssh).
Instead of globally modifying LD_LIBRARY_PATH, we can add the following line to the Makefile.config (for example after the lines that define ANACONDA_HOME and PYTHON_INCLUDE [lines 76-79 I believe]):

LDFLAGS += -Wl,-rpath,$(ANACONDA_HOME)/lib

This will basically tell the Caffe library at runtime where to look for libraries (see https://stackoverflow.com/questions/25918462/why-ld-library-path-is-bad-and-the-correct-way-to-load-dynamic-libraries for more info)

@vra Can you tell me why? I uncomment PKG_ CONFIG and it can pass, I am confused about it, what is the reason? Thank you in advance!

I have sucessfully install opencv3.4 and uncomment OPENCV_VERSION in config file, undefined reference to `cv::imread error. I modify the makefile as in https://github.com/BVLC/caffe/issues/1276, I add opencv_imgcodecs in make file

I just uncommended OPENCV_VERSION := 3 and solved the problem, wish it helps.

I just uncommended OPENCV_VERSION := 3 and solved the problem, wish it helps.

that's right. you should check your opencv version with the binary file under /usr/bin/opencv_version

The uncomment OPENCV solution works for all versions above 3. I am using OpenCV 3.4.2 and it worked.

Uncommended OPENCV_VERSION := 3 ,uncomment PKG_ CONFIG, or add opencv_imgcodecs in Makefile.
All those methods don't help for me...The opencv version is 3.4.0. someone can help me? Thank you

I built the caffe with cmake using mkdir build && cd build && cmake .. -DCMAKE_CXX_FLAGS="-std=c++11" -DCPU_ONLY=ON -DPYTHON_EXECUTABLE="/usr/local/bin/python3" .

Notice the output of cmake, especially you have several opencv version.

I have installed two

  • opencv 3.4.3 with brew
  • opencv 2.4.11 with conda in py2 environment

After conda uninstall python

-- Dependencies:
--   BLAS              :   Yes (vecLib)
--   Boost             :   Yes (ver. 1.68)
--   glog              :   Yes
--   gflags            :   Yes
--   protobuf          :   Yes (ver. 3.6.1)
--   lmdb              :   Yes (ver. 0.9.23)
--   LevelDB           :   Yes (ver. 1.20)
--   Snappy            :   Yes (ver. 1.1.7)
--   OpenCV            :   Yes (ver. 3.4.3)  # used to be ver.2.4.11
--   CUDA              :   No

Then caffe installed successfully on my macOS machine.

I've spent literally all day trying to build caffe using every bit of advice on here. No luck. I still get the usual 3 or 4 "undefined reference" errors.
I've tried it with OpenCV installed via apt-get libopencv-dev (3.2.0) and compiling version it myself using cmake with CUDA support (4.1.0).
I've turned on and off PKG_CONFIG, I've set OPENCV_VERSION := 3, added opencv_imgcodecs and every combination of the above.

I'll take any help I can get.

For reference:
I'm on a Ryzen 2700X with a GTX 1080Ti.
I have CUDA 10.1 installed
I have cuDNN installed (matching above)

In my case I was using OpenCV 2.4 then installed OpenCV 3.0 using conda install -c menpo opencv3=3.2.0 and it finally compiled.

This hepled me a lot.

I was facing the same problem and could not solve the problem using these and other suggested solutions.
My problem must have been linked to the opencv installation.
Following
https://yangcha.github.io/Caffe-Conda/
helped me to finally get caffe running on my PC.

I had also the same issue when compiling Caffe and none of the suggested solutions worked. Finally I have tried to use OpenCV vesion 3.4.2 and the compilation worked. The packaged OpenCV version is 3.2 so I have compiled the 3.4.2 from the source code.

Was this page helpful?
0 / 5 - 0 ratings