undefined reference to cv::ml with opencv_ml and opencv_objdetect - opencv

When developing a program, I tried to use opencv to open a CSV file. And I wrote
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
// ...
cv::Ptr<cv::ml::TrainData> mlData = cv::ml::TrainData::loadFromCSV("train.csv", 1);
And My CMakeLists.txt, according to OpenCV doc, is
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIBRARY_DIRS})
message(STATUS "${OpenCV_LIBS}")
add_executable(Test ${EXAMPLE_DIR}/test.cpp)
target_link_libraries(Test ${OpenCV_LIBS} ${PROJECT_NAME})
But I got a undefined Error:
undefined reference to `cv::ml::TrainData::loadFromCSV(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, char)'
I know it was because some opencv libs weren't linked. However, Print-Info In CMakeLists.txt told me that:
-- opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio
It seems that I linked all the libs I could but I still got an undefined error.

Ok, after serval times compiling and carefully consideration, I finally found what's wrong with my program.
The big problem is the link order. I just put ${OpenCV_LIBS} before ${PROJECT_NAME}. When I wrote:
target_link_libraries(Test ${PROJECT_NAME} ${OpenCV_LIBS})
target_link_libraries(Train ${PROJECT_NAME} ${OpenCV_LIBS})
Then All is well.

Related

Caffe build fails repeatedly

I am trying to compile the software OpenPose on an Ubuntu 14 machine. The installation script installs Caffe. While installing Caffe, I get the following error message:
CXX tools/upgrade_net_proto_text.cpp
CXX/LD -o .build_release/tools/upgrade_net_proto_text.bin
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/upgrade_net_proto_text.bin] Error 1
From here, I have tried running ldconfig, I use OpenCV 3.2.0, so I have uncommented that in the makefile. I have also tried updating the OpenCV path in ~/.bashrc but I am not sure if I did it correctly. I did:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
After each of these steps, I ran the install script again but it fails with the same error message (shown above) every time.
I haven't seen anyone so far who successfully installed caffe with openCV 3.2.0,i faced the same problem and what i did was to downgrade to OpenCV 3.1.Another problem that came along was missing modules that are in open_contrib that you should install along opencv,i d refer you to this answer also related to OpenPose here

How to compile OpenCV in HEVC with Cmake?

I am modifying HEVC opensource project downloaded from bitbucket.
I have added OpenCV libraries now for some frame processing.
Code works fine with visual studio.
Problem is when I try to make it with cmake following error is producing:
libx265.so.102: undefined reference to `cv::dilate(cv::_InputArray const&, cv::_OutputArray const&, cv::_InputArray const&, cv::Point_<int>, int, int, cv::Scalar_<double> const&)'

OpenCV SFM Xcode 7.2: Undefined symbols for architecture x86_64: cv::sfm::reconstruct

I have linked to all the appropriate libraries and headers in my Xcode project (where I have the samples files from OpenCV for scene_reconstruction), after building and installing OpenCV with the contrib module.
I have also applied the #define CERES_FOUND true fix that is recommended here:
https://github.com/Itseez/opencv_contrib/issues/476
The editor prompts me with the correct functions completions from the sfm module and the reconstruct.hpp file (except that the prompts I get have the last boolean is_projective flag missing from all reconstruct()'s i.e.:
one of the prompts is:
reconstruct(<#const std::vector<std::string> images#>, <#OutputArray Rs#>, <#OutputArray Ts#>, <#InputOutputArray K#>, <#OutputArray points3d#>)
I however get:
Undefined symbols for architecture x86_64:
"cv::sfm::reconstruct(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, cv::_OutputArray const&, cv::_OutputArray const&, cv::_InputOutputArray const&, cv::_OutputArray const&, bool)", referenced from:
_main in scene_reconstruction.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
when I build the project.
I did a camera calibration in the same project and it ran fine, and everything else is alright, I have been using OpenCV built from source for years now, so I've taken care of the basics.
How do I proceed to fix/probe the issue further?
It was an issue with CERES_FOUND not being set because of a version mismatch between eigen and ceres.

Sample Program From Terminal Could Not Run

I'm new to opencv in ubuntu. I've follow the tutorial from here for setting up the opencv in linux. However, I have failed to make the sample program runs. After I type the following,
$ g++ DisplayImage.cpp
These error appears,
/tmp/cc3GTOtQ.o: In function `main':
DisplayImage.cpp:(.text+0x53): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text+0xe5): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text+0x113): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text+0x147): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
DisplayImage.cpp:(.text+0x169): undefined reference to `cv::waitKey(int)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::~Mat()':
DisplayImage.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::operator=(cv::Mat const&)':
DisplayImage.cpp:(.text._ZN2cv3MataSERKS0_[cv::Mat::operator=(cv::Mat const&)]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::release()':
DisplayImage.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
Here is the full command line build C++ program with OpenCV Libraries.
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binaryName main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy
If you installed OpenCV in different location, you should change the path of 'include' and lib 'directory' to appropriate location.
Edit:-
Instead of running this command every time just create a file build.sh in your project directory, change it's permission to executable by running chmod 777 build.sh and for building the project just run this file.
You need to link with the opencv libraries: probably you'll need to add at least -lcv and maybe -lcxcore and -lhighgui to your command line. See this similar question.

compilation error when using boost framework and opencv in ios project

I need to use boost framework in my iOS project. My first mission was getting boost to work for Ios. I tried using this script
http://goodliffe.blogspot.co.uk/2010/09/building-boost-framework-for-ios-iphone.html
but wasn't able to build due to lipo errors (?) and couldn't figure out how to solve it. I looked for a precompiled framework and came accross this project on github:
https://github.com/danomatika/ofxLua
There is an Ios version of boost included in the lib section. So I tried using it in my project
but now when I build my project I am getting these errors:
Undefined symbols for architecture i386:
"vtable for boost::filesystem::detail::utf8_codecvt_facet", referenced from:
(anonymous namespace)::path_locale() in boost(path.o)
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"_inflate", referenced from:
MatlabIO::uncompressVariable(unsigned int&, unsigned int&, unsigned int&, std::vector<char, std::allocator<char> > const&) in MatlabIO.o
"_inflateEnd", referenced from:
MatlabIO::uncompressVariable(unsigned int&, unsigned int&, unsigned int&, std::vector<char, std::allocator<char> > const&) in MatlabIO.o
"_inflateInit_", referenced from:
MatlabIO::uncompressVariable(unsigned int&, unsigned int&, unsigned int&, std::vector<char, std::allocator<char> > const&) in MatlabIO.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I also want to make clear that I installed boost using
sudo port install boost
and under my xcode project I am not linking to /usr/local/include/boost/ or something like that. It seems that it is compiling with incorrect architecture.
Any solution for this? Maybe the framework isn't compiled for Ios? Or is my project not referencing the correct version
You can use the BOOST 1.57.0 with ofxiOSBoost static libraries and headers:
https://github.com/danoli3/ofxiOSBoost
Releases for libc++ (c++11), libstdc++.
All build scripts are in the /scripts folder (updated Peter Goodliffe script)

Resources