0

I am having issues running this code:

    // CannyStill.cpp

    //#include "opencv2/core/core.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"

    #include<iostream>
    #include<conio.h>        

int main() {
  cv::Mat imgOriginal;        // input image
  cv::Mat imgGrayscale;       // grayscale of input image
  cv::Mat imgBlurred;         // intermediate blured image
  cv::Mat imgCanny;           // Canny edge image

  imgOriginal = cv::imread("image.png");          // open image

if (imgOriginal.empty()) {                                  // if unable to open image
    std::cout << "error: image not read from file\n\n";     // show error message on command line
    _getch();                                               // may have to modify this line if not using Windows
    return(0);                                              // and exit program
}

cv::cvtColor(imgOriginal, imgGrayscale, CV_BGR2GRAY);       // convert to grayscale

cv::GaussianBlur(imgGrayscale,          // input image
    imgBlurred,                         // output image
    cv::Size(5, 5),                     // smoothing window width and height in pixels
    1.5);                               // sigma value, determines how much the image will be blurred

cv::Canny(imgBlurred,           // input image
    imgCanny,                   // output image
    82,                         // low threshold
    164);                       // high threshold

                                // declare windows
cv::namedWindow("imgOriginal", CV_WINDOW_AUTOSIZE);     // note: you can use CV_WINDOW_NORMAL which allows resizing the window
cv::namedWindow("imgCanny", CV_WINDOW_AUTOSIZE);        // or CV_WINDOW_AUTOSIZE for a fixed size window matching the resolution of the image
                                                        // CV_WINDOW_AUTOSIZE is the default
cv::imshow("imgOriginal", imgOriginal);     // show windows
cv::imshow("imgCanny", imgCanny);

cv::waitKey(0);                 // hold windows open until user presses a key

return(0);

}

My issues seems to come from issues from the include statements. I can one or two of them and I have in all the includes I get:

C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x81): undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0xd4): undefined reference to `std::cout'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0xd9): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x142): undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x1e1): undefined reference to `cv::GaussianBlur(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x25a): undefined reference to `cv::Canny(cv::_InputArray const&, cv::_OutputArray const&, double, double, int, bool)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x295): undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x2c6): undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x30c): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x35c): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x37c): undefined reference to `cv::waitKey(int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x4e3): undefined reference to `std::ios_base::Init::~Init()'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text+0x504): undefined reference to `std::ios_base::Init::Init()'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x42): undefined reference to `cv::String::allocate(unsigned int)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf): undefined reference to `cv::String::deallocate()'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to `cv::fastFree(void*)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x40): undefined reference to `cv::Mat::deallocate()'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4): undefined reference to `cv::fastFree(void*)'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.eh_frame$_ZN2cv6StringD1Ev+0x13): undefined reference to `__gxx_personality_v0'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.eh_frame$_ZN2cv3MatD1Ev+0x13): undefined reference to `__gxx_personality_v0'
C:\Users\RYANDE~1\AppData\Local\Temp\cceJbRuT.o:CannyStill.cpp:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2.exe: error: ld returned 1 exit status

I have played around with commenting out some and including others and it just leaves an error that cv does not have some of the functions. Which from my understanding of opencv I need all of these libraries.

I am using OpenCV 3.4.1 that I built from the sources using Cmake for MinGW compiler which I am trying to use to compile this.

Any help would be appreciated and if you have any questions I will try to answer them to the best of my ability. Thanks.

Travis
  • 1
  • your include files are correct. What happens when you uncomment all 3 ? – Srini Mar 30 '18 at 18:43
  • This looks more like the linker lacking some libraries to me (the mention of [__gxx_personality](https://stackoverflow.com/questions/329059/what-is-gxx-personality-v0-for?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) in particular suggests libstdc++ should be linked but isn't). How are you compiling your code? – Caninonos Mar 30 '18 at 18:44
  • When I uncomment all three of the opencv includes I get an error for each cv function: 'cv' has not been declared. I am compiling the code in VS code which is just running it in a shell instance. cd "c:\Users\Ryan Deyhle\Documents\C++ Codes\Test\" && gcc -B C:\External\build\install\ CannyStill.cpp -o CannyStill && "c:\Users\Ryan Deyhle\Documents\C++ Codes\Test\"CannyStill – Travis Mar 30 '18 at 18:53
  • Try turning "gcc" into "g++" (that might solve the problem of libstdc++ not being linked), you also certainly need to add flags to tell the linker to link against opencv (something like "-lopencv-core -lopencv_highgui -lopencv_imgproc") – Caninonos Mar 30 '18 at 19:04
  • Btw, don't comment the include directives. If you do, you'll get errors before the linker even kicks in. The only reason you get something which might "look less scary" (things like "'identifier' has not been declared") is that those errors prevent the following steps of the compilation from occurring. – Caninonos Mar 30 '18 at 19:11
  • The -lopencv-core should be the path to the opencv-core.dll or .a file right? – Travis Mar 30 '18 at 19:20
  • No, at least on mac and linux versions of gcc (and I assume with mingw on windows but I might be wrong). However, you can add any number of "-L path/to/directory/containing/relevant/library/files" to tell the computer where those dll/a files are to be found (again, on mac/linux's gcc, I assume it's the same with mingw as it's basically a windows port of gcc). – Caninonos Mar 30 '18 at 19:24
  • Here is [a brief explanation](http://www.mingw.org/wiki/specify_the_libraries_for_the_linker_to_use) of how to specify the libraries to link against on mingw's website. (basically, that confirms what I said earlier) – Caninonos Mar 30 '18 at 19:26
  • Then I'm assume my version of opencv compiled wrong, it cannot find either the .dll or the .lib files for any of those . – Travis Mar 30 '18 at 19:41
  • It's a possibility, but if cmake didn't appear to return an error I'd find that surprising. I don't know if you ran any kind of install command after you compiled (and I unfortunately am unsure of where those files would end up on windows anyway), but you can always search the directory where you built opencv for a subdirectory containing the dll/a files (usually the directory is merely named "lib") then add this path with a -L flag. If you don't find any such directory, then yes, it's likely something went wrong. – Caninonos Mar 30 '18 at 19:50
  • I use the -B command to add an additional search directory (or that's what I think), to the preprocessor. I did run an install after I set up the cmake but maybe there was an issue I didn't know about. I'll just have to try it again. – Travis Mar 30 '18 at 19:56
  • According to https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#Directory-Options (again, I assume it's the same for mingw), -B does indeed translate into both -isystem (for headers) and -L (for libraries) flags. However, while it is stated that "include" is added to the -isystem path, it isn't stated that "lib" is added to the -L path (I find that slightly surprising, but maybe "lib" really is added, I usually use -I or -isystem and -L rather than -B so I don't know). You may want to try -L to make sure, otherwise I'm afraid I can't help you (good luck!) – Caninonos Mar 30 '18 at 20:14

0 Answers0