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
16
  • 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? Commented Aug 3, 2022 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 <opencv2/core.hpp> | ^~~~~~~~~~~~~~~~~ Commented Aug 3, 2022 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. Commented Aug 3, 2022 at 10:13
  • 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 Commented Aug 3, 2022 at 10:38
  • 1
    @meaning-matters ok now I have include -I and -L both please check my updated question. Thanks for help Commented Aug 3, 2022 at 11:20

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I think u meant -lopencv_core to specify specific library. Also with -L/usr/local/lib can u please confirm
@user786 It's unclear to me what's unclear you and what you want me to confirm. Please be more specific.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.