0

I'm a beginner programmer so may have missed something obvious here. I'm following this tutorial to set up opencv on my Mac: OpenCV Linux Install (Yes it's the linux version, but it's supposed to work with Mac) I've followed all directions, and everything worked correctly. ( I did CMake, make, and sudo make install with no errors)

However, when I compile, I get errors saying the compiler cannot find the header files. Example:

fatal error: ' opencv2\opencv.hpp ' file not found
#include < opencv2\opencv.hpp > 

I'm guessing I did not yet link my installation or maybe "set the path"? However, that was not in the directions. Any ideas?

GPPK
  • 6,546
  • 4
  • 32
  • 57
s123
  • 461
  • 1
  • 4
  • 18
  • Let me understand: what fails compiling is your own project using some OpenCV functions, correct? What kind of project is yours? Basically what is happening is that the opencv libraries are not in your default include path (See http://stackoverflow.com/questions/4980819/gcc-include-directories), so you have to specify it explicitly. Normally you will solve this by adding to the compiler line `-I`, but the solution depends on the kind of project you have: if you have a CMake project you have a special command called `include_directories` – Antonio May 03 '15 at 19:54
  • Thanks, Yes, what fails is compiling my own project using OpenCV functions. It's actually a sample project I found from OpenCV website. I tried compiling like so: g++ -I /Users/user/Desktop/opencv2/include main.cpp and I got the same error I mentioned earlier. Inside my include directory there is cmake_install.cmake CMakeFiles and Makefile. How do I compile with "include_directories"? – s123 May 03 '15 at 20:10
  • Still same error. It's a capital i. g++ -I/Users/user/Desktop/opencvbuild/include main.cpp main.cpp:3:10: fatal error: 'opencv2/imgcodecs.hpp' file not found #include "opencv2/imgcodecs.hpp" ^ 1 error generated. – s123 May 03 '15 at 20:16
  • OK, I'm going to download source, make and make install again so I make sure I have everything right then check the path. Will post again in 30 mins. – s123 May 03 '15 at 20:21
  • I now compile like so, and get a linker error instead. I cut out a lot of the text because it was too long to post. g++ -I/usr/local/include/ main.cpp Undefined symbols for architecture x86_64: "cv::Mat::deallocate()", referenced from: cv::Mat::release() in main-937b25.o "cv::Mat::copySize(cv::Mat const&)", referenced from: .... ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – s123 May 03 '15 at 21:06
  • How would that look like? Could you show the whole text that I should write in the terminal? (Like "g++ -l/usr/.....) – s123 May 03 '15 at 21:58
  • g++ -l/usr/local/include/ main.cpp <-- that is a lowercase L. Get the same error though: main.cpp:3:10: fatal error: 'opencv2/imgcodecs.hpp' file not found #include "opencv2/imgcodecs.hpp" – s123 May 03 '15 at 22:02
  • g++ -I-l-L/usr/local/include/opencv main.cpp doesn't work either. – s123 May 03 '15 at 22:06
  • Thanks for your time so far. I don't know where the library file would be. My include folder is here. Maybe you could see? https://drive.google.com/folderview?id=0B7NGOO5M53gKfkg4UjZfNUJlV2ZzZXhEbHEwZE53cm0wSnpkU3M0YUE5Q21lbjZJTmxaSFk&usp=sharing – s123 May 03 '15 at 22:24
  • I opened it. I don't know which of the files would be the library. Maybe you could have a look? https://drive.google.com/file/d/0B7NGOO5M53gKbXlBdDhPRzRxR0U/view?usp=sharing – s123 May 03 '15 at 22:40
  • I just tried this, but got this error. : g++ -I/usr/local/include/opencv -l -L main.cpp -bash: opencv_core: No such file or directory Also tried this but got error g++ -I/usr/local/include/opencv -l -L main.cpp -bash: opencv_core.3.0.0: No such file or directory – s123 May 03 '15 at 22:55
  • I am learning a lot, thanks for your generosity. Now I'm back to the previous error: g++ -I/usr/local/include/opencv -lopencv_core -L/usr/local/lib/ main.cpp main.cpp:2:10: fatal error: 'opencv2\opencv.hpp' file not found #include – s123 May 03 '15 at 23:03
  • Still same error. g++ -I/usr/local/include -lopencv_core -L/usr/local/lib/ main.cpp main.cpp:2:10: fatal error: 'opencv2\opencv.hpp' file not found #include – s123 May 03 '15 at 23:07
  • Well when I did "make install" again earlier before I decided to check " install_manifest.txt file", there was a compilation error. I ignored it since I thought it would be OK since it compiled ok earlier. I'll do another make install now, hopefully no errors this time then do the g++ -I.... again – s123 May 03 '15 at 23:12
  • I got an error earlier because I didn't do "sudo make install" and did "make install" instead. I now compile like so, but still get a linker error: g++ -I/usr/local/include/ main.cpp Undefined symbols for architecture x86_64: "cv::Mat::deallocate()", referenced from: cv::Mat::release() in main-93b4c7.o "cv::Mat::copySize(cv::Mat const&)", referenced from: cv::Mat::Mat(cv::Mat const&) in main-93b4c7.o ... ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – s123 May 03 '15 at 23:19
  • Same linker error: g++ -I/usr/local/include -lopencv_core -lopencv_imgproc -L/usr/local/lib/ main.cpp Undefined symbols for architecture x86_64: "cv::imread(cv::String const&, int)", referenced from: parseCmdArgs(int, char**) in main-200554.o ...ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – s123 May 03 '15 at 23:28
  • Short linker error this time: g++ -I/usr/local/include -lopencv_core -lopencv_imgproc -lhighgui -L/usr/local/lib/ main.cpp ld: library not found for -lhighgui clang: error: linker command failed with exit code 1 (use -v to see invocation) – s123 May 03 '15 at 23:32
  • g++ -I/usr/local/include -lopencv_core -lopencv_imgproc -lopencv_highgui -L/usr/local/lib/ main.cpp Undefined symbols for architecture x86_64: "cv::imread(cv::String const&, int)", referenced from: parseCmdArgs(int, char**) in main-884a9f.o "cv::imwrite(cv::String const&, cv::_InputArray const&, std::__1::vector > const&)", referenced from: ... _main in main-884a9f.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – s123 May 03 '15 at 23:38
  • 1
    For sure, never had a stranger spend so much effort helping me out. Thanks a lot. – s123 May 03 '15 at 23:41
  • 1
    When you ask the new question, show the compiler command line and the relevant part of the main.cpp code. Maybe link to this question, and take care of cleaning a bit the comments here above. Good luck! (Now gotta sleep :) ) – Antonio May 03 '15 at 23:45
  • Please also specify which OpenCV version you are dealing with (2.X or 3.X) – Antonio May 04 '15 at 09:31
  • I stopped trying to get OpenCV to work on the terminal altogether, and very easily got it to work with Xcode after following these directions: https://www.youtube.com/watch?v=XJeP1juuHHY The link assumes that sudo make install was already done. Thanks again for all the help, I learned a bunch of things from that. – s123 May 05 '15 at 20:59
  • Glad I could help, it was my pleasure! – Antonio May 05 '15 at 21:55

1 Answers1

1

After some trial and error iterations, it looks your compiler line should be:

g++ -I/usr/local/include -lopencv_core -lopencv_imgproc -lopencv_highgui -L/usr/local/lib/ main.cpp main.cpp

Namely you should check your install_manifest.txt (resume of your opencv installation command, available in the opencv build directory) and add the options:

  • -I<path to the include directory>
  • -l<name of the library file to be linked, removing "lib" prefix and any extension> (repeat for as many libraries you need to link to)
  • -L<path to the library directory>

For your system, the library files can be distinguished as having extension .dylib (and prefix lib). In Windows the extension would be .dll and for Unix .so (for dynamic libraries, while static libraries have extension .lib and .a respectively)

Antonio
  • 19,451
  • 13
  • 99
  • 197