C++LibTiff:对_TIFFerrorHandler的未定义引用

C++ LibTiff: undefined reference to _TIFFerrorHandler

本文关键字:未定义 引用 TIFFerrorHandler C++LibTiff      更新时间:2023-10-16

我正试图在我的项目中使用SDL_image,但我无法通过此错误消息

||=== Build: Win32 Release in Sandbox (compiler: GNU GCC Compiler) ===|
..libmingw32libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
..libmingw32libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
..libmingw32libtiff4.a(tif_error.o):tif_error.c|| undefined reference to `_TIFFerrorHandler'|
..libmingw32libtiff4.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)) ===|

起初,我得到了对_TIFFalloc、_TIFFrealloc等的未定义引用,但我通过创建一个源文件并将其添加到项目中来修复它们。

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);
}

SDL_image所需的所有库都编译为静态库(zlib、libpng、libjpeg、libwebp和libtiff4),甚至SDL_iimage也编译为静态。

我在SDL_image项目中定义了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以启用库。SDL_image和库的编译都没有任何问题。

然而,当我将SDL_image和库链接到我的应用程序的项目,并尝试编译SDL_iimage提供的示例showimage.c时,我得到了对…错误消息的未定义引用。

我尝试重新排序Code::Blocks项目中的库,因为我知道顺序对MinGW很重要(我使用的是MinGW 4.8.2 x86)。到目前为止,一切都不起作用,我也没什么想法。

我能够修复错误,并通过在tif_fix.c文件中添加两个新函数最终使应用程序编译和工作:

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;

我在FreeImage>PluginTIFF.cpp文件中找到了它们,所以这归功于FreeImage。

编辑:在手动搜索了LibTiff4库文件后,我在Changelog文件中找到了一些信息,显然有libtiff/tif_{unix,vms,win32}.c实现了它们,但我的代码::块项目中没有包含该文件。