OpenCV 2.3 Compiling Issue - Undefined Refence - Ubuntu 11.10 - c++

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

Related

How to recompile ntl and gmp libraries with "-fPIC" flag & make HELib as a shared library & undefined reference to func(std::ostream&) error

a C++ code built into a shared library for HELib is using NTL and GMP static library. But it gets following error:
/usr/bin/ld: /usr/local/lib/libntl.a(FFT.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libntl.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
It was suggested int the post Click here
to recompile NTL and GMP with "-fPIC " flags.
I'am not able to find how I should do that.
Edit:
I'm able to build the shared library now after :
recompiling GMP and NTL by:
./configure --enable-shared
for gmp and
./configure SHARED=on
After make Install of HELib. I get error when I run the example codes.
The link to the Makefile : click to see makefile
Error:
g++ -g -O2 -std=c++11 -pthread -DFHE_THREADS -DFHE_BOOT_THREADS -DFHE_DCRT_THREADS -o Test_General_x Test_General.cpp -L/usr/local/lib -lntl -lgmp -lm -lfhe
/usr/local/lib/libfhe.so: undefined reference to write_raw_int(std::ostream&, long, long)'
/usr/local/lib/libfhe.so: undefined reference toread_raw_ZZ(std::istream&, NTL::ZZ&)'
/usr/local/lib/libfhe.so: undefined reference to void write_raw_vector<long>(std::ostream&, std::vector<long, std::allocator<long> > const&)'
/usr/local/lib/libfhe.so: undefined reference towriteEyeCatcher(std::ostream&, char const*)'
/usr/local/lib/libfhe.so: undefined reference to write_raw_xdouble(std::ostream&, NTL::xdouble)'
/usr/local/lib/libfhe.so: undefined reference toread_ntl_vec_long(std::istream&, NTL::Vec&)'
/usr/local/lib/libfhe.so: undefined reference to void read_raw_vector<long>(std::istream&, std::vector<long, std::allocator<long> >&)'
/usr/local/lib/libfhe.so: undefined reference toreadEyeCatcher(std::istream&, char const*)'
/usr/local/lib/libfhe.so: undefined reference to read_raw_int(std::istream&, long)'
/usr/local/lib/libfhe.so: undefined reference toread_raw_xdouble(std::istream&)'
/usr/local/lib/libfhe.so: undefined reference to write_raw_ZZ(std::ostream&, NTL::ZZ const&)'
/usr/local/lib/libfhe.so: undefined reference towrite_ntl_vec_long(std::ostream&, NTL::Vec const&, long)'
collect2: error: ld returned 1 exit status
Makefile:179: recipe for target 'Test_General_x' failed
make: *** [Test_General_x] Error 1
For NTL v11.5.1 atleast, doing ./configure --help | grep -i pic (as suggested in one of the comments for GMP) did not help at all - it found no matches. However, making a one line change in the file ntl-11.5.1/src/DoConfig (on line 17) from:
'CXXFLAGS' => '-g -O2'
to:
'CXXFLAGS' => '-g -O2 -fPIC',
solved the problem for me.
Marc Glisse provided the answer for the first two parts of the question.For the third part "Undefined Reference error" the answer is I'd not compiled and linked a x.cpp containing the functions that caused the undefined reference into my shared library. hence check : nm -CD /usr/local/lib/libfhe.so to see if these functions are listed with a linking address or not. If not then check which code provides this functionality. Link that code to the shared library.

Using OpenCV in Eclipse (getting started)

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!

how to compile and run this program, pthread issue?

I would like to compile the following program that makes alignment of fasta sequences, the program is available in:
http://compbio.cs.princeton.edu/mems/
I have downloaded a zip file called sparseMEM and downloaded the g++ in Ubuntu Linux, in the directory created there is a Makefile archive, so I tried to compile with make. The problem is that I got the following error:
g++ -lpthread -O3 -DSIXTYFOURBITS mummer.o qsufsort.o sparseSA.o fasta.o -o mummer
mummer.o: In function `main':
mummer.cpp:(.text.startup+0x480): undefined reference to `pthread_create'
mummer.cpp:(.text.startup+0x4a8): undefined reference to `pthread_join'
sparseSA.o: In function `sparseSA::MEM(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::vector<match_t, std::allocator<match_t> >&, int, bool, int)':
sparseSA.cpp:(.text+0x3bbc): undefined reference to `pthread_create'
sparseSA.cpp:(.text+0x3be0): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
make: *** [mummer] Error 1
how I can make it run?
also if somebody knows how to test this program it would be great
thanks
For Linux you need -pthread instead of -lpthread.

Linking issues with wxWidget sample on Ubuntu 12.04 (amd64)

I have a problem linking the wxWidget sample application (http://www.wxwidgets.org/docs/tutorials/hello.htm) with is stored in main.cpp. I try to compile and link it using:
g++ `wx-config --cxxflags --libs` main.cpp
The output I get is the following:
/tmp/ccFHWUaX.o: In function `wxCreateApp()':
main.cpp:(.text+0x31): undefined reference to `wxAppConsole::CheckBuildOptions(char const*, char const*)'
/tmp/ccFHWUaX.o: In function `main':
main.cpp:(.text+0x91): undefined reference to `wxEntry(int&, char**)'
/tmp/ccFHWUaX.o: In function `MyFrame::MyFrame(wxString const&, wxPoint const&, wxSize const&)':
main.cpp:(.text+0x1d2): undefined reference to `wxFrameNameStr'
main.cpp:(.text+0x267): undefined reference to `wxEmptyString'
main.cpp:(.text+0x2ea): undefined reference to `wxEmptyString'
main.cpp:(.text+0x366): undefined reference to `wxMenuBar::wxMenuBar()'
main.cpp:(.text+0x3d1): undefined reference to `wxFrameBase::SetMenuBar(wxMenuBar*)'
main.cpp:(.text+0x3da): undefined reference to `wxStatusLineNameStr'
main.cpp:(.text+0x407): undefined reference to `wxFrame::CreateStatusBar(int, long, int, wxString const&)'
main.cpp:(.text+0x44f): undefined reference to `wxFrameBase::SetStatusText(wxString const&, int)'
main.cpp:(.text+0x533): undefined reference to `wxFrame::~wxFrame()'
(and many lines more...)
WxWidgets-2.8 is installed using the ubuntu repository and its libs are located in /usr/lib/x86_64-linux-gnu. I also tried to build specifying the library path with:
-L/usr/lib/x86_64-linux-gnu/
but, this does not change the output. I was blaming multiarch for my problem, but actually only as I don't know how it works exactly.
Can someone tell me how to build the sample correctly?
Thank you
Michael
When using static linking, the libraries must always come after the object files using the symbols from them, otherwise they're simply ignored by the linker as they're not needed at the moment when it first sees them. So us2012 is correct, you need to put wx-config part after your source file.
You could also use shared wxWidgets libraries, then the order wouldn't matter. But it's still a good habit to use the right order, which works for both static and shared libraries, anyhow.

Why simple mysql++ code compiles standalone, but not in project?

At first, small program:
#include <mysql++.h>
using namespace mysqlpp;
void mainuu ()
{ Connection conn("mysql", "localhost", "root", "pwd");}
If I compile it as one file in CodeLite or in such way:
g++ -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlclient -lmysqlpp -o Test mysql_api.cpp
it's ok
but, when I try to build whole project with this file I get this:
g++ -o ./Debug/server ./Debug/main.o ./Debug/log.o ./Debug/packet.o ./Debug/mysql_api.o -L.
./Debug/mysql_api.o: In function `mainuu()':
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:10: undefined reference to `mysqlpp::Connection::Connection(char const*, char const*, char const*, char const*, unsigned int)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:12: undefined reference to `mysqlpp::Connection::query(char const*)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:13: undefined reference to `mysqlpp::SQLTypeAdapter::SQLTypeAdapter(char const*, bool)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:13: undefined reference to `mysqlpp::operator<<(mysqlpp::quote_type1, mysqlpp::SQLTypeAdapter const&)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:19: undefined reference to `mysqlpp::operator<<(std::basic_ostream<char, std::char_traits<char> >&, mysqlpp::String const&)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:10: undefined reference to `mysqlpp::Connection::~Connection()'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:10: undefined reference to `mysqlpp::Connection::~Connection()'
./Debug/mysql_api.o: In function `mysqlpp::Row::operator[](int) const':
/usr/include/mysql++/row.h:328: undefined reference to `mysqlpp::Row::at(unsigned int) const'
./Debug/mysql_api.o: In function `mysqlpp::Query::store()':
/usr/include/mysql++/query.h:467: undefined reference to `mysqlpp::Query::str(mysqlpp::SQLQueryParms&)'
/usr/include/mysql++/query.h:467: undefined reference to `mysqlpp::SQLTypeAdapter::SQLTypeAdapter(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/include/mysql++/query.h:467: undefined reference to `mysqlpp::Query::store(mysqlpp::SQLTypeAdapter const&)'
collect2: ld returned 1 exit status
make[1]: *** [Debug/server] Error 1
make[1]: Leaving directory `/home/asyler/.codelite/workspace/test/server'
make: *** [All] Error 2
Here is CodeLite g++ compiler settings:
-g -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlclient -lmysqlpp -L/usr/lib/mysql -L/usr/lib/mysql++ -lmysql++
Looks like you need to edit CodeLite project settings and add these settings -lmysqlclient -lmysqlpp that you pass in command line. Fill Library Path and Libraries fields on Linker tab.
Those are linker errors.
When you create your final executable, you still must provide references to all library functions, just as you did when you compiled the single translation unit.
So, pass -lmysqlclient -lmysqlpp to g++ this time, too.
If you are using an Integrated Development Environment, configure your project's build settings accordingly. In particular, I see that CodeLite has both "Compiler" and "Linker" build settings. It's "Linker" settings that you're after.
For more information on the build process (i.e. compiling, linking and the difference), read a good C++ book.