Home > Back-end >  Compiling opencv program cause gcc -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/cor
Compiling opencv program cause gcc -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/cor

Time:08-04

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

Thanks for any help

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
$ 

Ok I have updated again this question with his

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

CodePudding user response:

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

  • Related