Make opencv use system libpng #6424

Closed
wants to merge 2 commits into from
@eraserhd

This one should be correct. It prevents OpenCV from building the included libpng or using the included PNG headers so that linking with other libraries (FLTK in my case) does not produce conflicting header/library versions for libpng.

Verified both 1) loading a PNG with FLTK's routine and 2) loading a PNG with OpenCV's routine in a test app, and both now work.

eraserhd added some commits Jul 12, 2011
@eraserhd eraserhd Use system -lpng
Otherwise OpenCV programs can't be used with other libraries which
use the system -lpng.
bb18f54
@eraserhd eraserhd Make OpenCV use system png.h 3dec081
@nickoneill

Any idea how to make this work with the current opencv Formula? The first line is easy enough to add in, but the second doesn't work ("No such file or directory - 3rdparty/include/png.h") presumably because the Formula has been changed for the 2.3.1a update.

With just the first addition I still get this error:

Undefined symbols for architecture x86_64:
  "_png_set_gray_1_2_4_to_8", referenced from:
      cv::PngDecoder::readData(cv::Mat&)     in grfmt_png.o

I expect this is because I'm not removing the other png dependency and it's linking against that one rather than the good one in x11, but that's speculation. I'm not real familiar with brew or cmake.

@Sharpie

I installed OpenCV on Lion and then hit the lib directory with otool -L *.dylib | grep png afterwards and everything was pointing to the system LibPNG in /usr/x11/lib.

So, I don't know if this is a valid issue anymore... could someone confirm?

@nickoneill

I get that error on Lion without this patch as well, so it's still an issue for me. Freshly installed lion system (not a snow leopard upgrade).

@Sharpie

What does the following give you:

