Using OpenCV in Eclipse (getting started) - c++

I want to use OpenCV in Eclipse Neon for C++. Unfortunately, there are some linking errors while building the project.
I followed this tutorial and passed following steps:
Installing Eclipse Neon for C/C++ (Windows 64-bit) http ://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/neon1a
Installing MinGW (mingw-get-setup.exe, https ://sourceforge.net/projects/mingw/files/Installer/) in the folder C:\MinGW
Installing OpenCV 3.1 for Windows (http ://opencv.org/downloads.html) in the folder C:\opencv
Creating a new and empty C++ project in eclipse
Adding the includes and libaries like described in the tutorial
Adding a source file Test.cpp which does not gives any errors
#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] : "lenna.png", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
Building the project
Building the project returns following errors:
19:00:45 **** Incremental Build of configuration Release for project OpenCV ****
Info: Internal Builder is used for build
g++ "-IC:\\opencv\\build\\include" -O3 -Wall -c -fmessage-length=0 -o Test.o "..\\Test.cpp"
g++ "-LC:\\opencv\\build\\x64\\vc12\\lib" "-LC:\\opencv\\build\\x64\\vc14\\lib" -o OpenCV.exe Test.o -lopencv_world310
Test.o:Test.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x2d): undefined reference to `cv::String::allocate(unsigned int)'
Test.o:Test.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x15): undefined reference to `cv::Mat::deallocate()'
Test.o:Test.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x6d): undefined reference to `cv::fastFree(void*)'
Test.o:Test.cpp:(.text.startup+0x4f): undefined reference to `cv::imread(cv::String const&, int)'
Test.o:Test.cpp:(.text.startup+0x56): undefined reference to `cv::String::deallocate()'
Test.o:Test.cpp:(.text.startup+0xc0): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
Test.o:Test.cpp:(.text.startup+0xc7): undefined reference to `cv::String::deallocate()'
Test.o:Test.cpp:(.text.startup+0xd3): undefined reference to `cv::waitKey(int)'
Test.o:Test.cpp:(.text.startup+0x134): undefined reference to `cv::String::deallocate()'
Test.o:Test.cpp:(.text.startup+0x161): undefined reference to `cv::String::deallocate()'
collect2.exe: error: ld returned 1 exit status
19:00:45 Build Finished (took 781ms)
Where can be the error?
Thank you for helping!

Related

Compiling c++ mysql connector library with g++ results in undefined reference errors on Windows

I am trying to compile a c++ program using MySQL connector using g++ on Windows 11. I have installed the connector library using the MySQL installer. No matter how I try to compile it, the compiler returns undefined reference to ... errors.
The program is as follows:
#include <mysqlx/xdevapi.h>
int main() {
return 0;
}
I try to compile with
g++ -o mysql.exe -std=c++11 -I "C:/Program Files/MySQL/Connector C++ 8.0/include" -L"C:/Program Files/MySQL/Connector C++ 8.0/lib64" mysql.cpp -lmysqlcppconn8-2-vs14
I have also tried to compile with the static library. (Not sure what the difference is)
g++ -o mysql.exe -std=c++11 -I "C:/Program Files/MySQL/Connector C++ 8.0/include" -L"C:/Program Files/MySQL/Connector C++ 8.0/lib64/vs14" mysql.cpp -lmysqlcppconn8-static
I get the same error no matter which connector i try to link to:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\matsh\AppData\Local\Temp\ccJYEfB7.o:mysql.cpp:(.text$_ZN6mysqlx4abi22r06string6traitsIcE6to_strB5cxx11ERKS2_[_ZN6mysqlx4abi22r06string6traitsIcE6to_strB5cxx11ERKS2_]+0x1c): undefined reference to `mysqlx::abi2::r0::string::Impl::to_utf8[abi:cxx11](mysqlx::abi2::r0::string const&)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\matsh\AppData\Local\Temp\ccJYEfB7.o:mysql.cpp:(.text$_ZNK6mysqlx4abi22r05Value5printERSo[_ZNK6mysqlx4abi22r05Value5printERSo]+0x197): undefined reference to `mysqlx::abi2::r0::common::Value::print(std::ostream&) const'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\matsh\AppData\Local\Temp\ccJYEfB7.o:mysql.cpp:(.rdata$_ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE]+0x20): undefined reference to `mysqlx::abi2::r0::common::Value::print(std::ostream&) const'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\matsh\AppData\Local\Temp\ccJYEfB7.o:mysql.cpp:(.rdata$.refptr._ZTVN6mysqlx4abi22r05DbDocE[.refptr._ZTVN6mysqlx4abi22r05DbDocE]+0x0): undefined reference to `vtable for mysqlx::abi2::r0::DbDoc'
collect2.exe: error: ld returned 1 exit status
I understand that similar questions have been asked previously, but most of them are on Linux and none solve my problem. What am I doing wrong? Am I using the right compiler arguments?
Edit:
As a workaround, I followed this tutorial on how to compile the code in Visual Studio 2022: https://www.youtube.com/watch?v=a_W4zt5sR1M

