Linking error while customizing zcalloc and zcfree functions in the zlib library

1.4k views Asked by At

While integrating the Nurbs library (http://www.rhino3d.com/opennurbs) into my project, I run into the following linking error with the library zlib.

1>zlib.lib(deflate.obj) : error LNK2019: unresolved external symbol _zcfree referenced in     function _z_deflateInit2_
1>zlib.lib(inflate.obj) : error LNK2001: unresolved external symbol _zcfree
1>zlib.lib(deflate.obj) : error LNK2019: unresolved external symbol _zcalloc referenced in     function _z_deflateInit2_
1>zlib.lib(inflate.obj) : error LNK2001: unresolved external symbol _zcalloc 

I checked the function

z_deflateInit2

in the file deflate.c from ZLIB source code, and guess that this function cannot find the implementation of the two functions zcfree adn zcalloc. The reason is that the Nurbs library customize the function zcfree and zcalloc, which is done in two steps.

  1. Cusomize zcalloc and zcfree in the the Nurbs library code.

    opennurbs_zlib.h
    extern "C" {
        voidpf zcalloc (voidpf, unsigned, unsigned);
        void  zcfree (voidpf, voidpf);
    }
    

    These two functions are implementated in the file opennurbs_zlib_memory.cpp as following

    #define voidpf z_voidpf
    voidpf zcalloc (voidpf, unsigned items, unsigned size)
    {
        return oncalloc(items, size);
    }
    
    void  zcfree (voidpf, voidpf ptr)
    {
        onfree(ptr);
    }
    
  2. Compile the ZLIB library with the flag: MY_ZCALLOC and Z_PREFIX

As I checked, both these two steps are done, but why I still got the linking error.

Could you give me some advices? Thanks so much!

Update: @Dale Lear: thanks for your support. But my situation is different. Instead if linking with the opennurbs.lib, I tried to integrate the source code of opennurbs into my project (like the surface module of point cloud library: http://www.pointclouds.org/blog/trcs/moerwald/). I build zlib from the project zlib in opennurbs solution. This zLib is built with the modified zconfig.h (Z_PREFIX and Z_MYCALL, i guess so, is defined). This means that the function zfree and zalloc is still waiting for implementation. But why does ZLib does not take zcfree() and zcalloc from opennurbs_zlib_memory.cpp. I don't understand why does it take.

1

There are 1 answers

0
Dale Lear On

From your description and the error log you provided, I'm guessing that the situation is:

1) You are building a Windows program using some version of Microsoft's C++ compiler.

2) You want to statically link with zlib.lib

3) You want to statically link with opennnurbs_staticlib.lib

I cannot determine what version of Microsoft's C++ compiler or opennurbs you're using.

If you are using the latest public release of opennurbs (version 2013-07-11), then the zcfree() and zcalloc() functions are defined in the file opennurbs_zlib_memory.cpp.

If you build opennurbs_staticlib.lib using the opennurbs_staticlib.vcxproj project file that is included with the source code, it will compile opennurbs_zlib_memory.cpp and include the zcfree() and zcalloc() functions in it in opennurbs_staticlib.lib. If you build zlib using the zlib code and zlib/zlib.vcxproj file that is included with opennurbs 2013-07-11, then it will be built with all the necessary defines and you will have two static libraries, zlib.lib and opennurbs_staticlib.lib, that link with all dependencies resolved.

If you are using customized project files, the first thing to check is that you are statically linking the results of compiling opennurbs_zlib_memory.cpp in some way.

If you want to use opennurbs as a DLL, I'd suggest building opennurbs.dll with the opennurbs.vcxproj project file that comes with the source code. This opennurbs.dll will statically include zlib.lib when it links and you do not have to link with anything except the resulting opennurbs.lib to use the DLL version.

Does this help?

-- Dale Lear