libpng函数调用都是未解析的外部符号

libpng function calls are all unresolved external symbols

我正在尝试 link 我的项目针对 libpng 版本 1.6.23 dll 库,但是我的 link 同事只看到我对它进行的所有函数调用都是未解析的外部符号。我正在使用 Visual Studio Express 2013。这是 linker 输出的片段。

1>PNGUtils.obj : error LNK2019: unresolved external symbol png_set_sig_bytes referenced in function "public: static void * __cdecl PNGUtils::Read(char const *,unsigned int &,unsigned int &,int &)" (?Read@PNGUtils@@SAPEAXPEBDAEAI1AEAH@Z)
1>PNGUtils.obj : error LNK2019: unresolved external symbol png_sig_cmp referenced in function "private: static bool __cdecl PNGUtils::Validate(struct _iobuf *)" (?Validate@PNGUtils@@CA_NPEAU_iobuf@@@Z)
1>PNGUtils.obj : error LNK2019: unresolved external symbol png_create_read_struct referenced in function "public: static void * __cdecl PNGUtils::Read(char const *,unsigned int &,unsigned int &,int &)" (?Read@PNGUtils@@SAPEAXPEBDAEAI1AEAH@Z)
1>PNGUtils.obj : error LNK2019: unresolved external symbol png_set_longjmp_fn referenced in function "public: static void * __cdecl PNGUtils::Read(char const *,unsigned int &,unsigned int &,int &)" (?Read@PNGUtils@@SAPEAXPEBDAEAI1AEAH@Z)
1>PNGUtils.obj : error LNK2019: unresolved external symbol png_create_info_struct referenced in function "public: static void * __cdecl PNGUtils::Read(char const *,unsigned int &,unsigned int &,int &)" (?Read@PNGUtils@@SAPEAXPEBDAEAI1AEAH@Z)
1>PNGUtils.obj : error LNK2019: unresolved external symbol png_read_info referenced in function "public: static void * __cdecl PNGUtils::Read(char const *,unsigned int &,unsigned int &,int &)" (?Read@PNGUtils@@SAPEAXPEBDAEAI1AEAH@Z)
1>PNGUtils.obj : error LNK2019: unresolved external symbol png_set_expand_gray_1_2_4_to_8 referenced in function "public: static void * __cdecl PNGUtils::Read(char const *,unsigned int &,unsigned int &,int &)" (?Read@PNGUtils@@SAPEAXPEBDAEAI1AEAH@Z)

我 link 成功地使用了 lua 和 glew 等其他库,据我所知,在尝试 link 到 libpng 时我没有做任何不同的事情。

奇怪的是,当我将 linker 置于详细模式时。它指出...

1>  Finished searching libraries
1>  
1>  Unused libraries:
1>    libs\png\libpng16.lib

linker 似乎声称我没有使用 libpng 库,即使我清楚地调用了它的函数,例如在我的代码段中 PNGUtils.cpp 读取方法。

png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!pngPtr)
{
    LOG(LOG_CHANNEL_ERROR, "PNGUtils: Couldn't initialize png read struct");

    fclose(fp);

    return NULL;
}

png_infop infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr) 
{
    LOG(LOG_CHANNEL_ERROR, "PNGUtils: Couldn't initialize png info struct");
    png_destroy_read_struct(&pngPtr, (png_infopp)0, (png_infopp)0);

    fclose(fp);

    return NULL;
}

我不明白为什么 linker 似乎忽略了对 libpng 的函数调用。我很乐意听到任何想法或答案,当然也非常愿意根据要求提供更多信息。

确保您的项目和 libpng.dll 是使用 相同的设置,如 libpng 附带的 projects/vstudio/README.txt 中所述:

WARNING ======= Libpng 1.6.x does not use the default run-time library when building static library builds of libpng; instead of the shared DLL runtime it uses a static runtime. If you need to change this make sure to change the setting on all the relevant projects:

libpng
zlib
all the test programs

The runtime library settings for each build are as follows:

           Release        Debug
DLL         /MD            /MDd
Library     /MT            /MTd

NOTICE that libpng 1.5.x erroneously used /MD for Debug DLL builds; if you used the debug builds in your app and you changed your app to use /MD you will need to change it back to /MDd for libpng 1.6.0 and later.

The Visual Studio 2010 defaults for a Win32 DLL or Static Library project are as follows:

                 Release     Debug
DLL               /MD         /MDd
Static Library    /MD         /MDd

Also, be sure to build libpng, zlib, and your project all for the same platform (e.g., 32-bit or 64-bit).

cI 知道这个问题有点老了,但也许有人有同样的困难。

在我的案例中,解决“未解决的 link”(Visual Studio 16.4.6 2019)问题的 2 个步骤是:

  • 将我的项目编译为 x86(因为我使用的库是在这种架构下构建的)并使用 32 位版本的 libpng .dll
  • 添加zlib(代码中包含zlib.h和linking 对应的.dll和.lib)

这样就编译成功了