I'm a student from germany and for my bachelor thesis I am required to run a programm and later do something with the output it gives me. However, the programm is written by using openCV and I have a lot of trouble configuring openCV on my PC. I already followed a lot of tutorials and I have openCV 2.4.11 installed on my PC correctly (I think).
Now I tried to test my setup using the following code from one of the tutorials:
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv ) {
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data ) {
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
I properly told eclipse where my include files and lib files are and which of them to include. I don't get any errors in the code whatsovever.
But when I try and build my project, it won't work. Instead, I get an endless list of errors that lead to being unable to build it.
The errors read like (That's like 0.1 % of them):
templmatch.cpp:(.text._ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1158): Nicht definierter Verweis auf `cv::dft(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
templmatch.cpp:(.text._ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1c14): Nicht definierter Verweis auf `cv::dft(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
templmatch.cpp:(.text._ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1cbd): Nicht definierter Verweis auf `cv::mulSpectrums(cv::_InputArray const&, cv::_InputArray const&, cv::_OutputArray const&, int, bool)'
templmatch.cpp:(.text._ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1cfa): Nicht definierter Verweis auf `cv::dft(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
collect2: error: ld returned 1 exit status
make: *** [DisplayImage2] Error 1
16:04:37 Build Finished (took 660ms)
I googled the "Nicht definierter Verweis" part and found out that it is the german version of "undefined reference". However the solutions offered there did not solve my problem. I had the same problem in windows as well as on a different computer before but people told me it was Window's fault and that I should use ubuntu and all my problems would be gone.
I'm sorry if this was answered before and I just missed it. However I'd really appreciate any help :)
Sorry, not enough reputation to comment, so answering:
Your undefined references are referencing functions (like dft) that are not being called in your code. You might be actually building something different than what you think.
In case it's helpful, here's a skeleton CMakeLists.txt file which will compile a opencv c++ file:
cmake_minimum_required(VERSION 2.8)
PROJECT(yourFileName)
FIND_PACKAGE(OpenCV REQUIRED)
ADD_EXECUTABLE(${PROJECT_NAME} yourfile.cxx)
TARGET_LINK_LIBRARIES(
${PROJECT_NAME}
${OpenCV_LIBS})
ADD_DEFINITIONS(-std=c++11)
If you run this directly through cmake either gui or command then it should work and then at least it will run so you can work out eclipse later..
Related
I'm making a jump from python to C++ in vscode. My project utilizes OpenCV and I quickly ran into a lot of problems with trying to get my compiler (mingw-w64) and CMake to find and reference the OpenCV functions.
My test code looks like this. I know I have to change my file path, but when I do I get the same problem.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat image1,image2;
// Read the file
image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg");
// Check for invalid input
if(! image1.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//Write the File
imwrite( "C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",image1);
// Read the Writen File
image2 =imread("C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg");
namedWindow("Image1");
imshow("Image1",image1);
namedWindow("Image2");
imshow("Image2",image2);
waitKey(0);
}
My CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.0.0)
project(chessbot VERSION 0.1.0)
include(CTest)
enable_testing()
find_package( OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(chessbot main.cpp)
target_link_libraries(chessbot ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
When I use the CMake Build tool (which I believe is supposed to make my executable for me) I get a bunch of undefined references to opencv functions
uild] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:25: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:28: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:29: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:31: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:32: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::waitKey(int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:13: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:25: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:10: undefined reference to `cv::Mat::~Mat()'
[build] CMakeFiles\chessbot.dir/objects.a(main.cpp.obj):C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:10: more undefined references to `cv::Mat::~Mat()' follow
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\chessbot.dir\build.make:115: chessbot.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:838: CMakeFiles/chessbot.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2
There is another compile tool that doesn't return undefined references and seems to compile just fine, which leads me to believe that CMake cant find the target library for some reason. The learning curve is steep for me. Can someone explain to me the issue? Why cant I reference the OpenCV functions?
I can't seem to get the c++ bindings for the HDF5 library to compile when using gcc.
The test program is the example found at:
https://support.hdfgroup.org/ftp/HDF5/current/src/unpacked/c++/examples/h5tutr_crtdat.cpp
My cmake file reads as follows:
cmake_minimum_required(VERSION 3.1.0)
project(readhdf5 C CXX)
find_package(HDF5 COMPONENTS C CXX HL REQUIRED)
link_directories(${HDF5_INCLUDE_DIRS})
include_directories(${HDF5_INCLUDE_DIRS})
add_executable(readdata ../src/main.cpp)
target_link_libraries(readdata ${HDF5_LIBRARIES})
target_link_libraries(readdata ${HDF5_CXX_LIBRARIES})
Which I am using with the command:
cmake -DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8 ..
The error I recieve is:
me#comp:~/Downloads/hdf5-cmake-example-master/build$ make
[ 50%] Linking CXX executable readdata
Undefined symbols for architecture x86_64:
"H5::H5File::H5File(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)", referenced from:
_main in main.cpp.o
"H5::H5Location::createDataSet(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, H5::DataType const&, H5::DataSpace const&, H5::DSetCreatPropList const&) const", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [readdata] Error 1
make[1]: *** [CMakeFiles/readdata.dir/all] Error 2
make: *** [all] Error 2
I'm having difficulty working out the cause of the error.
If I do not specify the compiler it defaults to clang/clang++ which compiles fine.
If I use only the C bindings then it compiles fine.
If I specify the compilers as h5cc and h5c++ wrappers, then it compiles fine.
I'm not sure why the gcc/g++ compiler can't find the source code where clang/clang++ can.
I'm using macOS High Sierra.
HDF5 was installed using Homebrew.
Thanks very much for your suggestions.
I understand what an undefined symbol is and how they're usually fixed. However the HDF5 library doesn't seem to follow the typical pattern of how an external library is linked. The question is not about how to fix undefined symbols in general but why they are not found only when using gcc in this case.
I suspect the answer requires specific knowledge of this library rather than general c++.
I encountered very similar problems with HDF5 and C++, which ultimately where related to this ABI issue, which also affects clang (when it cannot be fixed via resorting to an older ABI). If your problem is caused by the same issue, read on.
Ultimately, I solved this by avoiding std::string in the interface to the HDF5 library. This implies
not using any HDF5 functions taking/returning std::string
not using any HDF5 structures using std::string
For most cases you can simply use the version of the HDF5 routines taking const char* and pass string::c_str(). However, there are two more difficult cases.
HDF5 exceptions are based on std::string, so you cannot use them, i.e. catch other than via catch(...)
Reading a std::string via HDF5. To this end I actually re-implemented the corresponding routine (stolen from the HDF5 source code) to compile it with the correct ABI for std::string. Here it is:
//
// reading a std::string, see H5DataSet.cpp
//
std::string read_string(H5::DataSet const&data_set,
H5::DataType const&mem_type,
H5::DataSpace const&mem_space,
H5::DataSpace const&file_space,
H5::DSetMemXferPropList const&xfer_plist
= H5::DSetMemXferPropList::DEFAULT)
{
const auto is_variable_len = H5Tis_variable_str(mem_type.getId());
if(is_variable_len<0)
throw std::runtime_error("read_string: H5Tis_variable_str failed");
std::string strg;
if(!is_variable_len) {
const auto data_size = data_set.getInMemDataSize();
if(data_size>0) {
std::unique_ptr<char[]> strg_C{new char[data_size+1]};
std::memset(strg_C.get(), 0, data_size+1);
if(0>H5Dread(data_set.getId(), mem_type.getId(), mem_space.getId(),
file_space.getId(), xfer_plist.getId(), strg_C.get()))
throw std::runtime_error("read_string: H5Dread failed for fixed length string");
strg = strg_C.get();
}
} else {
char*strg_C;
if(0>H5Dread(data_set.getId(), mem_type.getId(), mem_space.getId(),
file_space.getId(), xfer_plist.getId(), &strg_C))
throw std::runtime_error("read_string: H5Dread failed for variable length string");
strg = strg_C;
std::free(strg_C);
}
return strg;
}
I want to use opencv sfm module.I installed sfm module with all dependencies.
I can use all opencv methods without reconstruct method.I have a problem with this method.I have linker error i searched on internet.But i couldn't solve the problem.
Link : compiling code with OpenCV Libraries
I found this and a lot of topic but i dont know how can i solve this problem
This is my cmake file:
project(x)
cmake_minimum_required(VERSION 3.6)
find_package(OpenCV REQUIRED)
include_directories(/usr/include/eigen3)
include_directories(/usr/local/include/opencv2/sfm)
set(OpenCV_LIBS -L. -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lopencv_highgui -lopencv_video -lopencv_viz -lopencv_calib3d -lopencv_sfm -lpthread)
add_executable(x main.cpp)
target_link_libraries(x ${OpenCV_LIBS})
And this is error :
undefined reference to `cv::sfm::reconstruct(cv::_InputArray const&, cv::_OutputArray const&, cv::_OutputArray const&, cv::_InputOutputArray const&, cv::_OutputArray const&, bool)'
collect2: error: ld returned 1 exit status
Can anyone help me?
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.
I've just followed the instructions to install opencv 3 on my ubuntu computer (I've already installed and ran programs using opencv 3 on a different linux device), however, when I tried to run one of my test programs from my old computer I get the following errors:
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `main':
VideoTest.cpp:(.text+0x6f): undefined reference to `cv::VideoWriter::fourcc(char,char,char, char)'
VideoTest.cpp:(.text+0xc3): undefined reference to `cv::VideoWriter::open(cv::String const&, int, double, cv::Size_<int>, bool)'
VideoTest.cpp:(.text+0x103): undefined reference to `cv::namedWindow(cv::String const&, int)'
VideoTest.cpp:(.text+0x146): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
VideoTest.cpp:(.text+0x1b1): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::String(char const*)':
VideoTest.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x3b): undefined reference to `cv::String::allocate(unsigned int)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::~String()':
VideoTest.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0xd): undefined reference to `cv::String::deallocate()'
collect2: ld returned 1 exit status
make[2]: *** [VideoTest] Error 1
make[1]: *** [CMakeFiles/VideoTest.dir/all] Error 2
make: *** [all] Error 2
Any idea what's going on? I'm relatively new to opencv. Here's my test code for reference:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
VideoCapture vCap(0);
VideoWriter vWrite;
vWrite.open("test.avi", vWrite.fourcc('M','J','P','G'), 20, Size(640, 480), true);
while (1) {
namedWindow("VideoFeed", WINDOW_AUTOSIZE);
Mat frame;
vCap.read(frame);
vWrite.write(frame);
imshow("VideoFeed", frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
return 0;
}
Thanks in advance.
Edit: My CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(VideoTest)
find_package(OpenCV REQUIRED)
add_executable(VideoTest VideoTest.cpp)
target_link_libraries(VideoTest ${OpenCV_LIBS})
VideoCapture and VideoWriter were moved to the videoio module in 3.0, so you have to additionally
#include "opencv2/videoio.hpp"
also, you don't seem to link to any of the required opencv libs, those are:
opencv_core, opencv_videoio, opencv_highgui
Alright, I've finally figured out what's going on. Something internally in the CMakeFiles folder went screwy when I moved the directory over to my new system. Removing all files/directories besides the project .cpp file and the CMakeLists.txt file, and running "cmake ." and "make" again seems to solve the problem.
Below cmake file works for me.
cmake_minimum_required(VERSION 3.5)
project(ImageProcessing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(SOURCE_FILES main.cpp)
add_executable(ImageProcessing ${SOURCE_FILES})
target_link_libraries( ImageProcessing ${OpenCV_LIBS} )
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")