OpenCv undefined reference to `cv:: - c++

I am new at OpenCv. I am using Eclipse C/C++. When i try to run this sample code i faced with these errors. What should i do to solve this problem? Is there any problem at configurating ?
#using namespace std;
#using namespace cv;
int main( int argc, const char** argv )
{
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); /
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
16:11:28 **** Incremental Build of configuration Debug for project OpenCv ****
Info: Internal Builder is used for build
g++ "-IC:\\opencv\\build\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\OpenCv.o" "..\\src\\OpenCv.cpp"
g++ "-LC:\\opencv\\build\\x86\\vc12\\lib" -o OpenCv.exe "src\\OpenCv.o" -lopencv_calib3d249 -lopencv_contrib249 -lopencv_core249 -lopencv_features2d249 -lopencv_flann249 -lopencv_gpu249 -lopencv_highgui249 -lopencv_imgproc249 -lopencv_legacy249 -lopencv_ml249 -lopencv_nonfree249 -lopencv_objdetect249 -lopencv_ocl249 -lopencv_photo249 -lopencv_stitching249 -lopencv_superres249 -lopencv_ts249 -lopencv_video249 -lopencv_videostab249
src\OpenCv.o: In function `main':
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:10: undefined reference to `cv::imread(std::string const&, int)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:19: undefined reference to `cv::namedWindow(std::string const&, int)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:20: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:20: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:22: undefined reference to `cv::waitKey(int)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:24: undefined reference to `cv::destroyWindow(std::string const&)'
src\OpenCv.o: In function `ZN2cv3MatD1Ev':
C:/opencv/build/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
src\OpenCv.o: In function `ZN2cv3Mat7releaseEv':
C:/opencv/build/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status

problem1: you will have to include opencv / c++ header files to make it work:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#using namespace cv;
#include <iostream>
#using namespace std;
int main() {
...
then, problem2: you cannot use the vc12 libs with mingw. (it's a different compiler)
there are no more prebuild mingw libs for opencv, so, before doing anything else, you will have to build the opencv libs locally using cmake.
again, do you really need to use mingw / eclipse ? (vs express is still free)

Related

Trying to build C++ Dlib example fails to link with hundreds of undefined reference errors

I'm trying to compile a DLIB example: http://dlib.net/image_ex.cpp.html
and I copied it word-for-word:
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
// make sure the user entered an argument to this program
if (argc != 2)
{
cout << "error, you have to enter a BMP file as an argument to this program" << endl;
return 1;
}
And I used this command:
g++ dlib1.cpp -o dlib1 -std=c++11 -O3 -I ~/Packages/dlib/ -lpthread -lX11 -ljpeg -lpng -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT
And despite that command working fine months ago: [Can't include the JPEG_SUPPORT headers in a dlib cpp file ] this time, it gave me hundreds of errors:
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x7e): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x94): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xaa): undefined reference to `dlib::png_loader::is_graya() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xd0): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x176): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x1ce): undefined reference to `dlib::png_loader::is_graya() const'
What exactly is going wrong here?
Some of the samples need a command line such as ...
g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
I guess source.cpp is a "library as cpp code" link : dlib compile

Crosscompiling VisualGDB/C++ Cubietruck => Linkererror

