OpenCV 3.1 Upgrade Leads to Linker Errors on Linux - c++

I've recently upgraded from OpenCV 2.4.11 to OpenCV 3.1 by following this guide. This sudo make install seems to have worked successfully and when I run pkg-config --modversion opencv, I get the appropriate version (3.1.0).
But for some reason, I am now running into linker errors, and even simple programs are unable to compile, for example:
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char* argv[])
{
Mat im1 = imread(argv[1]);
return 0;
}
returns the errors:
main.cpp:-1: error: undefined reference to cv::imread(cv::String const&, int)
main.cpp:-1: error: undefined reference to `cv::String::allocate(unsigned long)'
main.cpp:-1: error: undefined reference to `cv::String::deallocate()'
:-1: error: collect2: error: ld returned 1 exit status
Could this be due to conflicts with the previous installation? Before I go through and start manually deleting files in /usr/local/ [lib | include | bin], I thought I'd ask here first. Any suggestions?

Your linker path is probably wrong. Make sure the opencv you want is the first one on your linker path, or it may link against the wrong one. Deleting opencv2 would probably solve the problem... consider using a package manager in future so you don't have messes like this to contend with all the time.

I went ahead and took the nuclear option (sudo rm followed by sudo make install), and everything compiles now. I'm not proud of this, nor do I necessarily advocate it, but if anyone else reaches this point of desperation, you may find this kill log helpful:
sudo rm -r /usr/share/opencv
sudo rm -r /usr/share/OpenCV/
sudo rm -r /usr/local/include/opencv
sudo rm -r /usr/local/bin/opencv*
sudo rm -r /usr/local/share/OpenCV/
sudo rm -r /usr/local/lib/libopencv_*
sudo rm /usr/local/lib/pkgconfig/opencv.pc
sudo rm /usr/local/lib/python2.7/dist-packages/cv2.so

Related

Why can't I compile the Boost.Serialization demo?

I am running Ubuntu 16.04. I'm trying to get Boost.Serialization set up, and I'm running into trouble compiling the first demo from the tutorial (titled A Very Simple Case). I have followed the Boost Getting Started page for installing Boost and building Serialization, doing the following:
download the code from SourceForge (I have tried different versions including 1.67.0 and 1.55.0)
cd boost_1_55_0
./bootstrap.sh --prefix=/usr/local --with-libraries=serialization
sudo ./b2 install
check that /usr/local/include and /usr/local/lib include the relevant Boost files
and then I compile demo.cpp using
g++ -o demo demo.cpp -I /usr/local/include/boost -l boost_serialization
and get the following error:
/tmp/cc9a7HRg.o: In function boost::archive::text_iarchive::text_iarchive(std::istream&, unsigned int)':
demo.cpp:(.text._ZN5boost7archive13text_iarchiveC2ERSij[_ZN5boost7archive13text_iarchiveC5ERSij]+0x36): undefined reference toboost::archive::detail::shared_ptr_helper::shared_ptr_helper()'
/tmp/cc9a7HRg.o: In function boost::archive::text_iarchive::~text_iarchive()':
demo.cpp:(.text._ZN5boost7archive13text_iarchiveD2Ev[_ZN5boost7archive13text_iarchiveD5Ev]+0x24): undefined reference toboost::archive::detail::shared_ptr_helper::~shared_ptr_helper()'
collect2: error: ld returned 1 exit status
The line causing the error is
boost::archive::text_iarchive ia(ifs);
It is worth mentioning that the output line
boost::archive::text_oarchive oa(ofs);
was causing a similar error before the most recent re-install. Also, if I comment out all of the input (including the line causing the error), the program compiles fine, but causes a segmentation fault upon running. I have included cout << "done" << endl; at the end of main, but it is not reached.
Does anybody know what might be causing this error? Sorry if this seems like a silly question. I am very new to C++ development. Thank you in advance!
sudo apt-get install libboost-all-dev
worked for me.
This command is taken from the same source as lboyd's solution:
https://www.technical-recipes.com/2012/getting-started-with-the-boost-libraries-in-ubuntu-linux/
then I compile with
g++ serialization.cpp -lboost_serialization -lboost_system
For anybody else running into this issue, I managed to fix it by manually uninstalling everything I had installed from source (deleting the relevant Boost files from /usr/locale/include/ and /usr/local/lib/ as well as the root Boost directory) and installing using apt-get as described here.

Fortran Coarray cant compile

