I'm trying to display an image on the screen, so I installed SDL2 image. I can successfully compile my code, but when I try to run the resulting executable it gives me this error:
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
I have tried running brew upgrade libpng
and brew update
, but neither worked. I'm not sure what I can do to fix this.
In case it helps, here's the code I used.
In 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, 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, 0, 0, 0, 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;
}
And in Makefile:
main:
g++ main.cpp -o image -I include -L lib -l SDL2-2.0.0 -l SDL2_image-2.0.0
I managed to fix this by deleting the libpng folder in /usr/local/Cellar
and installing it with brew install libpng
all over again.