【问题标题】:error: LNK1120: 9 unresolved externals错误:LNK1120:9 个未解决的外部
【发布时间】:2014-05-07 14:02:42
【问题描述】:

我正在尝试使用以下代码将 bmp 图像转换为 png 图像:

#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_DEPRECATE

#include <png.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

void GetDesktopResolution(int& horizontal, int& vertical)
{
    RECT desktop;
    // Get a handle to the desktop window
    const HWND hDesktop = GetDesktopWindow();
    // Get the size of screen to the variable desktop
    GetWindowRect(hDesktop, &desktop);
    // The top left corner will have coordinates (0,0)
    // and the bottom right corner will have coordinates
    // (horizontal, vertical)
    horizontal = desktop.right;
    vertical = desktop.bottom;
}

typedef struct _RGBPixel {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
} RGBPixel;

/* Structure for containing decompressed bitmaps. */
typedef struct _RGBBitmap {
    RGBPixel *pixels;
    size_t width;
    size_t height;
    size_t bytewidth;
    uint8_t bytes_per_pixel;
} RGBBitmap;

/* Returns pixel of bitmap at given point. */
#define RGBPixelAtPoint(image, x, y) \
    *(((image)->pixels) + (((image)->bytewidth * (y)) \
                        + ((x) * (image)->bytes_per_pixel)))

/* Attempts to save PNG to file; returns 0 on success, non-zero on error. */
int save_png_to_file(RGBBitmap *bitmap, const char *path)
{
    FILE *fp = fopen(path, "wb");
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    size_t x, y;
    png_uint_32 bytes_per_row;
    png_byte **row_pointers = NULL;

    if (fp == NULL) return -1;

    /* Initialize the write struct. */
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png_ptr == NULL) {
        fclose(fp);
        return -1;
    }

    /* Initialize the info struct. */
    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL) {
        png_destroy_write_struct(&png_ptr, NULL);
        fclose(fp);
        return -1;
    }

    /* Set up error handling. */
    if (setjmp(png_jmpbuf(png_ptr))) {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
        return -1;
    }

    /* Set image attributes. */
    png_set_IHDR(png_ptr,
                 info_ptr,
                 bitmap->width,
                 bitmap->height,
                 8,
                 PNG_COLOR_TYPE_RGB,
                 PNG_INTERLACE_NONE,
                 PNG_COMPRESSION_TYPE_DEFAULT,
                 PNG_FILTER_TYPE_DEFAULT);

    /* Initialize rows of PNG. */
    bytes_per_row = bitmap->width * bitmap->bytes_per_pixel;
    png_malloc(png_ptr, bitmap->height * sizeof(png_byte *));
    for (y = 0; y < bitmap->height; ++y) {
        uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel);
        row_pointers[y] = (png_byte *)row;
        for (x = 0; x < bitmap->width; ++x) {
            RGBPixel color = RGBPixelAtPoint(bitmap, x, y);
            *row++ = color.red;
            *row++ = color.green;
            *row++ = color.blue;
        }
    }

    /* Actually write the image data. */
    png_init_io(png_ptr, fp);
    png_set_rows(png_ptr, info_ptr, row_pointers);
    png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);

    /* Cleanup. */
    for (y = 0; y < bitmap->height; y++) {
        png_free(png_ptr, row_pointers[y]);
    }
    png_free(png_ptr, row_pointers);

    /* Finish writing. */
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(fp);
    return 0;
}

int main()
{
        RGBBitmap rgbbitmap;
    int w, h;
    GetDesktopResolution(w, h);
    rgbbitmap.height = h;
    rgbbitmap.width = w;
    rgbbitmap.bytes_per_pixel = 1;
    rgbbitmap.bytewidth = w / 100;

    RGBPixel rgbpixel;
    rgbpixel.blue = 100;
    rgbpixel.green = 100;
    rgbpixel.red = 100;
    rgbbitmap.pixels = &rgbpixel;

    save_png_to_file(&rgbbitmap, "abc.bmp");

        return 0;
}

执行此代码会触发这些错误:

LNK1120:9 个未解决的外部问题

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析外部符号 _png_create_info_struct (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析的外部符号 _png_create_write_struct (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析的外部符号 _png_destroy_write_struct (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析外部符号 _png_free (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析外部符号 _png_init_io (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析外部符号 _png_malloc (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”(?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z) 中引用了未解析的外部符号 _png_set_IHDR

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”(?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z) 中引用的未解析外部符号 _png_set_rows

LNK2019:函数“int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)”中引用的未解析外部符号 _png_write_png (?save_png_to_file@@YAHPAU_RGBBitmap@@PBD@Z)

我无法找到解决这些错误的方法。请问有什么绝妙的建议吗?

我目前在 Windows 7 SP1 平台上使用 Visual Studio Ultimate 2013。

非常感谢!

【问题讨论】:

  • 安装了PNG Library?并将其添加到您的项目中?
  • @JoachimPileborg,是的,没关系。
  • 您似乎在使用 libpng 而不添加库。在菜单“PROJECT -> Properties -> Linker -> Input -> Additional Dependencies”中添加“libpng.lib”并确保库目录有您的 libpng 目录。
  • @rookiepig,这样做会引发另一个问题The program can't start because libpng12.dll is missing from your computer. 请问如何解决?
  • 抱歉,错过了一件事。要解决这个问题,您可以将该 dll 复制到您的项目文件夹中。另一种解决方案是将libpng的“bin”路径添加到项目的可执行目录或windows系统路径中。

标签: c++


【解决方案1】:

我想,你没有链接你的库,只是包含了标题。 This question 回答你是怎么做到的...

如果没有,可能会发生很多事情:

  • 您正在尝试使用错误参数调用这些函数
  • 你包含了错误的头文件
  • 您有混合库,或者您正在尝试将 MinGW、VS2012/VS2012 编译库链接到 VS2013 编译器,因为我不知道它们是否兼容...

您可以尝试download png 库,创建 VS2012 项目并尝试编译它。当你这样做时,你应该在链接时绝对没有问题......

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 2015-01-11
    • 2017-09-25
    • 2012-07-20
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2012-06-03
    相关资源
    最近更新 更多