Bugster
Bugster

Reputation: 1594

Odd undefined reference errors

Alright I will try to describe my problem in detail. I've started using a ziploading API for SFML in order to read game resources from an archive, I've placed all required headers and source files in my project in Codeblocks, so my project consists of the following:

Source files for game
ZipLoader.h
ZipLoader.cpp
ioapi.h
unzip.h
zconf.h
zlib.h

So in my game menu header, where I attempt to draw a game menu based on resources in the archive, I have this:

    #include <SFML/Graphics.hpp>
    #include "Globals.h"
    #include "ZipLoader.h"

class GameMenu
{
    private:
    Zip::File file();

}

Which is basically, where I tried to initialize a zip file so I can load from it in gameMenu.cpp where I declared my menu. However I'm getting some very weird errors in ZipLoader.cpp, which are as follows:

src\ZipLoader.cpp|81|undefined reference to `unzOpen'|
src\ZipLoader.cpp|89|undefined reference to `unzLocateFile'|
src\ZipLoader.cpp|94|undefined reference to `unzOpenCurrentFile'|
src\ZipLoader.cpp|100|undefined reference to `unzGetCurrentFileInfo'|
src\ZipLoader.cpp|105|undefined reference to `unzReadCurrentFile'|
src\ZipLoader.cpp|109|undefined reference to `unzCloseCurrentFile'|
src\ZipLoader.cpp|110|undefined reference to `unzClose'|

But I find that impossible, I figured I'm missing the library but I have it imported in the file that's giving me errors, all the methods listed above are located in unzip.h which is imported in the file that is giving me errors as seen bellow:

#include "ZipLoader.h"
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
#include "unzip.h"

...
unzFile ZipHandle=unzOpen(DataFile); // error????

And as you can see bellow, in unzip.h these references exist very well:

extern unzFile ZEXPORT unzOpen OF((const char *path));
extern unzFile ZEXPORT unzOpen2 OF((const char *path,
                                    zlib_filefunc_def* pzlib_filefunc_def));
    extern int ZEXPORT unzClose OF((unzFile file));

This is by far the oddest error I've ever gotten in a C++ program, how can it be undefined when it's right there? (I'm using Windows XP SP3)

Upvotes: 1

Views: 2782

Answers (3)

Simon Hyll
Simon Hyll

Reputation: 3628

I had a very similar problem with unzGoToFirstFile. If you download the ZLib sources and do a simple "grep -irl unzGoToFirstFile" you'll find that these functions are not part of ZLib at all. They are however part of a contributed library called minizip found within the ZLib sources.

Add this to the ZLib CMakeLists.txt somwhere near the bottom and it will create a library called "libunzip.a" with the required unzGoToFirstFile etc functions you require. Just link your program towards that and you should be golden.

set(UNZIP_SOURCES "contrib/minizip/unzip.c"
"contrib/minizip/zip.c"
"contrib/minizip/ioapi.c")
set(UNZIP_HEADERS "contrib/minizip/unzip.h"
"contrib/minizip/zip.h"
"contrib/minizip/ioapi.h")

add_library(unzip STATIC 
"${UNZIP_SOURCES}"
"${UNZIP_HEADERS}")

target_link_libraries(unzip zlibstatic)

install(TARGETS unzip
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )

install(FILES ${UNZIP_HEADERS} DESTINATION "${CMAKE_INSTALL_PREFIX}/include")

Upvotes: 2

Hernan Velasquez
Hernan Velasquez

Reputation: 2820

You have your functions declared in a header file so you should not have compiling error. Your problem is that in the linking process, you are not including the libraries that contains the implementation of those functions.

You have to know where are those files and include them in the linking process.

Upvotes: 1

Mark
Mark

Reputation: 6093

I think, you forgot to add the zlib to the library list.

If you're using MSVC, go to your project settings, open the Linker settings and add zlib.lib (I believe this is the filename of libz for win32) to the additional dependencies.

If you're using gcc, add -lzlib (or was it -lz ?) to your commandline.

Upvotes: 1

Related Questions