Cannot understand this syntax in png.h

This is a discussion on Cannot understand this syntax in png.h within the C Programming forums, part of the General Programming Boards category; Hello, I'm having problems writing png files in c using libpng. When in my code I write: Code: png_ptr = ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    Cannot understand this syntax in png.h

    Hello,

    I'm having problems writing png files in c using libpng.
    When in my code I write:
    Code:
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    I'm told undefined reference to `png_create_write_struct'.

    I look into the png.h file and the first occurence 'png_create_write_struct' and find it in:

    /* Allocate and initialize png_ptr struct for writing, and any other memory */
    Code:
    extern PNG_EXPORT(png_structp,png_create_write_struct)
       PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
       png_error_ptr error_fn, png_error_ptr warn_fn));
    What is this code?? Is the function here PNG_EXPORT or PNGARG? I never saw such syntax and don't know how to read it.

    Maybe it's that I'm coding in ANSI C while that is some other C incompatible syntax?
    Last edited by simpatico_qa; 08-11-2009 at 08:53 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    You need to include the lib in which png_create_write_struct() is defined.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    itCbitC that I knew without you saying.
    According to this page: png_create_write_struct it should be png.h
    But as you can seen there the definition of the function is C code I understand.

    What do you say about the C code posted above, do you understand it?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > I'm told undefined reference to `png_create_write_struct'.
    This usually means you're not linking with the library properly.
    Is this the only one, or are there others?

    Check your library search paths, and name of library (which compiler/IDE are you using?)

    > What is this code?? Is the function here PNG_EXPORT or PNGARG?
    They're just two clever macros which expand to a function declaration in K&R C, and a prototype in ANSI-C
    I doubt you would need to worry about it if you're just interested in using the library.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    I actually get the problem with other functions of the same file but not for variables defined in the same.

    Code:
    png_byte color_type;
    png_byte bit_depth;
    
    png_structp png_ptr;
    png_infop info_ptr;
    
    png_bytep * row_pointers;
    All of these are recognized. I guess if there was a linking problem to the library they would also be undefined references, or not?

    I found in the distribution of pnglib the c file that defines the function. However including it should not be required since I've the libpng.LIB in the lib folder.

    I'm using Eclipse with MinGW.
    Last edited by simpatico_qa; 08-11-2009 at 09:37 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Yep, sounds like a link problem to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No, those could be defined in the header itself. In other words, you might have something like:
    Code:
    typedef char png_byte;
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    so what should i do compile the source code for the library on my own? As a static or shared library?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by simpatico_qa View Post
    I actually get the problem with other functions of the same file but not for variables defined in the same.

    Code:
    png_byte color_type;
    png_byte bit_depth;
    
    png_structp png_ptr;
    png_infop info_ptr;
    
    png_bytep * row_pointers;
    All of these are recognized. I guess if there was a linking problem to the library they would also be undefined references, or not?

    I found in the distribution of pnglib the c file that defines the function. However including it should not be required since I've the libpng.LIB in the lib folder.

    I'm using Eclipse with MinGW.
    You've found libpng.lib, which is good. Now you need to tell Eclipse that it exists. I've not used it, so I don't know exactly where to look, but you need to look for something like "linker options" and then "library paths"/"libraries to include"/etc.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    I've tried adding it to the library paths in eclipse, as well as -l libpng, libpng.a, libpng.lib, from the linker options, but in vain.
    I've also tried from the prompt:

    C:\Documents and Settings\Simpatico\My Documents\ws\Test>make -I C:\MinGW\libpn
    \include\libpng12 test test.c -l libpng
    gcc test.c -o test
    test.c:6:17: png.h: No such file or directory
    test.c:20: error: syntax error before "color_type"
    test.c:20: warning: data definition has no type or storage class
    test.c:21: error: syntax error before "bit_depth"
    test.c:21: warning: data definition has no type or storage class
    test.c:22: error: syntax error before "png_ptr"
    test.c:22: warning: data definition has no type or storage class
    test.c:23: error: syntax error before "info_ptr"
    test.c:23: warning: data definition has no type or storage class
    test.c:25: error: syntax error before '*' token
    test.c:25: warning: data definition has no type or storage class
    test.c: In function `write_png_file':
    test.c:36: error: `PNG_LIBPNG_VER_STRING' undeclared (first use in this functio
    )
    test.c:36: error: (Each undeclared identifier is reported only once
    test.c:36: error: for each function it appears in.)
    test.c:56: error: `width' undeclared (first use in this function)
    test.c:56: error: `height' undeclared (first use in this function)
    test.c:57: error: `PNG_INTERLACE_NONE' undeclared (first use in this function)
    test.c:58: error: `PNG_COMPRESSION_TYPE_BASE' undeclared (first use in this fun
    tion)
    test.c:58: error: `PNG_FILTER_TYPE_BASE' undeclared (first use in this function

    test.c:78: warning: passing arg 1 of `free' makes pointer from integer without
    cast
    make: *** [test] Error 1

    C:\Documents and Settings\Simpatico\My Documents\ws\Test>
    Please help me getting it to compile at least in the prompt.

    -----

    Solved it. I've specified in the linker the name libpng in the -l option and in the -L was specified the path.
    Last edited by simpatico_qa; 08-12-2009 at 04:59 AM. Reason: Solved it.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    although now eclipse generated the binaries, launching them they terminate before printing the test output (printf("test") . What else could be wrong here now? Launching the test.exe outside eclipse opens the prompt for .X seconds.
    If I comment the code that should write the png, it works.
    Last edited by simpatico_qa; 08-12-2009 at 05:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21