Need help to compile LibPng into static lib

Everything else that doesn't fall into one of the other PB categories.
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Need help to compile LibPng into static lib

Post by Denis »

Hello all,

for 2 weeks now, i've tried to compile Libpng 1.6.37 using visual studio 2019 (http://www.libpng.org/pub/png/libpng.html).
I discovered visual studio and my knowledge of C is very close to zero.

So, after reading and translating libpng-manual to french (about 70%, Tks DeepL!), I found out that Libpng has several ways to be compiled.
I've readen tutos to set it up.

Finally I still have the same problem that I don't understand.
Compilation VS x86 multithreaded (all libpng, including Zlib 1.2.11)
With static compilation (.lib), i get no error with visual studio but using PB with the lib i get this :
POLINK: fatal error: Corrupt library:

G:\PureBasic\PureBasic_5_70_x86\PureLibraries\Windows\Libraries\libpng16.lib';
unable to find symbol '_Png_access_versio_number' in member '.\Release Library\png.obj\'
The only function i use is

Code: Select all

Png_access_versio_number()
(to start slowly with LibPng)

After reading a forum post from Fred, i decide to install visual studio 2013 and it's excactly the same.
I've compiled to get a dll (x86 with standard options) and it's OK. I put the .lib file to folder
..\PureBasic_5_70_x86\PureLibraries\Windows\Libraries
and put the dll into the same folder as PB code. And guess what ? It runs, as a result i get what i want :
10637
wich is the LibPng version (last one)

For my test, i've removed original LibPng.lib and Zlib.lib et replace them with visual studion result (libPng size > 1600 Kb for static one and for dll function libpng size is 56 Kb.

I certainly do something wrong... and my brain is foggy :oops:
(visual studio is not yet quite my friend...)

I hope the solution is not so far but what's wrong ?

May be I will be able to put configuration files here but i have to search wich files...

I use Lib2PBImport 2013.exe to import function and here is PB code

Code: Select all

