macoshomebrewlibpng

Libpng installed from Brew claims to be an incompatible architecture on MacOS with M3


After installing libpng using Homebrew (brew install libpng), attempting to compile a project using the command gcc -o render_region.o render_region.c -I/usr/local/Cellar/libpng/1.6.43/include/libpng16 -L/usr/local/Cellar/libpng/1.6.43/lib -lpng16 returns

ld: warning: ignoring file '/usr/local/Cellar/libpng/1.6.43/lib/libpng16.16.dylib': found architecture 'x86_64', required architecture 'arm64'
Undefined symbols for architecture arm64:
  "_png_create_info_struct", referenced from:
      _saveImage in render_region-26644b.o
  "_png_create_write_struct", referenced from:
      _saveImage in render_region-26644b.o
  "_png_destroy_write_struct", referenced from:
      _saveImage in render_region-26644b.o
      _saveImage in render_region-26644b.o
      _saveImage in render_region-26644b.o
  "_png_init_io", referenced from:
      _saveImage in render_region-26644b.o
  "_png_set_IHDR", referenced from:
      _saveImage in render_region-26644b.o
  "_png_set_longjmp_fn", referenced from:
      _saveImage in render_region-26644b.o
  "_png_write_end", referenced from:
      _saveImage in render_region-26644b.o
  "_png_write_image", referenced from:
      _saveImage in render_region-26644b.o
  "_png_write_info", referenced from:
      _saveImage in render_region-26644b.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Searching online, the only similar problems I've seen were solved by adding -I/usr/local/Cellar/libpng/1.6.43/include/libpng16 -L/usr/local/Cellar/libpng/1.6.43/lib -lpng16 to the flags. I was adding the flags through libpng-config --cflags --ldflags so I verified what flags it was adding but it didn't help. I also tried reinstalling and checking the libpng site.

Did I somehow install the wrong version? Or is it linked incorrectly?

(EDIT) My computer has an M3 chip. After talking to someone elsewhere, I determined the initial cause of the problem was most likely due to bringing my profile (including my brew installation) over from my old macbook which did not use Apple silicon.


Solution

  • I showed the error to a friend irl and they realized that it was most likely caused by having brought my profile over from my old macbook.

    The Homebrew website has instructions for if there are installations for both architectures installed and I was able to modify them to adapt to having the wrong version installed.

    brew bundle dump --global
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    brew bundle install --global
    

    After that the file compiled fine.

    I was away from my laptop for a couple of years when the M1 actually came out but it seems if you update far enough the errors look a little different than they did at the beginning.