Undefined reference to cocos2d::unzOpen() on Windows

Hi! I tried to use features defined in ./external/unzip/unzip.h with Cocos2d-x V 3.17.2 but it throws this error, as if this function was not implemented… Apparently it has its definition in unzip.cpp from the same folder, and the cpp file is included as src of cococ2d project. What does this error mean? Any help is appreciated.
EDIT:
I’m using VS2017 on Win10.
All I wanted to do is just testing unzip. I included external/unzip/unzip.h and called unzOpen("somefile.zip") . It throws me an Undefined reference to cocos2d::unzOpen() error.

Which features?

What is the error message?

You didn’t include the important information in your post.

Hi R101. Thanks for the reply. I edited my post It’s a simple test of unzip.

That function is working perfectly fine on Windows 10 with 3.17.2.

Have you tried to step through the call to find out where it’s actually failing? It can’t be ‘undefined’, since from what you’ve stated, it compiled correctly, and it would have thrown an error if it couldn’t find the declaration, or if it couldn’t link to the definition of that function.

Also, are you sure the path to the zip file is correct?

Sorry It was a little bit complicated to explain what I did. I will try:

  1. I created a fresh C++ project with cocos2d-x V3.17.2
  2. I add #include "external/unzip/unzip.h" to my HelloWorldScene.cpp file in order to use the unzOpen() function, just for testing.
  3. It failed to compile because of zlib.h not found, in file unzip.h:
#ifndef _ZLIB_H
#include "zlib.h"
#endif

I modified the middle line to #include "./external/zlib/include/zlib.h" and it passes the include file check.
(Then I also did the same to ioapi.h because it also complains zlib.h not found)

  1. Weird error: <unistd.h> not found. This should be a unix header file, I don’t know why it’s get included. I then found the following content in zconf.h which seems to caused the macro Z_HAVE_UNISTD_H to be true.
#if 1    /* was set to #if 1 by ./configure */
#  define Z_HAVE_UNISTD_H
#endif

I changed the 1 to !CC_PLATFORM_WIN32 and the switch is successfully turned off.

  1. Tried to compile. It gives the Undefined reference to cocos2d::unzOpen() error.

    This is definitely not a correct way to use unzip… But how do you get it to work on Windows? Thanks!

You shouldn’t have to go to those lengths to use that function, so something may be wrong with your project files.

Try creating a new project using this:

cocos new gamename -p com.example.gamename -l cpp -d .

Then create the Visual Studio 2019 project file using cmake like this:

mkdir win32build
cd .\win32build
cmake .. -G"Visual Studio 16 2019" -Tv142 -A Win32

Now open the VS solution file win32build/gamename.sln, and in HelloWorld.cpp file, add this include:

#include <unzip.h>

Then use this in the init() method:

auto zipFile = unzOpen(FileUtils::getInstance()->getSuitableFOpen("filename.zip").c_str());

That works for me on Windows 10, using cocos2d-x 3.17.2, and release v3-deps-159 of the external dependencies.

EDIT: Out of curiosity, did you open up the project in the proj.win32 folder? If so, avoid it, because it may not work correctly, and generate your project files using cmake.

1 Like

Thank you for the detailed explanation. Worked like a charm now. Yes I did open up my project from the proj.win32 folder and the project seems really weird.:rofl:

I’m glad you got it working. I’m not sure if the proj.win32 has been updated at all for the new releases, or that it even serves any purpose now that cmake builds are working so well.

While @R101 's CMake solution did the job. I would like to write down what I found that was causing my problem:
In external/unzip.h, there is a macro CC_DLL in the function signatures:

unzFile CC_DLL unzOpen OF((const char *path));

Apparently, CC_DLL should be be translated to __declspec(dllexport) because this file belongs to libcocos2d project which is linked as DLL to the Game project. However I found the following lines in both unzip.h and ioapi.h :

// #include "platform/CCPlatformConfig.h"
#define CC_DLL 

The macro then become useless. and The Undefined reference error is because of missing __declspec(dllexport).
I enabled the 1st line instead of the 2nd, it worked.

The other problem in the proj.win32 project file is that zlib and unzip include paths were not added correctly. We can add the following lines to Additional Include Paths to solve the header not found issue.

$(EngineRoot)external\win32-specific\zlib\include
$(EngineRoot)external\unzip
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.