OpenCV SFM Module Reconstruct Method Linking Error - c++

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?

Related

OpenCV SFM module has undefined references to other OpenCV modules

I have built OpenCV 4.5.5 from source on Ubuntu 20.04.4 LTS with the sfm contrib module and I am trying to use it for scene reconstruction. The libraries were built successfully, however, whenever I call cv::sfm::reconstruct, I get the following linker errors when tying to build my program:
/usr/bin/ld: /usr/local/lib/x86_64-linux-gnu/libopencv_sfm.so.4.5.5: undefined reference to `cv::FlannBasedMatcher::add(cv::_InputArray const&)'
/usr/bin/ld: /usr/local/lib/x86_64-linux-gnu/libopencv_sfm.so.4.5.5: undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&) const'
/usr/bin/ld: /usr/local/lib/x86_64-linux-gnu/libopencv_sfm.so.4.5.5: undefined reference to `cv::DescriptorMatcher::knnMatch(cv::_InputArray const&, std::vector<std::vector<cv::DMatch, std::allocator<cv::DMatch> >, std::allocator<std::vector<cv::DMatch, std::allocator<cv::DMatch> > > >&, int, cv::_InputArray const&, bool)'
/usr/bin/ld: /usr/local/lib/x86_64-linux-gnu/libopencv_sfm.so.4.5.5: undefined reference to `libmv::MotionFromEssential(Eigen::Matrix<double, 3, 3, 0, 3, 3> const&, std::__debug::vector<Eigen::Matrix<double, 3, 3, 0, 3, 3>, std::allocator<Eigen::Matrix<double, 3, 3, 0, 3, 3> > >*, std::__debug::vector<Eigen::Matrix<double, 3, 1, 0, 3, 1>, std::allocator<Eigen::Matrix<double, 3, 1, 0, 3, 1> > >*)'
/usr/bin/ld: /usr/local/lib/x86_64-linux-gnu/libopencv_sfm.so.4.5.5: undefined reference to `libmv::correspondence::nRobustViewMatching::computeCrossMatch(std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)'
collect2: error: ld returned 1 exit status
I have tried recompiling OpenCV with the sfm module and all its dependencies a number of different ways but nothing seems to solve this problem.
The issue seems to be that libopencv_sfm.so is being linked incorrectly somehow and can't find symbols from the other OpenCV modules. I have checked that the linker can find cv::FlannBasedMatcher::add when not called by libopencv_sfm.so and it works when called directly from my program. Running ldconfig on /usr/local/lib/x86_64-linux-gnu also didn't work.
Here's how I'm linking my code against OpenCV with CMake (note: not the top-level CMakeLists):
# Target name
project(sfmPointCloud)
# Setting variables
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
# Creating executable target
add_executable(
${PROJECT_NAME}
"${INCLUDE_DIR}/logger.h"
"${SRC_DIR}/main.cpp"
"${SRC_DIR}/cmdline_parser.cpp"
"${SRC_DIR}/sfmPointCloud.cpp"
)
# Setting target's properties
set_target_properties(
${PROJECT_NAME} PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD 20
)
# Instruct basic signature of `find_package` function to fall back to "Config"
# mode immediately instead of trying "Module" mode first since no libraries used
# provide a Find Module
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
# Setting up OpenCV
message(STATUS "Searching for OpenCV package")
find_package(
OpenCV 4.5.5 REQUIRED
COMPONENTS core imgcodecs imgproc features2d calib3d xfeatures2d sfm
)
# Setting include directories and linking libraries
target_include_directories(
${PROJECT_NAME}
PRIVATE ${INCLUDE_DIR} # internal header files
PRIVATE "/usr/local/include/opencv4" # opencv2/
)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

How to include a static library using cmake?