; Libname: libpng16.lib
; created: 2019/11/13  16:09
ImportC "libpng16.lib"
      png_access_version_number()
      png_benign_error()
      png_build_grayscale_palette()
      png_calloc()
      png_chunk_benign_error()
      png_chunk_error()
      png_chunk_warning()
      png_convert_from_struct_tm()
      png_convert_from_time_t()
      png_convert_to_rfc1123()
      png_convert_to_rfc1123_buffer()
      png_create_info_struct()
      png_create_read_struct()
      png_create_read_struct_2()
      png_create_write_struct()
      png_create_write_struct_2()
      png_data_freer()
      png_destroy_info_struct()
      png_destroy_read_struct()
      png_destroy_write_struct()
      png_error()
      png_free()
      png_free_data()
      png_free_default()
      png_get_IHDR()
      png_get_PLTE()
      png_get_bKGD()
      png_get_bit_depth()
      png_get_cHRM()
      png_get_cHRM_XYZ()
      png_get_cHRM_XYZ_fixed()
      png_get_cHRM_fixed()
      png_get_channels()
      png_get_chunk_cache_max()
      png_get_chunk_malloc_max()
      png_get_color_type()
      png_get_compression_buffer_size()
      png_get_compression_type()
      png_get_copyright()
      png_get_current_pass_number()
      png_get_current_row_number()
      png_get_eXIf()
      png_get_eXIf_1()
      png_get_error_ptr()
      png_get_filter_type()
      png_get_gAMA()
      png_get_gAMA_fixed()
      png_get_hIST()
      png_get_header_ver()
      png_get_header_version()
      png_get_iCCP()
      png_get_image_height()
      png_get_image_width()
      png_get_int_32()
      png_get_interlace_type()
      png_get_io_chunk_type()
      png_get_io_ptr()
      png_get_io_state()
      png_get_libpng_ver()
      png_get_mem_ptr()
      png_get_oFFs()
      png_get_pCAL()
      png_get_pHYs()
      png_get_pHYs_dpi()
      png_get_palette_max()
      png_get_pixel_aspect_ratio()
      png_get_pixel_aspect_ratio_fixed()
      png_get_pixels_per_inch()
      png_get_pixels_per_meter()
      png_get_progressive_ptr()
      png_get_rgb_to_gray_status()
      png_get_rowbytes()
      png_get_rows()
      png_get_sBIT()
      png_get_sCAL()
      png_get_sCAL_fixed()
      png_get_sCAL_s()
      png_get_sPLT()
      png_get_sRGB()
      png_get_signature()
      png_get_tIME()
      png_get_tRNS()
      png_get_text()
      png_get_uint_16()
      png_get_uint_31()
      png_get_uint_32()
      png_get_unknown_chunks()
      png_get_user_chunk_ptr()
      png_get_user_height_max()
      png_get_user_transform_ptr()
      png_get_user_width_max()
      png_get_valid()
      png_get_x_offset_inches()
      png_get_x_offset_inches_fixed()
      png_get_x_offset_microns()
      png_get_x_offset_pixels()
      png_get_x_pixels_per_inch()
      png_get_x_pixels_per_meter()
      png_get_y_offset_inches()
      png_get_y_offset_inches_fixed()
      png_get_y_offset_microns()
      png_get_y_offset_pixels()
      png_get_y_pixels_per_inch()
      png_get_y_pixels_per_meter()
      png_handle_as_unknown()
      png_image_begin_read_from_file()
      png_image_begin_read_from_memory()
      png_image_begin_read_from_stdio()
      png_image_finish_read()
      png_image_free()
      png_image_write_to_file()
      png_image_write_to_memory()
      png_image_write_to_stdio()
      png_info_init_3()
      png_init_io()
      png_longjmp()
      png_malloc()
      png_malloc_default()
      png_malloc_warn()
      png_permit_mng_features()
      png_process_data()
      png_process_data_pause()
      png_process_data_skip()
      png_progressive_combine_row()
      png_read_end()
      png_read_image()
      png_read_info()
      png_read_png()
      png_read_row()
      png_read_rows()
      png_read_update_info()
      png_reset_zstream()
      png_save_int_32()
      png_save_uint_16()
      png_save_uint_32()
      png_set_IHDR()
      png_set_PLTE()
      png_set_add_alpha()
      png_set_alpha_mode()
      png_set_alpha_mode_fixed()
      png_set_bKGD()
      png_set_background()
      png_set_background_fixed()
      png_set_benign_errors()
      png_set_bgr()
      png_set_cHRM()
      png_set_cHRM_XYZ()
      png_set_cHRM_XYZ_fixed()
      png_set_cHRM_fixed()
      png_set_check_for_invalid_index()
      png_set_chunk_cache_max()
      png_set_chunk_malloc_max()
      png_set_compression_buffer_size()
      png_set_compression_level()
      png_set_compression_mem_level()
      png_set_compression_method()
      png_set_compression_strategy()
      png_set_compression_window_bits()
      png_set_crc_action()
      png_set_eXIf()
      png_set_eXIf_1()
      png_set_error_fn()
      png_set_expand()
      png_set_expand_16()
      png_set_expand_gray_1_2_4_to_8()
      png_set_filler()
      png_set_filter()
      png_set_filter_heuristics()
      png_set_filter_heuristics_fixed()
      png_set_flush()
      png_set_gAMA()
      png_set_gAMA_fixed()
      png_set_gamma()
      png_set_gamma_fixed()
      png_set_gray_to_rgb()
      png_set_hIST()
      png_set_iCCP()
      png_set_interlace_handling()
      png_set_invalid()
      png_set_invert_alpha()
      png_set_invert_mono()
      png_set_keep_unknown_chunks()
      png_set_longjmp_fn()
      png_set_mem_fn()
      png_set_oFFs()
      png_set_option()
      png_set_pCAL()
      png_set_pHYs()
      png_set_packing()
      png_set_packswap()
      png_set_palette_to_rgb()
      png_set_progressive_read_fn()
      png_set_quantize()
      png_set_read_fn()
      png_set_read_status_fn()
      png_set_read_user_chunk_fn()
      png_set_read_user_transform_fn()
      png_set_rgb_to_gray()
      png_set_rgb_to_gray_fixed()
      png_set_rows()
      png_set_sBIT()
      png_set_sCAL()
      png_set_sCAL_fixed()
      png_set_sCAL_s()
      png_set_sPLT()
      png_set_sRGB()
      png_set_sRGB_gAMA_and_cHRM()
      png_set_scale_16()
      png_set_shift()
      png_set_sig_bytes()
      png_set_strip_16()
      png_set_strip_alpha()
      png_set_swap()
      png_set_swap_alpha()
      png_set_tIME()
      png_set_tRNS()
      png_set_tRNS_to_alpha()
      png_set_text()
      png_set_text_compression_level()
      png_set_text_compression_mem_level()
      png_set_text_compression_method()
      png_set_text_compression_strategy()
      png_set_text_compression_window_bits()
      png_set_unknown_chunk_location()
      png_set_unknown_chunks()
      png_set_user_limits()
      png_set_user_transform_info()
      png_set_write_fn()
      png_set_write_status_fn()
      png_set_write_user_transform_fn()
      png_sig_cmp()
      png_start_read_image()
      png_warning()
      png_write_chunk()
      png_write_chunk_data()
      png_write_chunk_end()
      png_write_chunk_start()
      png_write_end()
      png_write_flush()
      png_write_image()
      png_write_info()
      png_write_info_before_PLTE()
      png_write_png()
      png_write_row()
      png_write_rows()
      png_write_sig()