i am trying to get an opencv c++ program, which runs on my laptop, on my hardware - furthermore i should mention, that i am new to embedded programming.
May someone can help me, because i got problems while cross-compiling with VisualGDB. I am using following board: Cubieboard 3 (Cubietruck- Dual-Core A20)
The toolchain is stored locally - so not on the board itself.
All libraries are included and detected in Visual Studio --> take a look at the Screenshot:
The "opencv_world320d.lib" - Library includes all the needed sub libraries - i found that here
The C++ code itself:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
Mat img(500, 1000, CV_8UC3, Scalar(0, 0, 100)); //create an image ( 3 channels, 8 bit image depth, 500 high, 1000 wide, (0, 0, 100) assigned for Blue, Green and Red plane respectively. )
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
When I try to build the linker fails with following message:
1>------ Erstellen gestartet: Projekt: LinuxProject2, Konfiguration: Debug VisualGDB ------
1> Linking VisualGDB/Debug/LinuxProject2...
1> VisualGDB/Debug/LinuxProject2.o: In function `cv::String::String(char const*)':
1>C:\OpenCV-3.2.0\opencv\build\include\opencv2\core\cvstd.hpp(622): error : undefined reference to `cv::String::allocate(unsigned int)'
1> VisualGDB/Debug/LinuxProject2.o: In function `cv::String::~String()':
1>C:\OpenCV-3.2.0\opencv\build\include\opencv2\core\cvstd.hpp(664): error : undefined reference to `cv::String::deallocate()'
1> VisualGDB/Debug/LinuxProject2.o: In function `cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)':
1>C:\OpenCV-3.2.0\opencv\build\include\opencv2\core\mat.inl.hpp(352): error : undefined reference to `cv::Mat::operator=(cv::Scalar_<double> const&)'
1> VisualGDB/Debug/LinuxProject2.o: In function `cv::Mat::~Mat()':
1>C:\OpenCV-3.2.0\opencv\build\include\opencv2\core\mat.inl.hpp(592): error : undefined reference to `cv::fastFree(void*)'
1> VisualGDB/Debug/LinuxProject2.o: In function `cv::Mat::create(int, int, int)':
1>C:\OpenCV-3.2.0\opencv\build\include\opencv2\core\mat.inl.hpp(684): error : undefined reference to `cv::Mat::create(int, int const*, int)'
1> VisualGDB/Debug/LinuxProject2.o: In function `cv::Mat::release()':
1>C:\OpenCV-3.2.0\opencv\build\include\opencv2\core\mat.inl.hpp(704): error : undefined reference to `cv::Mat::deallocate()'
1> VisualGDB/Debug/LinuxProject2.o: In function `main':
1>D:\Softwareentwicklung\Projects\LinuxProject2\LinuxProject2.cpp(18): error : undefined reference to `cv::namedWindow(cv::String const&, int)'
1>D:\Softwareentwicklung\Projects\LinuxProject2\LinuxProject2.cpp(19): error : undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
1>D:\Softwareentwicklung\Projects\LinuxProject2\LinuxProject2.cpp(21): error : undefined reference to `cv::waitKey(int)'
1>D:\Softwareentwicklung\Projects\LinuxProject2\LinuxProject2.cpp(23): error : undefined reference to `cv::destroyWindow(cv::String const&)'
1>collect2.exe : error : ld returned 1 exit status
Maybe someone had the same problems - i really searched a lot, but i wasn't able to find similar issues.
You have only told the toolchain where to look when searching for libraries ("Library directory"). You have not told it which libraries to link to... So it not being able to find any symbols from those missing libraries is completely unsurprising.
So, in addition to the library search directory/ies, you also need to populate the "Library names" field with the names of any libraries whose symbols you are referencing - in this case, at least opencv. That way, the linker can link to those libraries and therefore resolve those symbols.
Edit as per now-completely-different question
Based on this thread:
Telling gcc directly to link a library statically
It looks like you should move any static libraries into the "Additional linker" flags, as the "Library names" generates -l switches (not -l:), which are for dynamic libraries, but you are trying to link a static library.

Allegro 5 game won't compile with GCC