I want to add the library ixwebsocket my project. Following the instructions of the library I get the libixwebsocket.a (the compiled static library, correct me if I am wrong).
How I can use this .a file in my project?
For testing I have created the following program: main.cpp
#CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cpptest VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(cpptest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries(cpptest "${CMAKE_SOURCE_DIR}/libixwebsocket.a")
//main.cpp
#include <iostream>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
int main(int, char**) {
std::cout << "Hello, world!\n";
ix::WebSocket webSocket;
}
In the directory I have CMakeLists.txt, libixwebsocket.a and main.cpp.
I get a long list of undefined reference errors, the firsts ones:
[build] [ 50%] Linking CXX executable cpptest
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocket.cpp.o): in function `ix::WebSocket::start()':
[build] IXWebSocket.cpp:(.text+0x2240): undefined reference to `pthread_create'
ld] /usr/bin/ld: ../libixwebsocket.a(IXWebSocket.cpp.o): in function `ix::WebSocket::checkConnection(bool)':
[build] IXWebSocket.cpp:(.text+0x5b8d): undefined reference to `pthread_cond_clockwait'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `bool ix::WebSocketPerMessageDeflateCompressor::compressData<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) [clone .part.0]':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x67): undefined reference to `deflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::~WebSocketPerMessageDeflateCompressor()':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x35c): undefined reference to `deflateEnd'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::init(unsigned char, bool)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x3a6): undefined reference to `deflateInit2_'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::~WebSocketPerMessageDeflateDecompressor()':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x64c): undefined reference to `inflateEnd'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::init(unsigned char, bool)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x685): undefined reference to `inflateInit2_'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::decompress(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x7d6): undefined reference to `inflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `bool ix::WebSocketPerMessageDeflateCompressor::compressData<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<unsigned char, std::allocator<unsigned char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<unsigned char, std::allocator<unsigned char> >&) [clone .part.0]':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x90d): undefined reference to `deflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXSocketOpenSSL.cpp.o): in function `ix::SocketOpenSSL::openSSLInitialize()':
[build] IXSocketOpenSSL.cpp:(.text+0x1c): undefined reference to `OPENSSL_init_ssl'
[build] /usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x29): undefined reference to `OPENSSL_init_ssl'
How may these undefined refrence errors be resolved?
Thanks in advance!
It would be always best to use find_package() calls. This way you ensure that the library is indeed installed on your system, or is exported in the build tree.
If you have the library installed on your system you can use this call, like this:
find_package(ixwebsocket REQUIRED)
target_link_libraries(cpptest ixwebsocket::ixwebsocket)
In your case, since the library is compiled independently (and it is not a target in your tree), you need to use find_library(IXWEBSOCKET_LIB ixwebsocket) and specify additional hints/names if you have the library in your own location, following the docs.
I ran into a similar issue today, the library was linking just fine but a static lib is just a collection of .o files and has no understanding of it's own dependencies...
This command worked for me:
g++ -o output/myapp -std=c++17 myapp.cpp -Llib -framework CoreFoundation -framework Security -lmylib
I'm no expert in CMake but this answer seems to indicate that the below would achieve the same effect
target_link_libraries(program "-framework CoreFoundation" "-framework Security")
So if we apply it to your case:
#CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cpptest VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(cpptest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries(cpptest "${CMAKE_SOURCE_DIR}/libixwebsocket.a" "-framework CoreFoundation")
I've got a feeling pthread_create is likely to live in CoreFoundation but I'm just guessing... You may need to try a few frameworks to find the right one.
Hope this helps a little :)

OpenCV 3.0 undefined reference error?

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}")

Static linking OpenCV on Eclipse running on Windows

I am trying to static link OpenCV libraries on Windows. I already built the library with BUILD_SHARED_LIBS OFF and I created a project on the Eclipse (am using MinGW by the way). C:\opencv is set as my source while C:\opencv\build\x86\mingw is set as the destination on the cmake-gui. I then executed mingw32-make and mingw32-make install on C:\opencv\build\x86\mingw.
My question is, how do I static link those libraries on the Eclipse, I tried several ways but my OpenCV application still doesn't work on computers without OpenCV installed.
Here's what I tried so far, I set
C:\opencv\build\x86\mingw\install\include as the include folder;
C:\opencv\build\x86\mingw\lib as the library path on the "Linker" and I have opencv_core246, opencv_highgui246, opencv_objdetect246 and opencv_imgproc246.
This way it seems like it still uses "dynamic linking" so I tried adding -static on the Miscellaneous option but then I got these errors:
C:\opencv\build\x86\mingw\lib\libopencv_imgproc246.a(templmatch.cpp.obj):templmatch.cpp:
(.text$_ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1ce5):
undefined reference to `cv::dft(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
C:\opencv\build\x86\mingw\lib\libopencv_imgproc246.a(templmatch.cpp.obj):templmatch.cpp:
(.text$_ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1dab):
undefined reference to `cv::mulSpectrums(cv::_InputArray const&, cv::_InputArray const&, cv::_OutputArray const&, int, bool)'
C:\opencv\build\x86\mingw\lib\libopencv_imgproc246.a(templmatch.cpp.obj):templmatch.cpp:
(.text$_ZN2cv9crossCorrERKNS_3MatES2_RS0_NS_5Size_IiEEiNS_6Point_IiEEdi+0x1ded):
undefined reference to `cv::dft(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
collect2: ld returned 1 exit status
Anyone can provide me real step-by-step to static link OpenCV library on Windows using Eclipse and MinGW?
EDIT: I found out that in VS we have to add libraries such as: libtiff.lib libpng.lib libjpeg.lib libjasper.lib IlmImf.lib zlib.lib to the Linker which I did but the problem persists. Then I heard adding Vfw32.Lib comctl32.lib solved the problem but unfortunately I guess they are VS-specific.
Adding 3rd party libraries -llibjasper -llibtiff -lopencv_lapack -lzlib -lpng -ljpeg -lpthread -lrt should solve the issue.
Try compiling a very basic OpenCV code. The order of the libraries that are linked is important while linking static libraries.
If libraryA is depend on libraryB, then libraryA must be defined before defining libraryB.

opencv undefined reference for CvSVM::predict(CvMat * const)

I have encountered a strange problem about OpenCV 2.4.6.1.
The code is from http://www.opencv.jp/sample/svm.html.
I have link the project with
-lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_legacy -lopencv_nonfree -lopencv_optim
But it still has errors as follows:
undefined reference to `CvSVM::predict(CvMat const*) const'
collect2: ld returned 1 exit status
I have test with OpenCV 2.4.0 and OpenCV 2.4.5, both with these errors.
Could someone give me some advice, please? Thanks.