Building Imagick PHP extension for CentOS/RHEL 7 fails with error `C preprocessor “/lib/cpp” fails sanity check` and how to fix it

So I recently stumbled upon a problem with building Imagick PHP extension on CentOS/RHEL 7 and it drove me a little bit mad co’z I can’t seem to find the fix only to realize that the solution was actually so easy.

ImageMagick

ImageMagick

The following are the steps I made to reproduce the issue and fix it:

  1. Make sure you have gcc, php-devel, php-pear, and of course ImageMagick and ImageMagick-devel installed by running:
[root@ip-172-31-13-5 imagick-3.4.1]# yum install gcc php-devel php-pear ImageMagick ImageMagick-devel
  1. Then download the Imagick source from pecl.php.net and extract it
[root@ip-172-31-13-5 imagick-3.4.1]# wget https://pecl.php.net/get/imagick-3.4.1.tgz
[root@ip-172-31-13-5 imagick-3.4.1]# tar -zxvf imagick-3.4.1.tgz
  1. Cd to the extracted directory and run phpize
[root@ip-172-31-13-5 imagick-3.4.1]# cd imagick-3.4.1
[root@ip-172-31-13-5 imagick-3.4.1]# phpize

The phpize output should look like below:

[root@ip-172-31-13-5 imagick-3.4.1]# phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
  1. Once phpize finishes, start the build by running
./configure --with-php-config=/bin/php-config

The output should look like below:

[root@ip-172-31-13-5 imagick-3.4.1]# ./configure --with-php-config=/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... yes
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... unsupported
checking how to run the C preprocessor... /lib/cpp
configure: error: in `/home/ec2-user/imagick-3.4.1':
configure: error: C preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details

This is where the error shows.
The solution:
Install glibc-headers by running:

[root@ip-172-31-13-5 imagick-3.4.1]# yum install glibc-headers

Once the glibc-headers installation is done, try building again.
The output should look like below:

[root@ip-172-31-13-5 imagick-3.4.1]# ./configure --with-php-config=/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
...
...
...
checking for objdir... .libs
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC -DPIC
checking if cc PIC flag -fPIC -DPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking if cc supports -c -o file.o... (cached) yes
checking whether the cc linker (/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands
  1. Once the configure finishes, run make:
[root@ip-172-31-13-5 imagick-3.4.1]# make

It’s output should look like below:

[root@ip-172-31-13-5 imagick-3.4.1]# make
/bin/sh /home/ec2-user/imagick-3.4.1/libtool --mode=compile cc -fopenmp -I/usr/include/ImageMagick -I. -I/home/ec2-user/imagick-3.4.1 -DPHP_ATOM_INC -I/home/ec2-user/imagick-3.4.1/include -I/home/ec2-user/imagick-3.4.1/main -I/home/ec2-user/imagick-3.4.1 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/ImageMagick -DHAVE_CONFIG_H -g -O2 -c /home/ec2-user/imagick-3.4.1/imagick_file.c -o imagick_file.lo
libtool: compile: cc -fopenmp -I/usr/include/ImageMagick -I. -I/home/ec2-user/imagick-3.4.1 -DPHP_ATOM_INC -I/home/ec2-user/imagick-3.4.1/include -I/home/ec2-user/imagick-3.4.1/main -I/home/ec2-user/imagick-3.4.1 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/include/ImageMagick -DHAVE_CONFIG_H -g -O2 -c /home/ec2-user/imagick-3.4.1/imagick_file.c -fPIC -DPIC -o .libs/imagick_file.o
...
...
...
Libraries have been installed in:
/home/ec2-user/imagick-3.4.1/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.
  1. Then run make install:
[root@ip-172-31-13-5 imagick-3.4.1]# make install

The output should look like below:

[root@ip-172-31-13-5 imagick-3.4.1]# make install
Installing shared extensions: /usr/lib64/php/modules/
Installing header files: /usr/include/php/
  1. Now that extension is installed, let’s configure PHP to use it by adding a new php config file in /etc/php.d named imagick.ini
[root@ip-172-31-13-5 imagick-3.4.1]# vim /etc/php.d/imagick.ini

Then add the following inside the file:

extension=imagick.so
  1. Verify that Imagick is installed:
[root@ip-172-31-13-5 imagick-3.4.1]# php -m | grep imagick
imagick

Congratulations!

References:
http://trulymanaged.com/blog/error-libcpp-fails-sanity-check/
https://www.liaohuqiu.net/posts/install-php-imagick-on-centos/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: