undefined reference to `cv::imwrite(...)' - c++

I want to compile doppia, which has OpenCV as a dependency. Currently I am using OpenCV 2.4.9.1, which is the default on Ubuntu 16.04. Everything works fine besides the references to the function imwrite.
...
[100%] Linking CXX executable objects_detection
undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/objects_detection.dir/build.make:103: recipe for target 'objects_detection' failed
make[2]: *** [objects_detection] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/objects_detection.dir/all' failed
make[1]: *** [CMakeFiles/objects_detection.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
The file that throws the error is IntegralChannelsDetector.cpp.
In it, both opencv2/imgproc/imgproc.hpp and opencv2/highgui/highgui.hpp are included.
Running pkg-config opencv --cflags --libs also returns that all modules are linked correctly.
Researching about my error I learnt that if I was using OpenCV 3.x I'd have to include the img_codecs module, but that is not the case for OpenCV 2.x. I fear I'm stuck on 2.x for compatibility reasons.
Is there something left I could try to get the linking process completed?

Related

Linkage problem when building program with libtorch on Raspberry PI

I'm writing a C++ program using libtorch and OpenCV.
Here is the output of my CMakeLists.txt, with the libraries versions :
-- Pytorch status :
-- libraries: torch;torch_library;/usr/lib/libc10.so
-- OpenCV library status :
-- version: 3.2.0
-- libraries: opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- include path: /usr/include;/usr/include/opencv
-- Configuring done
-- Generating done
-- Build files have been written to: /home/<my_user>/<project>/cmake-build
You can see that torch needs libc10.so to work.
When I call make, everything goes fine, until this happens during linking :
[ 8%] Linking CXX executable POC_V4
/usr/lib/libc10.so: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/POC_V4.dir/build.make:298: POC_V4] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/POC_V4.dir/all] Error 2
make: *** [Makefile:104: all] Error 2
When I do a file command on this file, I see that it is compiled for x86-64 architectures, instead of ARM.
Here is the output of the file command on /usr/lib/libc10.so and on /usr/lib/libtorch.so.3.0.0 to have a comparison:
/usr/lib/libc10.so: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=f8019a46a003bc238af0fb3fb8331af8af69aa33, not stripped
/usr/lib/libtorch.so.3.0.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=2f8320292f01aab7c9e2e2577b3be2025689c0a7, stripped
How can I have a working libc10.so file ?
EDIT:
I started to re-build PyTorch on my raspberry PI so it would re-build libtorch too.
This takes many hours, and crashed during the night for a linkage problem :
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_compare_exchange_8'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/conv_to_nnpack_transform_test.dir/build.make:108: bin/conv_to_nnpack_transform_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:2836: caffe2/CMakeFiles/conv_to_nnpack_transform_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_compare_exchange_8'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/depthwise3x3_conv_op_test.dir/build.make:108: bin/depthwise3x3_conv_op_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:2867: caffe2/CMakeFiles/depthwise3x3_conv_op_test.dir/all] Error 2
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_compare_exchange_8'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/pattern_net_transform_test.dir/build.make:108: bin/pattern_net_transform_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:2805: caffe2/CMakeFiles/pattern_net_transform_test.dir/all] Error 2
make: *** [Makefile:161: all] Error 2
However, the build outputed a libc10.so file with the following signature!
/usr/lib/libc10.so: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=61d77cfaa9647739e17f6b08caeb53382d9e9108, not stripped
I replaced it in the /usr/lib folder and tried to build my project : It went further than before, and crashed for another file-format problem. This proves that if I manage to fully rebuild libtorch, I should be able to build my project !
Can someone help me solve the new linking problem ?
EDIT #2:
I found this issue with the same problem. Trying the given solution and coming back here if that works to write a full tutorial.
I have found a link overcome the issue you are facing. Please read it through it should solve your solve.
https://github.com/pytorch/pytorch/issues/14727
I finally made it work and detailed it in the answer of this question.
Don't hesitate to improve it if you find errors / bad practices !

CMake Linker Flags

I have a C++ code divided into 2 files image_desc_try.cpp, common.cpp and common.h. This code involves usage of Nvidia CUDA and NVidia NVInfer.
I could compile the entire code using the following with success.
g++ -std=c++11 image_desc_try.cpp common.cpp -I /usr/local/cuda/include -L /usr/local/cuda/lib64 -lcudart -lnvinfer -lnvparsers
Now, I am trying to do the same thing with CMake,
include_directories( "/usr/local/cuda/include" )
add_executable( desc_server
common.cpp
image_desc_try.cpp
)
target_link_libraries( desc_server
${catkin_LIBRARIES}
/usr/lib/aarch64-linux-gnu/libnvinfer.so
/usr/lib/aarch64-linux-gnu/libnvparsers.so
/usr/local/cuda-10.0/targets/aarch64-linux/lib/libcudart.so
)
This is giving me linking errors:
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `getPluginRegistry'
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `getInferLibVersion'
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `getLogger'
/usr/lib/gcc/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/libnvparsers.so: undefined reference to `nvinfer1::utils::reshapeWeights(nvinfer1::Weights const&, int const*, int const*, void*, int)'
collect2: error: ld returned 1 exit status
tx2_whole_image_desc_server/CMakeFiles/desc_server.dir/build.make:157: recipe for target '/home/dji/catkin_ws/devel/lib/tx2_whole_image_desc_server/desc_server' failed
make[2]: *** [/home/dji/catkin_ws/devel/lib/tx2_whole_image_desc_server/desc_server] Error 1
CMakeFiles/Makefile2:1578: recipe for target 'tx2_whole_image_desc_server/CMakeFiles/desc_server.dir/all' failed
make[1]: *** [tx2_whole_image_desc_server/CMakeFiles/desc_server.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
How to fix this?

Why does deleting these two lines lead to ld error "undefined reference"?

I have a project using opencv and cmake which builds 100% fine.
Then, I go to a C++ source file, which contains these lines, among other nonremarkable lines:
cv::imshow("Test", display);
cv::waitKey(0);
And I delete ONLY these two lines. Try building again, and I get:
$ make
(...)
[ 77%] Linking CXX executable project_test
libproject.so: undefined reference to `cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
libproject.so: undefined reference to `cv::imread(cv::String const&, int)'
libproject.so: undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
collect2: error: ld returned 1 exit status
CMakeFiles/project_test.dir/build.make:226: recipe for target 'project_test' failed
make[2]: *** [project_test] Error 1
CMakeFiles/Makefile2:105: recipe for target 'CMakeFiles/project_test.dir/all' failed
make[1]: *** [CMakeFiles/project_test.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2
Do you know of anything that would cause linking to fail on a small code deletion like that?
I tried building the original code from scratch and it worked, and I tried building the code with the two line deletion from scratch and it failed in the above way again.
EDIT:
Originally, the project's main CMakeLists.txt file contained this:
target_link_libraries(project opencv_core opencv_highgui)
Now I edited it to this:
target_link_libraries(project opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
And the project builds again. But why did it build in the first place without those libraries explicitly being linked? And why do those two lines work around that?

Normaliz undefined reference to `boost::system::system_category()'

