Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tflan43

macrumors newbie
Original poster
Oct 15, 2011
3
0
hey
I'm trying to write a program that converts a data file to a png using the libpng. however i get an error every time I call a fcn from libpng. i have both png.h and zlib.h include. i have reinstalled libpng 1.5.5 a couple of times, once with fink and another from the libpng homepage. Here is the error that i get:

Code:
Undefined symbols for architecture x86_64:
  "_png_create_write_struct", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_create_info_struct", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_destroy_write_struct", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_set_longjmp_fn", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_init_io", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_set_IHDR", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_set_sBIT", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_set_text", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_write_info", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_error", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_write_image", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
  "_png_write_end", referenced from:
      jsa2png(char*, char*)in ccRwfl2Q.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I've tried using compiler option -L and -I to show the compiler where the library and includes are, but no luck.
 
hey
I'm trying to write a program that converts a data file to a png using the libpng. however i get an error every time I call a fcn from libpng. i have both png.h and zlib.h include. i have reinstalled libpng 1.5.5 a couple of times, once with fink and another from the libpng homepage.

You might consider using NSImage instead, which can write images in a large number of different file formats.
 
If you don't want to or can't use NSImage, then you'll want to know about the libpng-config program. It should have been installed where ever you installed libpng.

From Terminal, run this program to find what compiler options you need.
Code:
libpng-config --cflags --ldflags

On my system the output is:
Code:
-I/opt/local/include/libpng15
-L/opt/local/lib -lpng15

To compile a file called pngtest.c file into a program called pngtest that uses libpng, on my system I need at least the following gcc command.
Code:
gcc -I/opt/local/include/libpng15 -L/opt/local/lib -lpng15 -o pngtest pngtest.c

You can embed the libpng-config command directly into the gcc command if you want. But don't do this until you've confirmed that you're getting valid output from libpng-config.
Code:
gcc `libpng-config --cflags --ldflags` -o pngtest pngtest.c
 
The first question that comes to mind is, "Does the libpng you've installed contain code for x86_64?". The file command can tell you what's in a lib:
Code:
file /path/to/lib

The second question is, "Are you compiling C or C++ code? If C++, are you using 'extern C' properly?".

The third question is, "Exactly what -L and -I options are being used?". I.e. Post your actual command-line.
 
The first question that comes to mind is, "Does the libpng you've installed contain code for x86_64?". The file command can tell you what's in a lib:
Code:
file /path/to/lib

The second question is, "Are you compiling C or C++ code? If C++, are you using 'extern C' properly?".

The third question is, "Exactly what -L and -I options are being used?". I.e. Post your actual command-line.

i ran /path/to/lib

Code:
new-host-5:libpng15.framework Space$ file /sw/lib/libpng15.dylib 
/sw/lib/libpng15.dylib: Mach-O 64-bit dynamically linked shared library x86_64

also, i ran libpng-config --cflags --ldflags and got:
Code:
new-host-5:libpng15.framework Space$ libpng-config --cflags --ldflags
-I/usr/local/include/libpng15
-L/usr/local/lib -lpng15

when i compiled it like u said, i got errors saying it couldn't find iostream fcns like:
Code:
new-host-5:test_fcns Space$ gcc -I/usr/local/include/libpng15 -L/usr/local/lib -lpng15 testJSA2PNG.cc 
Undefined symbols for architecture x86_64:
  "std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream()", referenced from:
      getMagicNum(char*) in cct8ePft.o
      getDescription(char*) in cct8ePft.o
      getAuthor(char*)in cct8ePft.o
      getTitle(char*)in cct8ePft.o
      getAlphaDepth(char*) in cct8ePft.o
      getAlpha(char*)in cct8ePft.o
      getColorDepth(char*) in cct8ePft.o
      ...
  "std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)", referenced from:
      getMagicNum(char*) in cct8ePft.o
      getDescription(char*) in cct8ePft.o
      getAuthor(char*)in cct8ePft.o
      getTitle(char*)in cct8ePft.o
      getAlphaDepth(char*) in cct8ePft.o
      getAlpha(char*)in cct8ePft.o
      getColorDepth(char*) in cct8ePft.o

also, in the program I'm mixing c and c++. Does that make a difference? And, using the -L -I flags, does that added to or overwrite the C_INCLUDE_PATH etc?
 
If you're using C++, change gcc to g++.


Code:
new-host-5:libpng15.framework Space$ file /sw/lib/libpng15.dylib 
[COLOR=red]/sw/lib/[/COLOR]libpng15.dylib: Mach-O 64-bit dynamically linked shared library x86_64

also, i ran libpng-config --cflags --ldflags and got:
Code:
new-host-5:libpng15.framework Space$ libpng-config --cflags --ldflags
-I[COLOR=red]/usr/local/[/COLOR]include/libpng15
-L[COLOR=red]/usr/local/[/COLOR]lib -lpng15

Did you notice the discrepancy highlighted in red above? You have at least 2 copies of libpng. One in /sw/lib and one in /usr/local. You've just compiled and linked against the copy in /usr/local/.
 
If you're using C++, change gcc to g++.




Did you notice the discrepancy highlighted in red above? You have at least 2 copies of libpng. One in /sw/lib and one in /usr/local. You've just compiled and linked against the copy in /usr/local/.

my bad, dumb mistake. u were right with the compiler options, thanks
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.