Im trying to compile an example of Coarray Fortran file.
https://github.com/ljdursi/coarray-examples
The command for compile is:
mpifort diffusion/diffusion-coarray.f90 -fcoarray=lib -o diffusion/diffusion-coarray -L ${PATH_TO_OPENCOARRAY_LIB} -lcaf_mpi
I've already installed the OpenCoarrays, using spack
But there is an error:
/usr/bin/ld: cannot find -lcaf_mpi
collect2: error: ld returned 1 exit status
Without -lcaf_mpi the error is:
/tmp/ccOdrmfc.o: In function `MAIN__':
diffusion-coarray.f90:(.text+0x32): undefined reference to `_gfortran_caf_num_images'
diffusion-coarray.f90:(.text+0x4c): undefined reference to `_gfortran_caf_this_image'
diffusion-coarray.f90:(.text+0x66): undefined reference to `_gfortran_caf_this_image'
diffusion-coarray.f90:(.text+0x77): undefined reference to `_gfortran_caf_num_images'
diffusion-coarray.f90:(.text+0x8a): undefined reference to `_gfortran_caf_num_images'
diffusion-coarray.f90:(.text+0xaa): undefined reference to `_gfortran_caf_this_image'
diffusion-coarray.f90:(.text+0xbd): undefined reference to `_gfortran_caf_this_image'
diffusion-coarray.f90:(.text+0xf4): undefined reference to `_gfortran_caf_num_images'
End etc.
Where is the problem?
Thanks for help. I've solved the problem. If anyone will be interested, here is my solution:
1. Install Linuxbrew following their instructions:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
test -d ~/.linuxbrew && PATH="$HOME/.linuxbrew/bin:$HOME/.linuxbrew/sbin:$PATH"
test -d /home/linuxbrew/.linuxbrew && PATH="/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:$PATH"
test -r ~/.bash_profile && echo "export PATH='$(brew --prefix)/bin:$(brew --prefix)/sbin'":'"$PATH"' >>~/.bash_profile
echo "export PATH='$(brew --prefix)/bin:$(brew --prefix)/sbin'":'"$PATH"' >>~/.profile
Install OpenCoarrays library brew install opencoarrays
Now we can compile files with caf and cafrun
Compile .exe-file caf fname.f90 -o test.exe
Run the program cafrun -np (numimages) test.exe
Hope it will be helpful for someone!
P.S. My OS is Ubuntu 16.04.4 LTS, gfortran: GNU Fortran 5.4.0 20160609 so the rest of necessary tools (for example, compiler caf and launcher cafrun are the part of OpenCoarrays package) will be installed by following the instruction.

Undefined reference to `OGRRegisterAll'

I am trying to write a gdal program to read shapefiles but I don't seem to be able to get past registering the drivers. Here is the code, I'm not sure why the functions in the header files are not available my source file even after including it.
#include "/usr/include/gdal/ogrsf_frmts.h"
int main()
{
GDALAllRegister();
}
If I run this: g++ demo.cpp, I get the following error:
demo.cpp: In function int main():
demo.cpp:6:21: error: GDALAllRegister was not declared in this scope
GDALAllRegister();
^
If I try the c version, I'm getting this:
#include "/usr/include/gdal/ogr_api.h"
int main()
{
OGRRegisterAll();
return 0;
}
gcc create.c
/tmp/cc3YB8sO.o: In function main:
create.c:(.text+0x11): undefined reference to OGRRegisterAll
collect2: error: ld returned 1 exit status
UPDATE
I am using Ubuntu 15.04 64bit
I installed all the packages using the apt:
sudo apt-get install gdal-bin libgdal-dev build-essential
I also have the ubuntugis ppa added and I have qgis 2.10 installed which is from the qgis repo and not from the ubuntugis ppa.
Thank you all for the help, the issue was the linker was not able to find the gdal library.
i did this for the C code
gcc create.c -lgdal

Installing and using the ParMetis library

