Jerry Abu Ayoub
Jerry Abu Ayoub

Reputation: 13

Compiling opencv on ubuntu with C++ version 17

I'm trying to add a pnp solver to opencv I'm working on ubuntu OS. first I followed a tutorial on how to install opencv from source by cloning the repositories, then I tested the example and it worked so it compiled and installed succesfully. I began adding my files and I made sure that no names are duplicated and all the files have been added so there were no issues with dependancies. then I ran the cmake again, and ran the make command, but it is giving me the following error:-

opencv/modules/calib3d/src/RansacOptimalNPnP/../NPnP/DualVar.h:71:8: error: ‘optional’ in namespace ‘std’ does not name a template type
   71 |   std::optional<std::tuple<Eigen::Matrix3d, Eigen::Vector3d, double>>

I looked it up online and there is a possibility that I need to use C++ version 17 but the standard version in opencv is set to 11. what can I change in the opencv cmake list to change that?

Upvotes: 0

Views: 1278

Answers (2)

Nick Hockings
Nick Hockings

Reputation: 21

As I understand it Thamognya's answer, while useful to most users, misses what you are trying to do :

author and build a new module, to be compiled into the OpenCV library, (and your new module depends on C++17).

Re Setting C++ version and other options in Cmake:

To set the C++ version, and other Cmake options, I recommend using "cmake-gui". This is part of cmake, but separately installed in Ubuntu (and most Linix distros).

sudo apt-get install cmake-gui

You will find the c++ compiler options in the CMAKE_CXX* variables.

Re extending OpenCV:

If you have not already done so, I recommend cloning the OpenCV-contrib and OpenCV-extra repositories.

git clone https://github.com/opencv/opencv_contrib.git

git clone https://github.com/opencv/opencv_extra.git

These provide

  1. examples of how to write and build new modules for OpenCV,
  2. the data to run all the unit tests, to ensure your build is working correctly.

Re C++ version compatability:

C++ is intended to be backwards compatible, but there are details... ABI compatability between compiler versions is a related issue.

This thread gives information on mixing versions of C++ in a library: Is it safe to link C++17, C++14, and C++11 objects

I would recommend starting by setting C++ version in "cmake-gui", and seeing if there are problems in configuration, build file generation, and compilation. Start with a minimal build and add only what you need. (i.e. Don't take on more complexity/bugs than you have to.)

OpenCV has the option to build many of its dependencies from source, (rather than using the versions you may have insalled on your operating system). This may help avoid compatability issues.

Upvotes: 1

Thamognya
Thamognya

Reputation: 153

You can install OpenCV from ubuntu's opencv package via the following:

for python:

sudo apt-get install python3-opencv

for libopencv-dev:

sudo apt-get install libopencv-dev

If you want to compile it and not use ubuntu's OpenCV package, do the following:

# dependencies
sudo apt-get install cmake
sudo apt-get install gcc g++
# for python2 support
sudo apt-get install python-dev python-numpy
# for python3 support
sudo apt-get install python3-dev python3-numpy
# GTK support
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
# GTK 2 support
sudo apt-get install libgtk2.0-dev
# GTK 3 support
sudo apt-get install libgtk-3-dev
# Optional Dependencies
sudo apt-get install libpng-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libopenexr-dev
sudo apt-get install libtiff-dev
sudo apt-get install libwebp-dev

now that you are done with dependencies continue to the actual installation

sudo apt-get install git
git clone https://github.com/opencv/opencv.git
mkdir build
cd build/
cmake ../

if properly configured this is the output

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.6)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.6)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
--     packages path:               lib/python2.7/dist-packages
--
--   Python 3:
--     Interpreter:                 /usr/bin/python3.4 (ver 3.4.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.4m.so (ver 3.4.3)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2)
--     packages path:               lib/python3.4/dist-packages
make
sudo make install

This was shamelessly copied from opencv docs py setup in ubunutu

Upvotes: 0

Related Questions