1

*****Code*****

#include "CImg.h"
#include <stdlib.h>

using namespace cimg_library;

int main() {

    CImg<unsigned char> image("lena.png");

    image.display();

    return 0;

}

*****ERROR***** Build successful but stuck somewhere: OUTPUT:

[CImg] * CImgIOException * [instance(0,0,0,0,0x0,non-shared)] CImg::load(): Failed to open file 'lena.png'. libc++abi.dylib: terminating with uncaught exception of type cimg_library::CImgIOException: [instance(0,0,0,0,0x0,non-shared)] CImg::load(): Failed to open file 'lena.png'.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
郭昆鹏
  • 11
  • 3

1 Answers1

1

If you want to use CImg on macOS, you need to do a couple of things.

Step 1 - PNG support

If you want CImg to be able to open PNG files, you must first install libpng and libz, which you can do with homebrew as follows:

brew install libpng

Then you must tell CImg that you want it to include support for PNG files, which you do by declaring a define before including CImg.h

#define cimg_use_png 1
#include "CImg.h"

Then you must tell Xcode where the library is. That is easiest via pkgconfig, so install that with:

brew install pkg-config

Now you can get the path to the header files (#includes) with:

pkg-config --cflags /usr/local/lib/pkgconfig/libpng.pc

Output

-I/usr/local/Cellar/libpng/1.6.34/include/libpng16

That tells you what to put in Xcode for the paths to the header files. Then you need to find the Linker settings with:

pkg-config --libs /usr/local/lib/pkgconfig/libpng.pc

Output

-L/usr/local/Cellar/libpng/1.6.34/lib -lpng16 -lz

So you must put that into Xcode as the path for linked libraries and the names for libraries to link to.

Step 2 - X11 suport

Once you have done that, and only if you want to display images on the screen, you need to set up XQuartz because macOS doesn't ship with an X11 server any more.

So, go to this website and download and install XQuartz. You will then need to tell Xcode where the headers (#includes) for XQuartz are, which is:

/opt/X11/include

and also how to link with it:

-L /opt/X11/lib -lX11

There are some helpful screenshots of the places in Xcode you need to alter on my answer to this question.

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432