Issues with libboost_thread linking

I'm just making my way through finishing the boost http server project, which was written in visual studio using msvc compiler. All went well until I started porting this to linux. Here's a linking error displayed in console
http_server.cpp:(.text+0x995): undefined reference to `vtable for boost::detail::thread_data_base'
/usr/bin/ld: http_server.cpp:(.text+0xa05): undefined reference to `pthread_condattr_setclock'
/usr/bin/ld: http_server.cpp:(.text+0xb83): undefined reference to `boost::thread::start_thread_noexcept()'
/usr/bin/ld: http_server.cpp:(.text+0xc69): undefined reference to `boost::thread::native_handle()'
/usr/bin/ld: http_server.cpp:(.text+0xc82): undefined reference to `boost::thread::join_noexcept()'
/usr/bin/ld: /tmp/ccFIQ0RA.o: in function `boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_context>, boost::_bi::list1<boost::_bi::value<boost::asio::io_context*> > > >::~thread_data()':
Here's my Makefile
STANDARD = c++11
LIBPATH=/usr/lib/x86_64-linux-gnu
LINUX_THREAD=-lpthread
BOOST_THREAD=/usr/lib/x86_64-linux-gnu/libboost_thread.a
OUTPUT=cppserver
build_gcc:
g++ -O2 -std=$(STANDARD) $(LINUX_THREAD) -o $(OUTPUT) CppServer/servers/http_server/http_server.cpp ;\
CppServer/servers/http_server/connection.cpp CppServer/core/app.cpp CppServer/core/dbconn.cpp ;\
CppServer/servers/http_server/response.cpp CppServer/servers/http_server/request.cpp ;\
CppServer/math_unit.cpp CppServer/main.cpp $(BOOST_THREAD) -L$(LIBPATH)
I also tried to set BOOST_THREAD to 'boost_thread', but it failed as well.
Any ideas?

error compiling c++ opencv application on Raspberry Pi

I have been spending the last few days attempting to address an error I have while compiling a brief example program I found online with opencv. The code for the application can be found below, with the console command and error following. Any and all help is appreciated.
.cpp file:
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(){
Mat img = imread("/home/pi/opencv/testPic.jpg",CV_LOAD_IMAGE_COLOR);
imshow("opencvtest",img);
waitKey(0);
return 0;
}
console command and error:
pi#LaserPhysics_RasPi ~/opencv $ g++ -o cppTest cppTest.cpp -I/usr/local/include/
/tmp/ccA0pvw2.o: In function `main':
cppTest.cpp:(.text+0x30): undefined reference to `cv::imread(cv::String const&, int)'
cppTest.cpp:(.text+0x74): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
cppTest.cpp:(.text+0x94): undefined reference to `cv::waitKey(int)'
/tmp/ccA0pvw2.o: In function `cv::String::String(char const*)':
cppTest.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x50): undefined reference to `cv::String::allocate(unsigned int)'
/tmp/ccA0pvw2.o: In function `cv::String::~String()':
cppTest.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccA0pvw2.o: In function `cv::Mat::~Mat()':
cppTest.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccA0pvw2.o: In function `cv::Mat::release()':
cppTest.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
You have to link your binary against OpenCV... Assuming you properly installed OpenCV this can be done with:
g++ -o cppTest cppTest.cpp -lopencv_core -lopencv_highgui -lopencv_imgproc

Unable to compile C++ OpenCV2.4.9, Win7x64, Eclipse Mars, MinGW

