库未加载 libpng16.16.dylib 因为版本不兼容

问题描述

我想在屏幕上显示图像,所以我安装了 SDL2 图像。我可以成功编译我的代码,但是当我尝试运行生成的可执行文件时,它给了我这个错误

dyld: Library not loaded: /usr/local/opt/libpng/lib/libpng16.16.dylib
  Referenced from: /usr/local/opt/sdl2_image/lib/libSDL2_image-2.0.0.dylib
  Reason: Incompatible library version: libSDL2_image-2.0.0.dylib requires version 54.0.0 or later,but libpng16.16.dylib provides version 51.0.0

我试过运行 brew upgrade libpngbrew update,但都没有奏效。我不确定我能做些什么来解决这个问题。

如果有帮助,这是我使用的代码

在 main.cpp 中:

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>

#undef main

int main(int argc,char** args)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    IMG_Init(IMG_INIT_PNG);
    SDL_Window* window = SDL_CreateWindow("Getting Started",SDL_WINDOWPOS_UNDEFINED,800,600,SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window,-1,0);
    SDL_Event input;
    bool quit = false;

    SDL_Texture* texture = NULL;
    SDL_Surface* temp = IMG_Load("some_image.png");

    texture = SDL_CreateTextureFromSurface(renderer,temp);

    SDL_FreeSurface(temp);

    SDL_Rect rect;
    rect.x = 0;
    rect.y = 500;
    rect.w = 100;
    rect.h = 100;

    while (!quit) {
        while (SDL_PollEvent(&input) > 0) {
            if (input.type == SDL_QUIT) quit = true;
        }

        SDL_SetRenderDrawColor(renderer,255);
        SDL_RenderClear(renderer);

        SDL_Rendercopy(renderer,texture,NULL,&rect);

        SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    IMG_Quit();
    SDL_Quit();

    return 0;
}

在 Makefile 中:

main:
    g++ main.cpp -o image -I include -L lib -l SDL2-2.0.0 -l SDL2_image-2.0.0

解决方法

我设法通过删除 /usr/local/Cellar 中的 libpng 文件夹并使用 brew install libpng 重新安装它来解决此问题。