Rust and opencv

Hi, have you used opencv rust package? I have tried to install it many ways. I have installed clang llvm and every possible library that any of docs or forums asked to install. But still I can't run even the "opencv Hello world".
All the terminal checks say, that everything is Ok.

`pkg-config --modversion opencv4
4.5.4`
`dpkg -l libopencv-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version             Architecture Description
+++-==============-===================-============-===========================>
ii  libopencv-dev  4.5.4+dfsg-9ubuntu4 amd64        development files for opencv
(base)`

I am using Cursor or VSCode, and the rust-analyzer is getting crazy with opencv used:
On cargo run:

error: failed to run custom build command for opencv v0.92.0

I have set some env variables there(in .bashrc):
`

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
export CXXFLAGS="-std=c++11 -isystem /usr/include/c++/11"`

but it still doesn't work

Maybe that's, because of missing configuration. However, there are many missing files and directories, that I have to direct.

`  === Environment configuration:
  ===   OPENCV_PACKAGE_NAME = None
  ===   OPENCV_PKGCONFIG_NAME = None
  ===   OPENCV_CMAKE_NAME = None
  ===   OPENCV_CMAKE_BIN = None
  ===   OPENCV_VCPKG_NAME = None
  ===   OPENCV_LINK_LIBS = None
  ===   OPENCV_LINK_PATHS = None
  ===   OPENCV_INCLUDE_PATHS = None
  ===   OPENCV_DISABLE_PROBES = None
  ===   OPENCV_MSVC_CRT = None
  ===   CMAKE_PREFIX_PATH = None
  ===   OpenCV_DIR = None
  ===   PKG_CONFIG_PATH = Some("/usr/lib/pkgconfig")
  ===   VCPKG_ROOT = None
  ===   VCPKGRS_DYNAMIC = None
  ===   VCPKGRS_TRIPLET = None
  ===   OCVRS_DOCS_GENERATE_DIR = None
  ===   DOCS_RS = None
  ===   PATH = Some("/home/wiktor/.yarn/bin:/home/wiktor/.config/yarn/global/node_modules/.bin:/usr/local/cuda-12.2/bin:/home/wiktor/anaconda3/bin:/home/wiktor/.local/bin:/home/wiktor/anaconda3/bin:/home/wiktor/anaconda3/condabin:/home/wiktor/.yarn/bin:/home/wiktor/.config/yarn/global/node_modules/.bin:/usr/local/cuda-12.2/bin:/home/wiktor/anaconda3/bin:/tmp/.mount_cursorzXIVlA:/tmp/.mount_cursorzXIVlA/usr/sbin:/home/wiktor/.cargo/bin:/home/wiktor/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/wiktor/n/bin")`

I have also tried to install it from the source. Based on that: How to Install OpenCV on Ubuntu | phoenixNAP KB
After make -j3 at some moment I get this:

`[ 38%] Linking CXX executable ../../bin/opencv_test_videoio
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_hash_table_get_keys_as_ptr_array'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_timeout_add_seconds_once'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_string_free_and_steal'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_hash_table_steal_all_values'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_ptr_array_sort_values'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_hash_table_get_values_as_ptr_array'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_hash_table_steal_all_keys'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_assertion_message_cmpint'
collect2: error: ld returned 1 exit status
make[2]: *** [modules/videoio/CMakeFiles/opencv_perf_videoio.dir/build.make:160: bin/opencv_perf_videoio] Error 1
make[1]: *** [CMakeFiles/Makefile2:8456: modules/videoio/CMakeFiles/opencv_perf_videoio.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 38%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_affine2d_estimator.cpp.o
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_hash_table_get_keys_as_ptr_array'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_timeout_add_seconds_once'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_string_free_and_steal'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2.0.so.0: undefined reference to `g_hash_table_steal_all_values'
/usr/bin/ld: /home/wiktor/anaconda3/lib/libgio-2

Btw. It works perfectly fine with Python, so I don't know, what's the problem.

That's a linker error, so setting CXXFLAGS won't help. Those functions are from glib, so you need to link against glib.

There is a problem.
Some guys tend to say that changing LD_LIBRARY_PATH would fix the problem.
However I cannot do this, because I have CUDA library set with that:

echo $LD_LIBRARY_PATH
/usr/local/cuda-12.2/lib64

I have also set /etc/ld.so.conf with:

include /etc/ld.so.conf.d/*.conf
include /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0

, but it doesn't work.
However, back to this error. I see that it points to:
anaconda3/lib/libgio-2.0.so.0,
but I see that there is also glib
/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
But I'm not sure what is the name of the env which points to this anaconda

*_PATH environment variables can contain arbitrarily many paths by convention. You separate them with a colon (:).

However, that wouldn't help here. LD_LIBRARY_PATH is for the dynamic loader, not the linker.

You have to add the -lglib flag to the linker invocation to link against glib. In general, to link against libfoo.so, you add the flag -lfoo.

Sorry what? That directory is the system default for libraries. It has nothing to do with anaconda.


In general, you would benefit a lot from learning the basics of compiling C or C++ code on Linux and exploring the fundamentals first (eg. what are the important system directories on a Unix-like system, what is the linker vs. the loader, etc.), before trying to dive into a complex topic such as FFI between two languages. If you can't even distinguish between the linker and the loader, you are extremely unlikely to be able to successfully write, compile, and deploy software that uses CUDA, Rust, and FFI at the same time.

I wrote a very complex Cuda code in Julia and in C++, so I'm very efficient in that, however, I don't know what common this has to run OpenCV on Rust, especially since running OpenCV with Python works perfectly fine.
Do you want to self-esteem or something?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.