otool -L `brew --prefix opencv`/lib/*.dylib | grep png
@nickoneill

opencv isn't installed, so I get can't open file: /usr/local/Cellar/opencv/2.3.1a/lib/*.dylib (No such file or directory)

@Sharpie

@nickoneill

Ok. Well I need some other people to install it and run that command---or point out where exactly OpenCV is linking to a LibPNG other than the one in the X11 SDK. Because everything looks fine on my Lion box so this could be a FLTK issue and not an OpenCV issue.

@nickoneill

It doesn't seem like a brew issue. This is the line where it fails:

#if PNG_LIBPNG_VER_MAJOR*100 + PNG_LIBPNG_VER_MINOR >= 104
png_set_expand_gray_1_2_4_to_8( png_ptr );
#else
png_set_gray_1_2_4_to_8( png_ptr );
#endif

Which is obviously switching between two functions for old/new versions of libpng. Apparently it's not detecting my version correctly (or at all) because making both functions png_set_expand_gray... builds great. I'll file an issue with opencv.

@cleanzero

I'm just starting to use homebrew and while attempting to install opencv I get

Linking CXX shared library ../../lib/libopencv_highgui.dylib
Undefined symbols for architecture x86_64:
"_png_set_gray_1_2_4_to_8", referenced from:
cv::PngDecoder::readData(cv::Mat&) in grfmt_png.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[2]: *** [lib/libopencv_highgui.2.3.1.dylib] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

How can I fix this ? Thanks.

@saskathex

I just moved from macports to homebrew. Both are upgraded lions from snow leopards. On the first one installation went without any problems. Second one tells me:

/tmp/homebrew-opencv-2.3.1a-WMBB/OpenCV-2.3.1/modules/highgui/src/grfmt_png.cpp:57:17: error: png.h: No such file or directory
/tmp/homebrew-opencv-2.3.1a-WMBB/OpenCV-2.3.1/modules/highgui/src/grfmt_png.cpp: In member function ‘void cv::PngDecoder::close()’:

The png headers cannot be found.

The patches above do not match to the current formula. Hope I am getting the to me new homebrew stuff correct.

----------------------- 3 hours later ----------------------

Changing the formula to use the following and it works! Important lines are the lines from 41 to 48.

def install
ENV.x11

ENV.append "CFLAGS", "-I/usr/X11/include" # env.x11 does not add this line
# ENV.append "CPPFLAGS", "-I/usr/X11/include"
ENV.append "CXXFLAGS", "-I/usr/X11/include" # env.x11 does not add this line

args = std_cmake_parameters.split
args << " -DOPENCV_EXTRA_C_FLAGS='-arch i386 -m32'" if ARGV.include? "--with-x --build32"
@stonemirror

I had a similar experience. Replace the line

def install

with

def install
ENV.x11

ENV.append "CFLAGS", "-I/usr/X11/include" # env.x11 does not add this line
# ENV.append "CPPFLAGS", "-I/usr/X11/include"
ENV.append "CXXFLAGS", "-I/usr/X11/include" # env.x11 does not add this line

args = std_cmake_parameters.split
args << " -DOPENCV_EXTRA_C_FLAGS='-arch i386 -m32'" if ARGV.include? "--with-x --build32"

got it building correctly (it seems) for me as well...

@cleanzero

Thanks a lot, worked like a charm on my system too

@sneilan

Awww yeah!!

@adamv

Note, perhaps ENV.x11 ought to update the Include paths too.

@jacknagel

Having to append -I/dir lines (which belong in CPPFLAGS) to {C,CXX}FLAGS is something that upstream should fix because they are misusing compiler flags.

@Sharpie

@jacknagel

Agreed. ENV.x11 appends the proper include flags to CPPFLAGS---also adding them to CFLAGS and CXXFLAGS would be a misuse of those environment variables.

@eraserhd

@nickoneill After a lot of trial and error, the solution to your problem (and mine) is to do the following:

$ sudo mv /Library/Frameworks/Mono.framework /tmp/
$ brew install opencv
$ sudo mv /tmp/Mono.framework /Library/Frameworks/

'brew doctor' indicates that Mono.framework might be the problem. (I never NEVER look at that until I've been hacking on a problem for a couple hours. sigh)

Also, recent versions do not use the bundled libpng, so I'm closing this pull request as no longer necessary.

@eraserhd eraserhd closed this Dec 26, 2011
@bryevdv

I still can't build openCV, same error message, even after a brew update:

[ 43%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/grfmt_tiff.o
Linking CXX shared library ../../lib/libopencv_highgui.dylib
ld: warning: duplicate dylib /usr/lib/libz.dylib
Undefined symbols:
"_png_set_gray_1_2_4_to_8", referenced from:
cv::PngDecoder::readData(cv::Mat&) in grfmt_png.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [lib/libopencv_highgui.2.3.1.dylib] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

I don't have a Mono Framework installed. brew doctor returns:

bryan@Laptop-3 /usr/local/Cellar/cmake $ brew doctor
Some "config" scripts were found in your path, but not in system or Homebrew folders.

./configure scripts often look for *-config scripts to determine if software packages
are installed, and what additional flags to use when compiling and linking.

Having additional scripts in your path can confuse software installed via Homebrew if
the config script overrides a system or Homebrew provided script of the same name.

/Library/Frameworks/Python.framework/Versions/Current/bin
python-config python2.7-config wx-config xml2-config xslt-config

Enthought Python was found in your PATH.

This can cause build problems, as this software installs its own
copies of iconv and libxml2 into folders that are picked up by
other build systems.

I tried some of the suggestions above, but to no avail, other suggestions would be appreciated

@eraserhd

@bryevdv This looks like the same problem I had. If you run 'brew doctor', it should warn you that Mono.framework exists. I had to do the following:

$ mv /Library/Frameworks/Mono.framework /tmp
$ brew install opencv
$ mv /tmp/Mono.framework /Library/Frameworks/

And everything worked.

@aseembehl

I am still getting the following error when installing OpenCV 2.3.1 on OSX 10.7 with Xcode 3.2.6

Linking CXX shared library ../../lib/libopencv_highgui.dylib
ld: warning: duplicate dylib /usr/lib/libz.dylib
Undefined symbols:
"_png_set_gray_1_2_4_to_8", referenced from:
cv::PngDecoder::readData(cv::Mat&) in grfmt_png.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [lib/libopencv_highgui.2.3.1.dylib] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2

Infact, I get the same error if I try to compile openCV from source without using homebrew. Any idea what is wrong?

I tried adding the following lines to the openCV formula:

ENV.x11

ENV.append "CFLAGS", "-I/usr/X11/include" # env.x11 does not add this line
# ENV.append "CPPFLAGS", "-I/usr/X11/include"
ENV.append "CXXFLAGS", "-I/usr/X11/include" # env.x11 does not add this line

args = std_cmake_parameters.split
args << " -DOPENCV_EXTRA_C_FLAGS='-arch i386 -m32'" if ARGV.include? "--with-x --build32" ```
@bryevdv

eraserhd: As I mentioned in my comment, I don't have any Mono framework installed, so that solution did not work for me.

I did finally get a 32-bit install working. To accomplish this, I installed a non-system lippng into /usr/local, obtained from here:

http://ethan.tira-thompson.com/Mac_OS_X_Ports.html

I gather the homebrew folks have some strong feelings about only using system libraries, OTOH I have work to do and this allowed me to do it.

@hsjunnesson

Thread necromancy, I know. But I wanted to chime in anyways.
libpng removed png_set_gray_1_2_4_to_8() in version 1.4.0. It was replaced with png_set_expand_gray_1_2_4_to_8().

Old code will probably reference that function even though it was deprecated since a while.

@xu-cheng xu-cheng locked and limited conversation to collaborators Feb 16, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.