I'm trying to port over a game that I made using Allegro 5.0 (from Windows to a Raspberry Pi running Raspian). I installed Allegro and moved over all my source files, but when I try to compile using: *g++ -std=c++0x .cpp -o SpaceFighter $(pkg-config --libs allegro-5) I get the following:
/tmp/ccBliSky.o: In function main':
main.cpp:(.text+0x130): undefined reference toal_show_native_message_box'
main.cpp:(.text+0x160): undefined reference to al_init_font_addon'
main.cpp:(.text+0x164): undefined reference toal_init_ttf_addon'
main.cpp:(.text+0x168): undefined reference to al_init_image_addon'
main.cpp:(.text+0x1a4): undefined reference toal_load_ttf_font'
main.cpp:(.text+0x574): undefined reference to al_draw_textf'
/tmp/ccBMyqom.o: In functionMenuItem::MenuItem()':
MenuItem.cpp:(.text+0xa0): undefined reference to al_load_ttf_font'
/tmp/ccBMyqom.o: In functionMenuItem::~MenuItem()':
MenuItem.cpp:(.text+0x1ac): undefined reference to al_destroy_font'
/tmp/ccBMyqom.o: In functionMenuItem::Draw(GameTime const*)':
MenuItem.cpp:(.text+0x2fc): undefined reference to al_draw_text'
/tmp/ccKXP3ds.o: In functionPlayerShip::SetAudioSample(std::string)':
PlayerShip.cpp:(.text+0x604): undefined reference to al_destroy_sample'
PlayerShip.cpp:(.text+0x64c): undefined reference toal_load_sample'
collect2: error: ld returned 1 exit status
AND NO THIS IS NOT A DUPLICATE OF "What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them?"
I know what usually causes most undefined reference errors. This question is specific to the allegro library I'm using.
Here's a bit of code (I'm obviously not going to post the whole game):
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_image.h>
#include <iostream>
#include "Game.h"
int main(int argc, char *argv[])
{
const char errorType[] = "Initialization Error";
const int flags = ALLEGRO_MESSAGEBOX_ERROR;
// Initialize Allegro
if (!al_init())
{
const char message[] = "Failed to initialize Allegro Library.";
//al_show_native_message_box(nullptr, "Error", errorType, message, nullptr, flags);
return -1;
}
Game game;
// Initialize a new window for gameplay.
int screenWidth = Game::GetScreenWidth();
int screenHeight = Game::GetScreenHeight();
Thanks in advance for any help!
Alegro divides its functionality across several modules. Your pkg-config line must include all the modules you are using. Try the following:
pkg-config --libs allegro-5 allegro_font-5 allegro_image-5 allegro_ttf-5
I had the same problem with the al_show_native_message_box function and I solved it by adding the following flag:
-lallegro_dialog

Compile/Debug code in CodeLite with -lX11 argument (On Linux Ubuntu 16.04 LTS)

I've been trying to debug and compile my C++ with Xlib code in CodeLite for a while now, but I can't seem to pass the -lX11 argument to the gcc compiler in CodeLite. How would I go about doing this (On Linux Ubuntu 16.04 LTS)?
Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <X11/Xlib.h>
int main(int argc, char **argv){
Display *display;
XEvent event;
Window win;
int screen;
bool running = true;
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
win = XCreateSimpleWindow(display,XRootWindow(display,screen),100,100,500,300,1,XBlackPixel(display,screen),XWhitePixel(display,screen));
XMapWindow(display,win);
while(running){
XNextEvent(display,&event);
}
}
Console Output:
----------Building project:[ Xlib - Debug ]----------
make[1]: Entering directory '/home/owner/Documents/C/Xlib'
/usr/bin/g++ -o ./Debug/Xlib #"Xlib.txt" -L.
./Debug/main.cpp.o: In function `main':
/home/owner/Documents/C/Xlib/main.cpp:14: undefined reference to `XOpenDisplay'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XWhitePixel'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XBlackPixel'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XRootWindow'
/home/owner/Documents/C/Xlib/main.cpp:16: undefined reference to `XCreateSimpleWindow'
/home/owner/Documents/C/Xlib/main.cpp:17: undefined reference to `XMapWindow'
/home/owner/Documents/C/Xlib/main.cpp:20: undefined reference to `XNextEvent'
Thanks in advance.

Compile c++ code using Magick++ and openMPI

I'm trying to compile my C++ code using Magick++ library to manipulate images in a distributed way using openMPI and I get some errors when I try to compile it.
This is my code:
#include "mpi.h"
#include <stdio.h>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv){
int rank, numtask;
InitializeMagick(*argv);
Image image;
try {
// Read a file into image object
image.read( "test_image.jpg" );
image.type( GrayscaleType );
Blob blob;
image.magick( "JPEG" ); // Set JPEG output format
image.write( &blob );
}
catch( Exception &error_ ){
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
//Now in the "distributed enviroment" I just print an hello world to test it.
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtask);
cout<<"HelloWorld\n";
MPI_Finalize();
}
this is the command that I type on the shell
mpiCC openmpi_project.cc -o openmpi_project
and this is the output of the shell
openmpi_project.cc:(.text+0x1d): undefined reference to
"Magick::InitializeMagick(char const*)"
openmpi_project.cc:(.text+0x29): undefined reference to
"Magick::Image::Image()"
openmpi_project.cc:(.text+0x5d): undefined reference to
"Magick::Image::read(std::string const&)"
openmpi_project.cc:(.text+0x86): undefined reference to
"Magick::Image::type(MagickCore::ImageType)"
openmpi_project.cc:(.text+0x92): rundefined reference to
"Magick::Blob::Blob()"
openmpi_project.cc:(.text+0xc6): undefined reference to
"Magick::Image::magick(std::string const&)"
openmpi_project.cc:(.text+0xf1): undefined reference to
"Magick::Image::write(Magick::Blob*)"
openmpi_project.cc:(.text+0xfd): undefined reference to
"Magick::Blob::~Blob()"
openmpi_project.cc:(.text+0x158): undefined reference to
"Magick::Image::~Image()"
openmpi_project.cc:(.text+0x1d3): undefined reference to
"Magick::Blob::~Blob()"
openmpi_project.cc:(.text+0x261): undefined reference to
"Magick::Image::~Image()"
/tmp/ccqFzUdy.o:(.gcc_except_table+0x58): undefined reference to
"typeinfo for Magick::Exception"
ImageMagick ships with config utilities. For Magick++ this utility is Magick++-config. See the Usage sub-section under the API docs.
LDFLAGS=$(Magick++-config --ldflags)
CXXFLAGS=$(Magick++-config --cxxflags)
$(CC) $CXXFLAGS openmpi_project.cc $LDFLAGS -o openmpi_project
Jump over to the MPI compiling/linking docs, and integrate Magick++'s additional flags to mpiCC
LDFLAGS=$(Magick++-config --ldflags)
CXXFLAGS=$(Magick++-config --cxxflags)
mpiCC --with-wrapper-cxxflags=$CXXFLAGS openmpi_project.cc \
--with-wrapper-ldflags=$LDFLAGS -o openmpi_project