Little new to OpenCV, i've made some stuff in Java OpenCV, but limited with this lib, i've step to C/C++ with much difficulties!
I've succeded building and running C OpenCV projects, but seeing most of tutorials running on C++ version, i'm trying to make a simple "HelloCV":
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("c:/lenna.png", CV_LOAD_IMAGE_COLOR);
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
imshow("MyWindow", img);
waitKey(0);
return 0;
}
I always get the following errors :
../src/mainCPP.cpp:12: undefined reference to `cv::imread(std::string const&, int)'
../src/mainCPP.cpp:14: undefined reference to `cv::namedWindow(std::string const&, int)'
../src/mainCPP.cpp:15: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
../src/mainCPP.cpp:15: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
../src/mainCPP.cpp:17: undefined reference to `cv::waitKey(int)'
src\maincpp.o: In function `ZN2cv3MatD1Ev':
C:/OpenCV_2.4.9/opencv/build/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
src\maincpp.o: In function `ZN2cv3Mat7releaseEv':
C:/OpenCV_2.4.9/opencv/build/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
I've followed about 4-5 installations tuto, but I only succed in crashing my projects!
My "working" settings :
C/C++ Build -> GCC C++ Compiler -> Includes -> Include paths (-I) : path to my include folder ( "C:\OpenCV_2.4.9\opencv\build\include" )
C/C++ Build -> GCC C++ Linker -> Libraries -> Library search path (-L) -> "C:\OpenCV_2.4.9\opencv\build\x86\vc11\lib",
I know I've put the path to the x86 instead of x64 due to lib error like : "Not compatible system architecture"...!
C/C++ Build -> GCC C++ Linker -> Libraries -> Libraries (-l) : opencv_core249, opencv_imgproc249 and highgui249 (i've tested including all libs but it's the same)
I've added the environment variable : C:\OpenCV_2.4.9\opencv\build\x86\vc11\bin (isn't it only for M$ Visual Studio?!)
Seems like openCV isn't getting installed properly. Try using Chocolatey, it can cleanly install the latest version of OpenCV on windows.

OpenCV 2.3 Compiling Issue - Undefined Refence - Ubuntu 11.10

System Info:
Ubuntu 11.10 (64 bits) with OpenCV 2.3 (installed today)
I'm trying to compile some very simple code in OpenCV 2.3 but I'm getting a weird error.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(){
cv::Mat image=cv::imread("img.jpg");
cv::namedWindow("My Image");
cv::imshow("My Image",image);
cv::waitKey(0);
return 1;
}
however, I'm getting these error messages...
-SG41:~/Desktop$ g++ `pkg-config opencv --cflags --libs` -o test_1 test_1.cpp
/tmp/ccCvS1ys.o: In function `main':
test_1.cpp:(.text+0x44): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
test_1.cpp:(.text+0x8e): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
test_1.cpp:(.text+0xbc): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test_1.cpp:(.text+0xf0): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test_1.cpp:(.text+0x112): undefined reference to `cv::waitKey(int)'
/tmp/ccCvS1ys.o: In function `cv::Mat::~Mat()':
test_1.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccCvS1ys.o: In function `cv::Mat::release()':
test_1.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
I am guessing that at least some of the libraries in the output of
pkg-config opencv --libs
are archive libraries. It is incorrect to put archive libraries before sources that need them (test_1.cpp in this case): the order of sources and libraries on the link line matters.
Try
g++ -o test_1 test_1.cpp `pkg-config opencv --cflags --libs`
I was having the same problem, but I found out pkg-config opencv --cflags is printing "-I/usr/include/opencv" instead of "-I/usr/include/opencv2"... Maybe a package bug on Ubuntu?
I am using cmake and had similar problems.
Something weird is going on with the cmake configuration files.
For me the problems were solved by simply setting OPENCV_FOUND to TRUE and OpenCV_FOUND to TRUE.
Also I had to set OpenCV_DIR to /usr/local/share/OpenCV.
See also CMake error configuring opencv
#EmployedRussian 's answer worked for me too. For those who are wondering how to specify this command in Eclipse, use this post -
https://www.eclipse.org/forums/index.php?t=msg&goto=233377&
Instead of adding gtk+, use opencv;
Instead of adding the new flags to 'Miscellaneous linker flags', add the new flags at the end after ${INPUT} in -
Project->Right click->Properties->C/C++ Build ->Settings->GCC C++ linker->Expert Settings: command line pattern