I'm trying to install the library Normaliz having dependencies like CocoALib, SCIP. I've made its dependencies according to their guidances. However, I get the error while make of Normaliz on both Ubuntu and Mac OSX,
[ 81%] Linking CXX executable normaliz
libnormaliz/libnormaliz.so.3.4.0: undefined reference to `boost::system::system_category()'
libnormaliz/libnormaliz.so.3.4.0: undefined reference to `boost::system::generic_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/normaliz.dir/build.make:99: recipe for target 'normaliz' failed
make[2]: *** [normaliz] Error 1
CMakeFiles/Makefile2:99: recipe for target 'CMakeFiles/normaliz.dir/all' failed
make[1]: *** [CMakeFiles/normaliz.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
I really fed up with it since two weeks! How can it be overcome?
Even if it is boost linkage problem, how can it be linked? There is no option for it. That kinda headaches cause apathy to open-source at least for young programmers like me.

Error make all for OpenCV3 - ubuntu 16.04

I am trying to install OpenCV3 but I keep getting this error:
usr/bin/ld: /usr/local/lib/libpython3.5m.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libpython3.5m.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
modules/python3/CMakeFiles/opencv_python3.dir/build.make:224: recipe for target 'lib/python3/cv2.cpython-35m-x86_64-linux-gnu.so' failed
make[2]: *** [lib/python3/cv2.cpython-35m-x86_64-linux-gnu.so] Error 1
CMakeFiles/Makefile2:7226: recipe for target 'modules/python3/CMakeFiles/opencv_python3.dir/all' failed
make[1]: *** [modules/python3/CMakeFiles/opencv_python3.dir/all] Error 2
Any idea on how to solve it?