1

I'm trying to compile a C++ program which includes the libuvc.h header file and some more header files of opencv2.

The problem is, that I'm getting the following errors (undefinded references):

root@Raspi_DataHarvest:~/Schreibtisch# g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2

/tmp/ccvQZdkd.o: In function `UVCCameraHandler::start(int, CameraProperties, int)':
UVCCameraHandler.cpp:(.text+0x164): undefined reference to `uvc_init'
UVCCameraHandler.cpp:(.text+0x184): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x204): undefined reference to `uvc_get_device_descriptor'
UVCCameraHandler.cpp:(.text+0x334): undefined reference to `uvc_open'
UVCCameraHandler.cpp:(.text+0x354): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x3a8): undefined reference to `uvc_get_stream_ctrl_format_size'
UVCCameraHandler.cpp:(.text+0x3e8): undefined reference to `uvc_get_stream_ctrl_format_size'
UVCCameraHandler.cpp:(.text+0x40c): undefined reference to `uvc_print_diag'
UVCCameraHandler.cpp:(.text+0x424): undefined reference to `uvc_print_stream_ctrl'
UVCCameraHandler.cpp:(.text+0x43c): undefined reference to `uvc_perror'
UVCCameraHandler.cpp:(.text+0x46c): undefined reference to `uvc_stream_open_ctrl'
UVCCameraHandler.cpp:(.text+0x48c): undefined reference to `uvc_stream_start_iso'
UVCCameraHandler.cpp:(.text+0x4a4): undefined reference to `uvc_perror'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::grabFramesGrayscale(cv::Mat&, cv::Mat&)':
UVCCameraHandler.cpp:(.text+0x5c8): undefined reference to `uvc_stream_get_frame'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::grabFramesColor(cv::Mat&, cv::Mat&)':
UVCCameraHandler.cpp:(.text+0x864): undefined reference to `uvc_stream_get_frame'
UVCCameraHandler.cpp:(.text+0x92c): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::uvcFindDevice(uvc_context*, uvc_device**, int, int, char const*, int)':
UVCCameraHandler.cpp:(.text+0xae0): undefined reference to `uvc_get_device_list'
UVCCameraHandler.cpp:(.text+0xb10): undefined reference to `uvc_get_device_descriptor'
UVCCameraHandler.cpp:(.text+0xd0c): undefined reference to `uvc_get_device_descriptor'
/tmp/ccvQZdkd.o: In function `UVCCameraHandler::setProperties(CameraProperties)':
UVCCameraHandler.cpp:(.text+0xfb0): undefined reference to `uvc_get_gain'
UVCCameraHandler.cpp:(.text+0xff8): undefined reference to `uvc_set_gain'
UVCCameraHandler.cpp:(.text+0x103c): undefined reference to `uvc_set_ae_mode'
UVCCameraHandler.cpp:(.text+0x1050): undefined reference to `uvc_set_exposure_abs'
UVCCameraHandler.cpp:(.text+0x1068): undefined reference to `uvc_set_ae_mode'
/tmp/ccvQZdkd.o: In function `cv::String::String(char const*)':
UVCCameraHandler.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x60): undefined reference to `cv::String::allocate(unsigned int)'
/tmp/ccvQZdkd.o: In function `cv::String::~String()':
UVCCameraHandler.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::String::operator=(cv::String const&)':
UVCCameraHandler.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x30): undefined reference to `cv::String::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::Mat::Mat(cv::Size_<int>, int, void*, unsigned int)':
UVCCameraHandler.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvj[_ZN2cv3MatC5ENS_5Size_IiEEiPvj]+0x140): undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
UVCCameraHandler.cpp:(.text._ZN2cv3MatC2ENS_5Size_IiEEiPvj[_ZN2cv3MatC5ENS_5Size_IiEEiPvj]+0x244): undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
/tmp/ccvQZdkd.o: In function `cv::Mat::~Mat()':
UVCCameraHandler.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/tmp/ccvQZdkd.o: In function `cv::Mat::operator=(cv::Mat const&)':
UVCCameraHandler.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x140): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/ccvQZdkd.o: In function `cv::Mat::create(int, int, int)':
UVCCameraHandler.cpp:(.text._ZN2cv3Mat6createEiii[_ZN2cv3Mat6createEiii]+0xc4): undefined reference to `cv::Mat::create(int, int const*, int)'
/tmp/ccvQZdkd.o: In function `cv::Mat::release()':
UVCCameraHandler.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
/tmp/ccvQZdkd.o: In function `cv::Mat::operator()(cv::Rect_<int> const&) const':
UVCCameraHandler.cpp:(.text._ZNK2cv3MatclERKNS_5Rect_IiEE[_ZNK2cv3MatclERKNS_5Rect_IiEE]+0x24): undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)'
collect2: error: ld returned 1 exit status

So, this looks to me like g++ does not find the location of the header files, but they are installed in the correct directory!

root@Raspi_DataHarvest:~# find / | grep libuvc.h
/usr/local/include/libuvc/libuvc.h

root@Raspi_DataHarvest:~# find / | grep opencv2/opencv.hpp
/usr/local/include/opencv2/opencv.hpp

Furthermore, there's no effect using the absolute paths, like:

#include "/usr/local/include/libuvc.h"
...

Does anyone know how to solve the reference errors?

Michael Gierer
  • 401
  • 2
  • 6
  • 15
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) –  Jan 25 '18 at 14:05

3 Answers3

3

Try

g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -luvc -lopencv

The most preferable

g++ UVCCameraHandler.cpp `pkg-config --libs --cflags opencv` `pkg-config --libs --cflags lubuvc`
273K
  • 29,503
  • 10
  • 41
  • 64
  • Doesn't work either: /usr/bin/ld: cannot find -lopencv2 – Michael Gierer Jan 25 '18 at 14:05
  • yes, the solution wos the "-lopencv". So the third line worked. But now I've got another problem: error while loading shared libraries: libuvc.so.0: cannot open shared object file: No such file or directory – Michael Gierer Jan 25 '18 at 15:14
  • I'm using Raspberry Debian Jessie. The path to libuvc.so.0 is /usr/lib/arm-linux-gnueabihf/libuvc.so.0 Using LD_LIBRARY_PATH doesnt work, its set to /usr/local/lib:/usr/lib/arm-linux-gnueabihf – Michael Gierer Jan 25 '18 at 15:24
  • But the path is set correctly: root@Raspi_DataHarvest:~/Schreibtisch# ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012 SEARCH_DIR("=/usr/arm-linux-gnueabihf/lib") SEARCH_DIR("=/usr/local/lib/arm-linux-gnueabihf") SEARCH_DIR("=/usr/local/lib") SEARCH_DIR("=/lib/arm-linux-gnueabihf") SEARCH_DIR("=/lib") **SEARCH_DIR("=/usr/lib/arm-linux-gnueabihf")** SEARCH_DIR("=/usr/lib") – Michael Gierer Jan 25 '18 at 15:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163886/discussion-between-s-m-and-michael-gierer). – 273K Jan 25 '18 at 15:55
2

Building a C++ program is a three-step process:

  1. Editing the source
  2. Compiling the source files into object files
  3. Linking the object files into the final executable

Modern compilers often use front-end programs (like e.g. GCC with the g++ program) that combine steps 2 and 3 into a single action.

In step 2 above, the header files are included.

In step 3 you need to link with the actual libraries. You do not do this, so the linker will complain that it can't find the functions you use.

Linking with the libraries using GCC is done with the -l (lower-case L) option, listing the library name. Like for example -lopencv to link with the OpenCV library.

If the libraries are in a non-standard location (which /usr/local/lib usually is) then you need to tell the linker where to find the libraries as well. This is done with the -L option, similar to the -I option you already use to tell the compiler where to search for header-files. For example -L/usr/local/lib.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Good, full explanation – WillC Nov 11 '18 at 02:45
  • Just to re-phrase the command line from *luisp*'s answer for clarity: --- `g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -L/usr/local/lib -luvc -lopencv_core` --- is --- `g++ [source .cpp] -I[path to library's header directory; for the compiler] -L[path to pre-compiled library's directory; for the linker] -l[pre-compiled library's name; for the linker] ` – WillC Nov 11 '18 at 02:47
1

You need to link against the correct libraries (assuming your libs are in /usr/local/lib):

g++ UVCCameraHandler.cpp -I/usr/local/include/libuvc -I/usr/local/include/opencv2 -L/usr/local/lib -luvc -lopencv_core

luisp
  • 91
  • 5