EndImport

Debug png_access_version_number()
A+
Denis
User avatar
IceSoft
Addict
Addict
Posts: 1616
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Need help to compile LibPng into static lib

Post by IceSoft »

warum verwendest Du nicht einfach:

Code: Select all

UsePNGImageDecoder
UsePNGImageEncoder
Belive!
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Need help to compile LibPng into static lib

Post by Denis »

IceSoft wrote:warum verwendest Du nicht einfach:

Code: Select all

UsePNGImageDecoder
UsePNGImageEncoder
Hi IceSoft,
it doesn't work. even with importC, png_access_version_number() return value from PB libpng.lib, not mine (and no conflict with function name).
I keep looking, I'm stubborn.
A+
Denis
User avatar
IceSoft
Addict
Addict
Posts: 1616
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Need help to compile LibPng into static lib

Post by IceSoft »

Please tell us first what are you want todo.
For me it looks like you be a really early beginner also with PB.
And you don't understand what I mean with the native PB commands in my post before.
Belive!
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Need help to compile LibPng into static lib

Post by Denis »

IceSoft,

I understand you want me to use UsePNGImageDecoder/UsePNGImageEncoder with the lib file i generate.
i want to compile libpng.lib (also static lib), and not using Libpng from PB distrib (especially with longjump on error).
What i want is to have a personal LibPng config and simply use advanced libpng functions for my project.

Finally, it's to learn to how do a .lib file for PB.
For now, i will start with the dll wich seems to work. may be imp defs are lacking.

Thank you.
A+
Denis
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Need help to compile LibPng into static lib

Post by Olliv »

Salut Denis, (Hi Denis)

Excuse me to be a little bit très con but why this following syntax alea ?

Code: Select all

Png_Access_Version_Number
Png_Access_Versio_Number
The last char 'n' of the word 'version' makes me doubting...


Hei Icesoft, (Hi glace molle)
Icesoft wrote:For me it looks like you be a really early beginner also with PB.
A little bit wicked one... Is it necessary ? Lib file is particular : you create a file which is "private" and a half life can be done without making such files !
User avatar
skywalk
Addict
Addict
Posts: 3994
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Need help to compile LibPng into static lib

