Use OpenCV library in CodeBlocks - c++

I am using Codeblocks 16.01 with OpenCV 3.2.0-vc14 which is installed to C:\opencv
The compile and link search directory is set in CodeBlocks already.
I wrote an openCV example, and there's a conflict as following:
-------------- Build: Debug in try_opencv (compiler: openCL)---------------
mingw32-g++.exe -L"C:\Program Files (x86)\Intel\OpenCL_SDK\6.3\lib\x86" -LC:\opencv\build\x64\vc14\lib -LC:\opencv\build\x64\vc14\bin -o bin\Debug\try_opencv.exe obj\Debug\main.o -lOpenCL C:\opencv\build\x64\vc14\lib\opencv_world320.lib C:\opencv\build\x64\vc14\lib\opencv_world320d.lib
obj\Debug\main.o: In function `main':
D:/TheOpenCLProgrammingBook/image_process_test/try_opencv/main.cpp:9: undefined reference to `cv::imread(cv::String const&, int)'
D:/TheOpenCLProgrammingBook/image_process_test/try_opencv/main.cpp:10: undefined reference to `cv::namedWindow(cv::String const&, int)'
D:/TheOpenCLProgrammingBook/image_process_test/try_opencv/main.cpp:11: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
D:/TheOpenCLProgrammingBook/image_process_test/try_opencv/main.cpp:12: undefined reference to `cv::waitKey(int)'
obj\Debug\main.o: In function `ZN2cv6StringC1EPKc':
C:/opencv/build/include/opencv2/core/cvstd.hpp:622: undefined reference to `cv::String::allocate(unsigned int)'
obj\Debug\main.o: In function `ZN2cv6StringD1Ev':
C:/opencv/build/include/opencv2/core/cvstd.hpp:664: undefined reference to `cv::String::deallocate()'
obj\Debug\main.o: In function `ZN2cv3MatD1Ev':
C:/opencv/build/include/opencv2/core/mat.inl.hpp:592: undefined reference to `cv::fastFree(void*)'
obj\Debug\main.o: In function `ZN2cv3MataSERKS0_':
C:/opencv/build/include/opencv2/core/mat.inl.hpp:613: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
obj\Debug\main.o: In function `ZN2cv3Mat7releaseEv':
C:/opencv/build/include/opencv2/core/mat.inl.hpp:704: undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
10 error(s), 0 warning(s) (0 minute(s), 0 second(s))
It's so confused. Could someone help me out?

You are attempting to link libraries, e.g.
C:\opencv\build\x64\vc14\lib\opencv_world320.lib
that have been built with the MS Visual Studio 2014 C++ compiler in a program
that you are building with a different C++ compiler, namely mingw32-g++.
That won't work. Libraries built by one C++ compiler are in general
binary incompatible with programs or libraries built with a different
one and in particular, code built with an MS C++ compiler is binary
incompatible with code built by a GCC compiler (which you are using).
In addition, it seems that you are building a 32-bit program
with mingw32-g++ - since the 32-bit TDM GCC compiler is the bundled
default compiler in Code::Blocks 16.01 - and are attempting to link
64-bit libraries with it. Even if the libraries were not made by
an incompatible compiler, you cannot link 64-bit code with 32-bit
code.
All the libraries that you link in your 32[64]-bit program must
have been built with the same compiler or at least a binary ABI compatible
compiler, and they must also be 32[64]-bit.
Start again and build OpenCV from source, for targeting mingw, 32-bit,
using your mingw32 compiler. Get the windows source in the zip archive Source code
from the GitHub OpenCV repo.
It seems as if you may already have built from source. If so, then you
built the libraries for 64-bit VC++14, not mingw32, and those libraries
are useless.
To build the libraries again and configure an OpenCV project in Code::Blocks,
follow this tutorial,
carefully.

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

Include 3rd party library in VS code in C++ project

