macOS Monterey下Perl Imager::File::PNG安装失败:libpng库/头文件未找到
问题背景
我在MacBook(macOS Monterey系统)上运行Perl脚本生成“魔法笔”OID图片(image.png)时持续报错。我不是程序员,谷歌排查一个多月没解决,已完成以下操作:
- 安装Homebrew
- 通过
brew install perl安装Perl - 通过
brew install libpng安装libpng - 在
perl -MCPAN -eshell中安装Imager、YAML及MP3::Info,均无报错
旧款MacBook按这些步骤能正常运行脚本,但这台Mac安装Homebrew时做过修改,不确定Imager能否调用libpng。尝试安装Imager::File::PNG时出现如下错误:
Running install for module 'Imager::File::PNG'
Checksum for /Users/darina/.cpan/sources/authors/id/T/TO/TONYC/Imager-File-PNG-0.99.tar.gz ok
Scanning cache /Users/darina/.cpan/build for sizes
............................................................................DONE
Configuring T/TO/TONYC/Imager-File-PNG-0.99.tar.gz with Makefile.PL
PNG: building independently
PNG: Generic: includes not found - libraries found
PNG: v1.6: includes not found - libraries found
PNG: v1.5: includes not found - libraries not found
PNG: v1.4: includes not found - libraries not found
PNG: v1.2: includes not found - libraries not found
PNG: v1.0: includes not found - libraries not found
PNG: base (+libz): includes not found - libraries not found
PNG: v1.6 (+libz): includes not found - libraries not found
PNG: v1.5 (+libz): includes not found - libraries not found
PNG: v1.4 (+libz): includes not found - libraries not found
PNG: v1.2 (+libz): includes not found - libraries not found
PNG: v1.0 (+libz): includes not found libraries not found
PNG: Checking if the compiler can find them on its own
PNG: Test code failed: Can't link/include 'png.h', 'stdio.h', 'png'
OS unsupported: PNG libraries or headers not found
Warning: No success on command[/usr/local/Homebrew/Cellar/perl/5.34.0/bin/perl Makefile.PL]
TONYC/Imager-File-PNG-0.99.tar.gz /usr/local/Homebrew/Cellar/perl/5.34.0/bin/perl Makefile.PL -- NOT OK
Failed during this command:
TONYC/Imager-File-PNG-0.99.tar.gz : writemakefile NO '/usr/local/Homebrew/Cellar/perl/5.34.0/bin/perl Makefile.PL' returned status 512
详细解决步骤(一步步操作)
步骤1:找到libpng的安装路径
打开终端,输入以下命令并按回车:
brew --prefix libpng
终端会输出一串路径(比如/usr/local/Cellar/libpng/1.6.40),用鼠标选中路径后按Command+C复制。
步骤2:安装cpanm(如果没装过)
如果之前没安装过cpanm,在终端输入以下命令安装:
brew install cpanm
等待安装完成后继续下一步。
步骤3:手动指定路径安装Imager::File::PNG
在终端输入以下命令,把[刚才复制的libpng路径]替换成你实际复制的路径:
cpanm --configure-args="--png-inc=[刚才复制的libpng路径]/include --png-lib=[刚才复制的libpng路径]/lib" Imager::File::PNG
举个例子,如果复制的路径是/usr/local/Cellar/libpng/1.6.40,完整命令就是:
cpanm --configure-args="--png-inc=/usr/local/Cellar/libpng/1.6.40/include --png-lib=/usr/local/Cellar/libpng/1.6.40/lib" Imager::File::PNG
输入完按回车,等待安装完成。
步骤4:验证安装是否成功
安装完成后,在终端输入以下命令:
perl -MImager::File::PNG -e 'print "安装成功\n"'
如果终端输出安装成功,说明安装没问题;如果还是报错,继续下一步。
步骤5:设置环境变量后重试
如果步骤3失败,在终端依次输入以下3条命令,每条输完按回车:
export C_INCLUDE_PATH=$(brew --prefix libpng)/include:$C_INCLUDE_PATH export LIBRARY_PATH=$(brew --prefix libpng)/lib:$LIBRARY_PATH export LD_LIBRARY_PATH=$(brew --prefix libpng)/lib:$LD_LIBRARY_PATH
然后再次运行安装命令:
cpanm Imager::File::PNG
安装完成后,再用步骤4的命令验证。
步骤6:测试脚本
回到你的Perl脚本所在文件夹,运行脚本,检查是否能正常生成image.png。
内容的提问来源于stack exchange,提问作者Darina

