Setup environment
1. Install CMake
cd ~
wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
tar xf cmake-3.14.5.tar.gz
cd cmake-3.14.5
./bootstrap --parallel=10
make -j4
sudo make -j4 install
2. Install Boost
cd ~
wget https://boostorg.jfrog.io/artifactory/main/release/1.69.0/source/boost_1_69_0.tar.gz
tar xf boost_1_69_0.tar.gz
cd boost_1_69_0
./bootstrap.sh
./b2 ... cxxflags="-std=c++0x -stdlib=libc++" linkflags="-stdlib=libc++" ...
sudo ./b2 toolset=gcc -j4 install
CMakeLists.txt
# Defines AppBase library target.
project(recipe_01)
cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 14)
message(FATAL_ERROR "app requires c++14 or newer")
elseif(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
find_package(Boost 1.60 REQUIRED COMPONENTS regex)
add_executable(main main.cpp)
target_link_libraries(main Boost::regex)
main.cpp
#include <boost/regex.hpp>
#include <iostream>
int main() {
// output the available regex syntaxes
std::cout
<< "Available regex syntaxes:\n"
<< "\t[0] Perl\n"
<< "\t[1] Perl case insensitive\n"
<< "\t[2] POSIX extended\n"
<< "\t[3] POSIX extended case insensitive\n"
<< "\t[4] POSIX basic\n"
<< "\t[5] POSIX basic case insensitive\n"
<< "Choose regex syntax: ";
// correctly set up flags, according to the chosen syntax
boost::regex::flag_type flag;
switch (std::cin.get()) {
case '0': flag = boost::regex::perl;
break;
case '1': flag = boost::regex::perl|boost::regex::icase;
break;
case '2': flag = boost::regex::extended;
break;
case '3': flag = boost::regex::extended|boost::regex::icase;
break;
case '4': flag = boost::regex::basic;
break;
case '5': flag = boost::regex::basic|boost::regex::icase;
break;
default:
std::cout << "Incorrect number of regex syntax. Exiting... \n";
return -1;
}
// Disabling exceptions
flag |= boost::regex::no_except;
//requesting regex patterns in a loop
// Restoring std::cin
std::cin.ignore();
std::cin.clear();
//Getting a String to match: in a loop
std::string regex, str;
do {
std::cout << "Input regex: ";
if (!std::getline(std::cin, regex) || regex.empty()) {
return 0;
}
// Without `boost::regex::no_except`flag this
// constructor may throw
const boost::regex e(regex, flag);
if (e.status()) {
std::cout << "Incorrect regex pattern!\n";
continue;
}
std::cout << "String to match: ";
while (std::getline(std::cin, str) && !str.empty()) {
bool matched = boost::regex_match(str, e);
std::cout << (matched ? "MATCH\n" : "DOES NOT MATCH\n");
std::cout << "String to match: ";
} // end of `while (std::getline(std::cin, str))`
std::cout << '\n';
// Restoring std::cin
std::cin.ignore();
std::cin.clear();
} while (1);
return 0;
} // int main()
To build
mkdir build
cd build
cmake ..
cmake --build .
And I get this error:
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable bin/main
CMakeFiles/main.dir/main.cpp.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char,
boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
main.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x8f): undefined reference to `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*,
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
CMakeFiles/main.dir/main.cpp.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
main.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*,
unsigned int)'
CMakeFiles/main.dir/main.cpp.o: In function `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>
> >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
main.cpp:(.text._ZN5boost16re_detail_10690012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_[_ZN5boost16re_detail_10690012perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_]+0x121): undefined reference to `boost::re_detail_106900::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:84: recipe for target 'bin/main' failed
make[2]: *** [bin/main] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I can not link to boost:regex with main function
Could you please tell me any solution for this issue?
I find some quote like
The boost 1.61 (and higher) are compiled with newer compilers, which use the _GLIBCXX_USE_CXX11_ABI=1 .. heh ABI. Meaning that for -std=c++11 std::basic_string... becomes std::__cxx11::basic_string... (and likewise for std::list; see the gcc Dual ABI documentation).
Since your linker error claims there is a missing symbol that contains std::string (which is short for std::basic_string<char,std::char_traits,std::allocator>), you are either compiling with a compiler that is too old and uses ABI 0, or you are even compiling explicitly with -D_GLIBCXX_USE_CXX11_ABI=0.
But how can I fix in my code?
Change the line
target_link_libraries(main Boost::regex)
to
target_link_libraries(main boost_regex)
And depending on your distro you may need to install some Boost devel packages.
I tried to compile simple program using OpenCV 4.11 and current version of Mingw on Windows 7. I downloaded binaries for OpenCV from https://github.com/huihut/OpenCV-MinGW-Build. I tried to compile it using command:
"g++ main.cpp -std=c++11 -I F:\cpp_tools\libs\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\include -L F:\cpp_tools\libs\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\lib -lopencv_core411.dll -lopencv_imgcodecs411.dll -lopencv_imgproc411.dll -lopencv_highgui411.dll -o myprog.exe"
and i got linker error:
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0xa0): undefined re
ference to cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&, int)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0x13e): undefined r
eference tocv::namedWindow(std::__cxx11::basic_string, std::allocator > const&, int)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0x19f): undefined r
eference to cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&, cv::_InputArray const&)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text+0x1c9): undefined r
eference tocv::waitKey(int)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text$_ZN2cv3MatD1Ev[__ZN
2cv3MatD1Ev]+0x2d): undefined reference to cv::fastFree(void*)'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text$_ZN2cv3Mat7releaseE
v[__ZN2cv3Mat7releaseEv]+0x40): undefined reference tocv::Mat::deallocate()'
C:\Users\Radek\AppData\Local\Temp\ccyTSmo1.o:main.cpp:(.text$ZN2cv3MataSEOS0[_
ZN2cv3MataSEOS0]+0xb4): undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status
I read alot of similar problems but any of it didn't solved my problem.
Source code:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
String imageName( "../data/HappyFish.jpg" ); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
image = imread( imageName, IMREAD_COLOR ); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );
imshow( "Display window", image );
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Shouldn't it be
g++ main.cpp -std=c++11 -I F:\cpp_tools\libs\OpenCV-MinGW-
Build-OpenCV-4.1.1-x64\include -L F:\cpp_tools\libs\OpenCV-
MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\lib
-llibopencv_core411 -llibopencv_imgcodecs411
-llibopencv_imgproc411 -llibopencv_highgui411 -o
myprog.exe
with -llibopencv instead of -lopencv?
Have you tried the help Running a C++ program with OpenCV 3.4.1 using MinGW-w64 g++ in Visual Studio Code on Windows 10 x64 mentioned on GitHub?
I am successfully running opencv python code on raspberry pi 2 (raspbian).
Now I want to try to compile opencv C++ code on raspberry pi 2 by using this command:
g++ -std=c++0x test_colour_tracking_1.cpp -otest_colour
The C++ coding as below.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while (true)
{
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("image",imgOriginal);
}
return 0;
}
But it show error as below.
/tmp/ccHcCqSm.o: In function `main':
test_colour_tracking_1.cpp:(.text+0x70): undefined reference to `cv::VideoCapture::VideoCapture(int)'
test_colour_tracking_1.cpp:(.text+0x7c): undefined reference to `cv::VideoCapture::isOpened() const'
test_colour_tracking_1.cpp:(.text+0xd8): undefined reference to `cv::VideoCapture::read(cv::Mat&)'
test_colour_tracking_1.cpp:(.text+0x150): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test_colour_tracking_1.cpp:(.text+0x164): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test_colour_tracking_1.cpp:(.text+0x1a4): undefined reference to `cv::VideoCapture::~VideoCapture()'
test_colour_tracking_1.cpp:(.text+0x1f0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/tmp/ccHcCqSm.o: In function `cv::Mat::~Mat()':
test_colour_tracking_1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccHcCqSm.o: In function `cv::Mat::release()':
test_colour_tracking_1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x58): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
And I want to ask how to check frame rate per second?
Try to use the following command:
g++ -std=c++0x test_colour_tracking_1.cpp -o test_colour `pkg-config --cflags --libs opencv`
I'm trying to write some code which uses openCV functions. I started by taking some of the example code available in the documentation:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1]); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
When I try to build it in Eclipse-CDT, I get this:
**** Build of configuration Debug for project openCV1 ****
make all
Building target: openCV1
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "openCV1" ./src/displayImage.o
./src/displayImage.o: In function `main':
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:25: undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:33: undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/jackstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:36: undefined reference to `cv::waitKey(int)'
./src/displayImage.o: In function `~Mat':
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
./src/displayImage.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
./src/displayImage.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [openCV1] Error 1
**** Build Finished ****
The same code, when I build with g++ (g++ -o displayImageInput displayImageInput.cpppkg-config opencv --cflags --libs) works.
I then changed the code to make the image greyscale,
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1]); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
Mat grey;
cvtColor(image, grey, CV_BGR2GRAY);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", grey);
waitKey(); // Wait for a keystroke in the window
return 0;
}
It gave the following error when building with g++:
dispImgSobel.cpp: In function ‘int main(int, char**)’:
dispImgSobel.cpp:34:27: error: ‘CV_BGR2GRAY’ was not declared in this scope
dispImgSobel.cpp:34:38: error: ‘cvtColor’ was not declared in this scope
I need help with two things, How to get it working in Eclipse, and second, how to resolve this scope error, for this and future use cases.
the first err is a linker problem. you did not link against opencv_core.a and opencv_highgui.a
rule of thumb: for every module header you include, you'll need to link the appropriate library.
the 2nd is a compiler problem, you forgot a header, #include <opencv2/imgproc/imgproc.hpp>
and, ofc. need to link opencv_imgproc
Make sure that everything in the output of pkg-config opencv --cflags --libs is in your search path for the linker.
There's also a pkg-config add-on for Eclipse which will allow you to put the exact string above in directly rather than manually adding each item of the output.
Installing OpenCV2.4.1 on windows 7 x64 - Mingw & codeblocks
Hi,
I followed this tutorial
Getting started with OpenCV 2.4 and MinGW on Windows 7
I configured the same except instead of x86, i put x64.
This is the example
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lena.jpg", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
when i compile main.cpp
C:\Users\rgap\Desktop>g++ -I"C:\opencv\build\include" -L"C:\opencv\build\x64\mingw\lib" main.cpp -lope
ncv_core241 -lopencv_highgui241 -o main
I get the following error
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0x62): undefined reference to `cv::imread(
std::string const&, int)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0xc7): undefined reference to `cv::_InputA
rray::_InputArray(cv::Mat const&)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0xfe): undefined reference to `cv::imshow(
std::string const&, cv::_InputArray const&)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text+0x120): undefined reference to `cv::waitKe
y(int)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text$_ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x2b): und
efined reference to `cv::fastFree(void*)'
C:\Users\rgap\AppData\Local\Temp\ccDgmPAT.o:main.cpp:(.text$_ZN2cv3Mat7releaseEv[cv::Mat::release()]+0
x3c): undefined reference to `cv::Mat::deallocate()'
collect2: ld devolvió el estado de salida 1
Is this a bug? :(
thanks :)