tags:

views:

119

answers:

1

Hi,

I tried installing OpenCV following the instructions for a MacPorts install on http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port, typing

sudo port install opencv

in the terminal.

The install/compilation seemed to go fine, and the files are in /opt/local subdirectories as they should be. As a first test, I then tried including highgui.h in a C++ source file:

#include <highgui.h>

but when compiling with g++ or gcc, I get error: highgui.h: No such file or directory. I'm new to developing on a Mac, so maybe I'm missing something? I thought I might have to set some path variable and after reading some posts I found when googling, I tried setting DYLD_LIBRARY_PATH=/opt/local/lib, but that was wild guess and it didn't seem to help. What should I do to make the compilers find the library?

Thanks!

+2  A: 

MacPorts installs C/C++ headers in /opt/local/include directory which is not the system default. It means that you have to explicitly tell GCC where to look for headers you are using. You can do that by specifying "-isystem" or "-I" command line options:

-isystem dir Search dir for header files, after all directories specified by -I but before the standard system directories. Mark it as a system directory, so that it gets the same special treatment as is applied to the standard system directories. If dir begins with "=", then the "=" will be replaced by the sysroot prefix; see --sysroot and -isysroot.


-Idir Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files (use -isystem for that). If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

If a standard system include directory, or a directory specified with -isystem, is also specified with -I, the -I option will be ignored. The directory will still be searched but as a system directory at its normal position in the system include chain. This is to ensure that GCC's procedure to fix buggy system headers and the ordering for the include_next directive are not inadvertently changed. If you really need to change the search order for system directories, use the -nostdinc and/or -isystem options.

I recommend using -isystem because it disables some warnings you cannot fix without modifying the code. For example, using std::auto_ptr if you compile your code with -std=c++0x etcetera.

The same goes for libraries. You have to tell GCC where to find them using -L option.

Vlad Lazarenko