Post by skywalk »

Your ImportC is wrong. None of the functions have their parameters?
Looking at the libpng headers, it is tricky to discern the exported functions since they are generating them with 2 Macro's. PNG_EXPORT() and PNG_EXPORTA().
You have to get the functions after they are built. Maybe they are documented in the help manual?

As mentioned, you chose a path of high resistance. :wink:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Need help to compile LibPng into static lib

Post by Denis »

Salut Oliv, (ca va?)

'Png_Access_Versio_Number' is a mistake, i don't use it

Hi skywalk,
yes, exported functions are lacking.

When i take a deeper look to original libpng.lib i find for example :
__imp__longjmp_png_access_version_number
which is not present inside my file version.
I will take a look to the 2 macros.
As mentioned, you chose a path of high resistance.
Yes, It's interesting to have a headache. :lol:
I like to learn and understand what I do.
Libpng can easily be compiled under VS 2010, a project file is avalaible - ..\projects\vstudio\vstudio.sln
The project is updated to match with vs 2013 or 2019.

Thank you for your help.
A+
Denis
Justin
Addict
Addict
Posts: 830
Joined: Sat Apr 26, 2003 2:49 pm

Re: Need help to compile LibPng into static lib

Post by Justin »

I am not an expert on this, but if you declare the function with __stdcall and rebuild, it seems to find the function in the obj file?? but then the error is the typical unresolved external symbol.
in png.h:

Code: Select all

long __stdcall png_access_version_number(void);
in png.c:
long __stdcall png_access_version_number(void)
{
return 0;

/* Version of *.c files used when building libpng */
/*return((png_uint_32)PNG_LIBPNG_VER);*/
}
I would be very interested to know if someone has ever succeeded in compiling a static lib in pb.

edit: tried

Code: Select all

extern __stdcall
with same results
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Need help to compile LibPng into static lib

Post by Denis »

Hi Justin,

In file changes, the calling convention is cdecl since Version 1.5.0beta19:
Version 1.5.0beta19 [April 24, 2010] Changed Windows calling convention back to __cdecl for API functions.
For Windows/x86 platforms only:
__stdcall is no longer needed for Visual Basic, so libpng-1.5.0 uses
__cdecl throughout (both API functions and callbacks) on Windows/x86
platforms.
The problem comes with the lack of exported functions (at least I think so).

In png.h, there are exported definition like this :

Code: Select all

 * The PNG_EXPORT() and PNG_EXPORTA() macros used below are defined in
 * pngconf.h and in the *.dfn files in the scripts directory.
 *
 *   PNG_EXPORT(ordinal, type, name, (args));
 *
 *       ordinal:    ordinal that is used while building
 *                   *.def files. The ordinal value is only
 *                   relevant when preprocessing png.h with
 *                   the *.dfn files for building symbol table
 *                   entries, and are removed by pngconf.h.
 *       type:       return type of the function
 *       name:       function name
 *       args:       function arguments, with types
 *
 * When we wish to append attributes to a function prototype we use
 * the PNG_EXPORTA() macro instead.
 *
 *   PNG_EXPORTA(ordinal, type, name, (args), attributes);
 *
 *       ordinal, type, name, and args: same as in PNG_EXPORT().
 *       attributes: function attributes
 */

/* Returns the version number of the library */
PNG_EXPORT(1, png_uint_32, png_access_version_number, (void));

/* Tell lib we have already handled the first <num_bytes> magic bytes.
 * Handling more than 8 bytes from the beginning of the file is an error.
 */
PNG_EXPORT(2, void, png_set_sig_bytes, (png_structrp png_ptr, int num_bytes));

/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a
 * PNG file.  Returns zero if the supplied bytes match the 8-byte PNG
 * signature, and non-zero otherwise.  Having num_to_check == 0 or
 * start > 7 will always fail (ie return non-zero).
 */
