Question

I'm trying to use SDL_image in my project however I can't get past this error message

||=== Build: Win32 Release in Sandbox (compiler: GNU GCC Compiler) ===|
..\lib\mingw32\libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
..\lib\mingw32\libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
..\lib\mingw32\libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
..\lib\mingw32\libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
||error: ld returned 1 exit status|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

At first I got undefined reference to _TIFFmalloc, _TIFFrealloc etc. but I fixed them by creating a source file and adding it to the project.

tif_fix.c

// This file is intended to fix the "undefined reference..." errors

#include <stdlib.h>
#include <string.h>

#include "tiffio.h"

void* _TIFFmalloc(tmsize_t s)
{
    return malloc(s);
}

void* _TIFFrealloc(void* p, tmsize_t s)
{
    return realloc(p, s);
}

void _TIFFmemset(void* p, int v, tmsize_t c)
{
    memset(p, v, c);
}

void _TIFFmemcpy(void* d, const void* s, tmsize_t c)
{
    memcpy(d, s, c);
}
int _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
{
    return memcmp(p1, p2, c);
}

void _TIFFfree(void* p)
{
    free(p);
}

All the libraries needed by SDL_image are compiled as static libraries (zlib, libpng, libjpeg, libwebp and libtiff4) and even SDL_image is compiled as static.

I have defined LOAD_BMP, LOAD_GIF,LOAD_JPG, LOAD_LBM, LOAD_PCX, LOAD_PNG, LOAD_PNM, LOAD_TGA, LOAD_TIF, LOAD_WEBP, LOAD_XCF, LOAD_XPM, LOAD_XV, LOAD_XXX in the SDL_image project to enable the libraries. Both SDL_image and the libraries compile without any problems.

However, when I link SDL_image and the libraries to my application's project and try to compile the example showimage.c provided with SDL_image I get the undefined reference to... error message.

I've tried to reorder the libraries in the Code::Blocks project because I know the order is important with MinGW (I'm using MinGW 4.8.2 x86). So far nothing works and I'm out of ideas.

Was it helpful?

Solution

I was able to fix the error and finally make the application to compile and work by adding two new functions to the tif_fix.c file:

static void msdosWarningHandler(const char* module, const char* fmt, va_list ap)
{
    // Do not handle warnings
}

TIFFErrorHandler _TIFFwarningHandler = msdosWarningHandler;

static void msdosErrorHandler(const char* module, const char* fmt, va_list ap) {

    // use this for diagnostic only (do not use otherwise, even in DEBUG mode)
    /*
    if (module != NULL) {
        char msg[1024];
        vsprintf(msg, fmt, ap);
        FreeImage_OutputMessageProc(s_format_id, "%s: %s", module, msg);
    }
    */
}

TIFFErrorHandler _TIFFerrorHandler = msdosErrorHandler;

I found them in the FreeImage > PluginTIFF.cpp file so the credit goes to FreeImage.

EDIT: After some manual searching though the LibTiff4 library files I found some info in the Changelog file and apparently there is libtiff/tif_{unix,vms,win32}.c that has them implemented but the file wasn't included in my Code::Blocks project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top