2

There is a error when compiling opencv program program simple includes #include <opencv2/core.hpp>

I compiled it with this command

 g++ test.cpp -o app `pkg-config --cflags --libs opencv`

compiling opencv in c++

This is full error

gcc -I/usr/local/lib test.cpp 
test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
    1 | #include <opencv2/core.hpp>

I compiled and install the opencv by looking at this page https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-opencv-from-source-using-cmake-using-the-command-line

The code is from https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html

Also my opencv so files are located at /usr/local/lib

Error also says

Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable

but I searched /usr directory there is no opencv.pc file

Update

Compiling program after updating my compile command This error throws

 $g++ -I/usr/local/include/opencv4/ test.cpp 
/usr/bin/ld: /tmp/ccuJvWgF.o: in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
$ 

Update 2

when now compiling

g++ -L/usr/local/lib/libopencv_core.so -I/usr/local/include/opencv4/ test.cpp 

it throws this error. There are many opencv so files in /usr/local/lib do I need to include specific opencv so files to compile the code in the link

 in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit sta
halfer
  • 19,824
  • 17
  • 99
  • 186
user786
  • 3,902
  • 4
  • 40
  • 72
  • Unless `opencv2/` is in your `usr/local/lib/` directory, you need to have an `-I` include path pointing to it. Could this be your issue? – meaning-matters Aug 03 '22 at 10:03
  • @meaning-matters I tried same error caused `g++ -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory 1 | #include | ^~~~~~~~~~~~~~~~~` – user786 Aug 03 '22 at 10:08
  • You say you compile with "g++ test.cpp -o app `pkg-config --cflags --libs opencv`" but then show the error output from "g++ -I/usr/local/lib test.cpp": can you clarify the precise commands you use to compile and link? Also, please edit your question to show the output from `pkg-config --cflags --libs opencv`. – G.M. Aug 03 '22 at 10:13
  • @user786 Is your `opencv2/` directory in `/usr/local/lib/`? – meaning-matters Aug 03 '22 at 10:15
  • @G.M. both these command complain about same header files. So same error. Did this clarify – user786 Aug 03 '22 at 10:24
  • @meaning-matters yes that's where all so files for opencv living – user786 Aug 03 '22 at 10:25
  • @user786 What does `ls /usr/local/lib/opencv2/core.hpp` say? – meaning-matters Aug 03 '22 at 10:28
  • 1
    @user786 It not the .so files that matter, it is the .hpp files. Normally they would be in `/usr/local/include` not `/usr/local/lib` – john Aug 03 '22 at 10:38
  • i@meaning-matters path for headers are -I/usr/local/include/opencv4/opencv2 – user786 Aug 03 '22 at 10:43
  • @john I have updated my question with updated compile command please check now functions and class are undefined – user786 Aug 03 '22 at 10:44
  • @user786 What you are missing now is the opencv libraries to link with (the .so files).. You need something like `-L/usr/local/lib -lopencv` but I'm not exactly sure, and it depends on exactly where you installed opencv. – john Aug 03 '22 at 10:49
  • It would have been better if you had installed opencv with a *package manager* instead of trying to do it yourself. That way your original command would have worked. – john Aug 03 '22 at 10:52
  • @user786 If that's where the OpenCV headers are, why didn't you have/show `-I/usr/local/include/opencv4/opencv2` in your commands? (I've downvoted because your question and comments lack clarity.) – meaning-matters Aug 03 '22 at 10:58
  • 1
    @meaning-matters ok now I have include -I and -L both please check my updated question. Thanks for help – user786 Aug 03 '22 at 11:20
  • @john my opencv so files all of them are in /usr/local/lib directory and I included both -L/usr/local/lib and -I/usr/local/include/opencv4/ with `g++ -L/usr/local/lib -I/usr/local/include/opencv4/ test.cpp` but now all opencv classes and functions calls are throwing exception undefined – user786 Aug 03 '22 at 11:30
  • maybe I have to include specific so file in -L all of them but not sure which so files – user786 Aug 03 '22 at 11:31

2 Answers2

1

With the -L option you should specify the library search path.

Then with -l options you specify the library names you'd like to link against.

So in your case I'd expect to see -L/usr/local/lib -lopencv_core. Note that the -l name has the lib prefix and file extension omitted. (You may need more OpenCV libraries.)

Seeing your struggles, I think it would be good to read a general tutorial about compiling and linking C/C++ programs (on your platform).

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
1

I had a similar problem when I moved some code from OpenCV2 to OpenCV4, it appears you are using some OpenCV4 as well. My fix was to not include opencv2/ but to include opencv4/opencv2/. Not exactly sure what about that made it work, I did that way too long ago, but it worked since.

ZwergofPhoenix
  • 141
  • 1
  • 6