I am trying to include a 3rd party library in my first C++ project.
Here is the file structure:
Here is the code:
If I try to compile it, I get the following error:
Starting build... /usr/bin/g++ -g /home/?/projects/c++/test.cpp -o /home/?/projects/c++/test
/usr/bin/ld: /tmp/cci5qRjP.o: in function `main': /home/?/projects/c++/test.cpp:7: undefined reference to `omp::HandEvaluator::HandEvaluator()'
/usr/bin/ld: /tmp/cci5qRjP.o: in function `omp::Hand::Hand(unsigned int)': /home/?/projects/c++/include/OMPEval/omp/Hand.h:49: undefined reference to `omp::Hand::CARDS'
/usr/bin/ld: /tmp/cci5qRjP.o: in function `omp::Hand::empty()': /home/?/projects/c++/include/OMPEval/omp/Hand.h:114: undefined reference to `omp::Hand::EMPTY'
/usr/bin/ld: /tmp/cci5qRjP.o: in function `omp::HandEvaluator::perfHash(unsigned int)': /home/?/projects/c++/include/OMPEval/omp/HandEvaluator.h:39: undefined reference to `omp::HandEvaluator::PERF_HASH_ROW_OFFSETS'
/usr/bin/ld: /tmp/cci5qRjP.o: in function `unsigned short omp::HandEvaluator::evaluate<true>(omp::Hand const&) const': /home/?/projects/c++/include/OMPEval/omp/HandEvaluator.h:27: undefined reference to `omp::HandEvaluator::LOOKUP'
/usr/bin/ld: /home/?/projects/c++/include/OMPEval/omp/HandEvaluator.h:31: undefined reference to `omp::HandEvaluator::FLUSH_LOOKUP'
collect2: error: ld returned 1 exit status
Build finished with error(s). The terminal process failed to launch (exit code: -1).
Terminal will be reused by tasks, press any key to close it.
The code is taken for the documentation of the library.
c_cpp_properties.json
launch.json
tasks.json
Wherever that tasks.json came from, it is not adequate for a complex project containing multiple source files. It compiles and tries to link only a single .cpp file; this is doomed to failure when there are multiple .cpp files that are supposed to work together.
You have a file named Makefile in your project directory... updating tasks.json to run make and let make run the compiler and linker would be a good start... but you should also expect to modify the Makefile each time you add new files to your project.

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.

Error when moving from OS X to linux Red-Hat

I had written my program using XCode. It works without any problems on the Mac. I was told that our projects would be compiled on Linux machines so I wanted to make sure it works on Linux before submitting. When I tried to compile on Linux, it gave me some reference errors:
/tmp/cckwoehj.o: In function `main':
main.cpp:(.text+0x9): undefined reference to `ReadFile()'
/tmp/cckwoehj.o: In function `leftSearch(NODE*, int)':
main.cpp:(.text+0x38b): undefined reference to `conflict(int**)'
main.cpp:(.text+0x3ca): undefined reference to `removeAllRowsWithLiteral(int**, int)'
main.cpp:(.text+0x3ec): undefined reference to `removeAllSpecifiedLiteral(int**, int)'
main.cpp:(.text+0x4ec): undefined reference to `conflict(int**)'
main.cpp:(.text+0x522): undefined reference to `unitPropagation(int**)'
main.cpp:(.text+0x538): undefined reference to `conflict(int**)'
/tmp/cckwoehj.o: In function `rightSearch(NODE*, int)':
main.cpp:(.text+0x992): undefined reference to `conflict(int**)'
main.cpp:(.text+0x9d4): undefined reference to `removeAllRowsWithLiteral(int**, int)'
main.cpp:(.text+0x9f3): undefined reference to `removeAllSpecifiedLiteral(int**, int)'
main.cpp:(.text+0xaf3): undefined reference to `conflict(int**)'
main.cpp:(.text+0xb29): undefined reference to `unitPropagation(int**)'
main.cpp:(.text+0xb3f): undefined reference to `conflict(int**)'
collect2: ld returned 1 exit status
The stuff I found online all talks about templates. I don't use templates. I also found one about a 32 and 64 bit OS problem which I do not have.
I have a function called ReadFile that is declared in a header. I call it from main and include that header file in main. I am not sure what the problem is. If OS X compiles and runs and Linux doesn't, I am assuming there is something OS X does internally that it thinks is obvious but Linux is not written that way. Is this a linker error? I think OS X would be programmed to do that internally.
You're declaring your functions in a header, but there's no definition, leading to undefined references when you try to turn main.cpp into an executable.
You need to compile all your source files and link them together (in the right order).
g++ -c ReadFile.cpp
#...
g++ -c main.cpp
g++ main.o ... ReadFile.o -o my_executable
Where the object files to the right rely on no unresolved symbols defined in the object files to its left on the commandline.
XCode is an IDE so I guess it handles the linking order for you.
To handle all this stuff automatically you'll have to also use an IDE and/or write a Makefile.
If you insist in a single command, yes, you can, as long as you include all source files in the line (e.g. g++ *.cpp), but this forces a full recompilation for every single change.

c++ and mongodb - can't compile - undefined reference to `boost::system::generic_category()

I am trying to use mongodb for the first time in c++. I just installed the latest version on Ubuntu and also installed the lastest v2.0 c++ driver code. It compiled just fine using scons. In the c++ file the below is my include.
#include <client/dbclient.h>
So..I am assuming I have to make a ref to a boost library but I don't know how to do it.
make all
Building target: rtb
Invoking: GCC C++ Linker
g++ -L/usr/local/include/ -L/home/boost -L/home/cpp/mongo-cxx-driver-v2.0/mongo -lfcgi++ -o"rtb" ./src/rtb.o
./src/rtb.o: In function `__static_initialization_and_destruction_0':
/home/boost/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/home/boost/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/home/boost/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [rtb] Error 1
Add -lboost_system to your link line.