I am installing ParMetis 4.0.3 but into a non-default directory, with:
make config prefix=/My-ParMETIS-Directory/
And afterward:
make install
After that, I get a set of directories in that folder. And to write a program that uses it, I'm supposed to add the 'include "parmetis.h"' in the headline, and also add the 'libraries/binaries'. How exactly do I do the latter?
I'm just trying to get my code to compile now, and doing so I run:
g++ test.cpp
This is test.cpp:
#include<iostream>
#include "include/parmetis.h"
using std::cout;
using std::endl;
int main()
{
cout << "Test!" << endl;
return 0;
}
I keep getting "was not declared in this scope" for everything/every-line in parmetis.h.
How can I get test.cpp use the other folders/files that were installed?
You need to provide the location of your ParMetis library to
the compiler, since you have choosen to install the library
not in the default library directories.
mpic++ test.cpp -I /My-ParMETIS-Directory/ -I /My-METIS-Directory/
Edit#2:
What I did to get your code compiling:
Download the ParMetis library from webpage to /tmp
cd /tmp/
wget http://glaros.dtc.umn.edu/gkhome/fetch/sw/parmetis/parmetis-4.0.3.tar.gz
Unpack archive
tar -xf parmetis-4.0.3.tar.gz
Install ParMETIS into directory /tmp/parmetis
mkdir parmetis
cd parmetis-4.0.3/
make config prefix=/tmp/parmetis
make install
Install METIS into directory /tmp/metis
cd /tmp/
mkdir metis
cd parmetis-4.0.3/metis
make config prefix=/tmp/metis
make install
Compile test.cpp which is located in /tmp/
cd /tmp/
mpic++ test.cpp -I /tmp/parmetis -I /tmp/metis
when you coding wtih C++ ,if you meet question like that :
ccJjiCo.o: In function `main':
bsplele.cpp:(.text+0x45e): undefined reference to `METIS_PartMeshNodal'
collect2: ld returned 1 exit status
you can check the compiler sentence and you should paste the -lmetis at the end of the compilation command.It is useful for me.
my right compilation command is that:g++ -L/home/hadoop/metis/lib -I/home/hadoop/metis/include/ LL_metis.cpp -lmetis

I installed libboost but can't link to it

I have installed libboost-dev through apt-get, and it's placed in /usr/lib.
/usr/lib$ ls | grep boost
libboost_filesystem.so.1.46.1
libboost_iostreams.so.1.46.1
libboost_serialization.so.1.46.1
libboost_system.so.1.46.1
libboost_thread.so.1.46.1
libboost_wserialization.so.1.46.1
But when I tried to compile a source that uses boost_thread I still got a error.
$ g++ tcp_echo.cpp -o tcp_echo -L/usr/lib -llibboost_thread
/usr/bin/ld: cannot find -lboost_thread
collect2: ld returned 1 exit status
$ g++ tcp_echo.cpp -o tcp_echo -L/usr/lib -lboost_thread
/usr/bin/ld: cannot find -lboost_thread
collect2: ld returned 1 exit status
What's the right way to install and link to libboost?
One thing I notice is that you do have no libboost_thread.so. You have
the versioned 1.46.1 file but usually libraries will create a symbolic
link to the versioned copy with the undecorated name. That might not
be it but it's one thing I noticed. (This is typically done by the
installer.) – Omaha
I think this is the point. It imply that I installed libboost the wrong way. In fact, I only installed libboost-dev:
sudo apt-get install libboost-dev
But what should I do is:
sudo apt-get install libboost-dev libboost1.46-doc libboost-date-time1.46-dev ibboost-filesystem1.46-dev libboost-graph1.46-dev libboost-iostreams1.46-dev libboost-math1.46-dev libboost-program-options1.46-dev libboost-python1.46-dev libboost-random1.46-dev libboost-regex1.46-dev libboost-serialization1.46-dev libboost-signals1.46-dev libboost-system1.46-dev libboost-test1.46-dev libboost-thread1.46-dev libboost-wave1.46-dev
(Or, in my particular case, install libboost-system1.46-dev libboost-thread1.46-dev at least)
And once you install them correctly, there should be .a and .so in /usr/lib.
/usr/lib$ ls | grep boost
libboost_date_time.a
libboost_date_time-mt.a
libboost_date_time-mt.so
libboost_date_time.so
libboost_date_time.so.1.46.1
libboost_filesystem.a
libboost_filesystem-mt.a
... and so on ...
In Ubuntu 16.04, the package is named: libboost-all-dev (not libboost-dev-all)
The comment box screwed up the quoting of this suggestion, so I'm posting it as an answer to get correct quoting.
It used to be, Ubuntu had the meta-package libboost-dev-all to install all of those. However, I can't seem to find it now. Here's a command line that might help:
sudo apt-get install `apt-cache search libboost | \
grep -- -dev | \
grep -v '[12]\.[0-9]' | \
awk '{ print $1; }'`
(Taken from https://github.com/imvu-open/istatd/ file install-boost-dev.sh )