PNG_EXPORT(3, int, png_sig_cmp, (png_const_bytep sig, size_t start,
    size_t num_to_check));
When i complile the dll and related libpng.lib, there is a exp file (libpng16.exp) in the complied folder for exported functions, so the dll works.
Inside, there is for example
_png_access_version_number CAN 8 DC1 STX
I'm still trying to get it works
A+
Denis
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Need help to compile LibPng into static lib

Post by jack »

hello Denis
the title says "Need help to compile LibPng into static lib" and yet in the first post you say that you built the library
so is your problem building a static libpng or using it in PB?
if you managed to build the static lib, did you use Cmake or some other way?
I managed to build a static libpng x64 using Cmake but it was a learning experience, and I don't know if I could clearly give the steps needed.
Justin
Addict
Addict
Posts: 830
Joined: Sat Apr 26, 2003 2:49 pm

Re: Need help to compile LibPng into static lib

Post by Justin »

The linker searches the png.obj for '_png_access_version_number' but in the obj file is undecorated 'png_access_version_number', i have no idea if this is normal.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Need help to compile LibPng into static lib

Post by jack »

in case someone wants to build libpng, here are the steps
what you need:
cmake
cmake-gui
gawk
visual studio
-----------------
extract the zlib archive
extract the libpng archive
open a command window
find out where vcvarsall.bat is and launch vcvarsall.bat x86 for 32-bit or vcvarsall.bat amd64 for 64-bit
cd to the zlib folder
edit win32\Makefile.msc and add -GS- to CFLAGS
nmake -f win32\Makefile.msc
------------------
inside the libpng folder, create a new folder by the name "build"
launch cmake-gui
enter the libpng source folder in the appropriate input field
enter the libpng build folder in the appropriate input field
------------------
on the bottom left click on configure
select the visual studio version that you want to use
optionally select the platform for generator, if empty, generator uses x64
click finish
------------------
you will get an Error in the configuration process, click OK
in the red pane, add /GS- to CFLAGS
scroll to the bottom
click on ZLIB_INCLUDE_DIR and replace ZLIB_INCLUDE_DIR-NOTFOUND with the path to the zlib folder
click on ZLIB_LIBRARY_DEBUG and delete ZLIB_LIBRARY_DEBUG-NOTFOUND and leave it blank
click on ZLIB_LIBRARY_RELEASE and replace ZLIB_LIBRARY_RELEASE-NOTFOUND with the full path of the zlib library, for example: F:\zlib-1.2.11\zlib.lib
------------------
Click on configure again
Click on generate
exit cmake-gui
------------------
launch visual studio
open the libpng.sln that's in the build folder
select your build options (debug or release) and build
Last edited by jack on Sun Nov 17, 2019 5:45 am, edited 1 time in total.
Justin
Addict
Addict
Posts: 830
Joined: Sat Apr 26, 2003 2:49 pm

Re: Need help to compile LibPng into static lib

Post by Justin »

Hi jack , thanks gor the info but have you been able to statically link the resulting .lib with import/endimport? That is yhe real question.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: Need help to compile LibPng into static lib

Post by jack »

@Justin
no, I get:
---------------------------
PureBasic - Linker error
---------------------------
POLINK: error: Unresolved external symbol '__security_cookie'.
POLINK: error: Unresolved external symbol '__security_check_cookie'.
POLINK: error: Unresolved external symbol '__GSHandlerCheck'.
POLINK: error: Unresolved external symbol '__imp___stdio_common_vfprintf'.
POLINK: error: Unresolved external symbol '__imp___acrt_iob_func'.
POLINK: error: Unresolved external symbol '__report_rangecheckfailure'.
POLINK: fatal error: 6 unresolved external(s).

I tried compiling the libraries with /GS- which I thought would get rid of some of the errors, I will keep on trying
Post Reply