"Fatal error: 'EXTERN.h' file not found" while installing Perl modules
Asked Answered
U

8

20

While trying to install Perl modules like JSON::XS or YAML::XS, i receive the same error:

XS.xs:1:10: fatal error: 'EXTERN.h' file not found

I use MacBook, xCode is up to date, everything else that could help is up to date too.

Uranyl answered 6/10, 2018 at 18:49 Comment(3)
Sounds like your Perl installation is incomplete. It's missing some headers.Khalid
I think this can happen when certain libraries are updated after installing Perl. Try reinstalling Perl?Richelle
Like @Khalid said, you are missing key parts of Perl itself. Lots of linux distros only install parts Perl by default, and leave it to you to install the left-out bits as needed using their package manager (apt / yum). It might be the same for MacBooks?Colby
R
29

Since OS X El Capitan, Apple introduced System Integrity Protection which restricts writing to /usr/lib /usr/bin and other sensitive directories (even to root or sudo user) that are used by the installation of Perl bundled with the Operating System. This can cause issues when it comes to installing new modules and also if trying to install XS modules ( those linked to external C libraries ).

For this reason you should not consider the default Perl installation as a working development environment, especially if you are installing custom modules.

Check out this thread on PM and others. I had since El-Capitan managed to solve this before by manually building from tarball and adding a few params or environment variables to set the paths believing that it would be best to retain use of the system Perl but this is not the way to go. This makes your environment difficult to build but also brittle and sensitive to OS updates that may either break things in many different ways.

The best practice seems to be starting with a Perl using brew install perl and work in this environment, remembering to setup your bash_profile as directed by the installer.

Also worth remembering to do a brew link perl. If you receive warnings about this clobbering what looks like system Perl libraries don't worry - these are likely modules that were installed by you over the top and it will cause you less trouble to link over these. If you have concerns, make a note of which module installs will be cleared and re-install them once your environment is configured ( ie your module installer approach is configured using cpanm or sticking with the old perl -MCPAN -e shell etc)

This new Perl setup from brew eliminates the need to continuing running sudo which adds another layer of things that can go wrong as environment variables don't follow through and permission conflicts arise etc.

Finally to simplify package/module installation I suggest doing a brew install cpanminus. If you had previously already installed this, you can ensure the paths etc are configured by doing a brew reinstall cpanminus

If you want to take it another step further then you can install perlbrew as well which will give you the ability to run multiple versions of Perl as your user and configure these with their own libs and modules which can be very useful particularly if aligning with your production environment for testing etc.

One problem you may face if moving from system Perl to this kind of approach is needing to deal with any hangovers from installing things with sudo. It wis worth taking a little time to get all this set up right though and your issues going forward will be greatly reduced and you won't be left with that nagging feeling that you don't want to change anything for fear of it all breaking.

I have also come across a Perl Blog Article that suggests a fix for XS issues with perlbrew on Mojave

This Gist described updating your cpan shell install root though this shouldn't be necessary unless your cpan is stuck in an old config after taking steps above.

I've also raised this as a new issue on PerlMonks

Reverso answered 25/10, 2018 at 20:54 Comment(2)
brew link perl wasn't necessary for me just now. I'm also unsure whether it was useful to have installed Perl via Homebrew before installing perlbrew (and then re-installing Perl via it). It might be good to isolate one's Perl environment from Homebrew updates too; not just macOS. perlbrew install-cpanm seems nice too.Lamoreaux
brew reinstall cpanminus didn't install cpanm with the proper path--it references System perl. I had to install it manually: metacpan.org/pod/…Purlieu
R
12

After reading https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes#3035624 and installing the Additional headers via

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

I successfully compiled without the missing 'EXTERN.h' error

Richmound answered 22/3, 2019 at 17:57 Comment(2)
Yeah, thanks for your input. My problem was solved after complete deletion and clean, proper re-installation. Seems like i messed up something.Uranyl
executing /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg fixed that for me even if I had Xcode10 and command line tools already installed. Thanks!Tutti
R
10

In order to follow the common advice I also tried with Perlbrew to install a dedicated development version of Perl. Especially with the advice in mind First, do not use the system Perl on MacOS. The installed version is for Apple, not for you (see the discussion here: https://www.perlmonks.org/?node_id=1224727).

Unfortunately, the following error occurred:

Test Summary Report
-------------------
porting/libperl.t                                                (Wstat: 65280 Tests: 35 Failed: 0)
  Non-zero exit status: 255
  Parse errors: No plan found in TAP output
Files=2653, Tests=1217766, 708 wallclock secs (52.74 usr  9.40 sys + 395.38 cusr 49.90 csys = 507.42 CPU)
Result: FAIL
make: *** [test_harness] Error 1
##### Brew Failed #####

Therefore, I decided to install it the following way (and not following the advice due to the error).

Even after having the above mentioned macOS SDK headers already installed on Catalina (macOS 10.15.2) it didn't work for me. I faced the issue during the installation of the Perl module Mac-SystemDirectory-0.13. The following steps (by identifying the missing file in hope of having a more generic approach for more or less equivalent issues) did the trick:

  1. Locate the header file (in this case EXTERN.h)

    sudo find /Library -type f -name EXTERN.h
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE/EXTERN.h
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Perl/5.28/darwin-thread-multi-2level/CORE/EXTERN.h
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE/EXTERN.h
    
  2. Ensure the installed Perl version (here 5.18) match the header file:

    perl -v | grep version 
    This is perl 5, version 18, subversion 4 (v5.18.4) built for darwin-thread-multi-2level
    
  3. Export the path for the C-Compiler (note MacOSX10.15.sdk for Catalina and Perl Version 5.18)

    export CPATH=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE
    
  4. Invoke the Makefile.PL with perl

    perl Makefile.PL
    
Renoir answered 26/1, 2020 at 3:59 Comment(0)
D
4

BTW — For anybody who's still struggling with this, my workaround was:

bash% module="Sub::Util"    # For example

bash% cpanm --configure-args="INC=-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE" "$module"
Dichasium answered 3/2, 2021 at 1:16 Comment(0)
J
2

For Big Sur and perl 5.30, EXTERN.h is at /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Perl/5.30/darwin-thread-multi-2level/CORE

I'm trying to upgrade CPAN itself and got that error. But I have /usr/bin/cpan and I can't write there so I have to tweak it to write the updated version to /usr/local/bin/cpan.

Jensen answered 19/7, 2021 at 21:18 Comment(0)
R
1

Please try this

CPATH=$(dirname $(find /usr/local/Cellar/ -name EXTERN.h)) cpan JSON::XS
Rabbitry answered 24/5, 2021 at 7:56 Comment(0)
M
1

No promises, but yum install perl-devel worked for me.

As @huyz has helpfully pointed out, if you hit this error on a Mac, you don't have this option, even though this is probably your issue, and you need to follow one of the above methods of getting a version of Perl that isn't missing important chunks, as per other answers.

But if, dear reader, you hit this error on a linux host, as I did, then this might be an option for you.

Macula answered 18/6, 2021 at 7:15 Comment(2)
OP is on macOS.Purlieu
@huyz. Problem isn't limited to MacOS.Macula
J
1

Building on what E Lisse suggested, you might also have luck looking in

/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/ 

For example:

CPATH=$(dirname $(find /System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/ -name EXTERN.h)) cpan JSON::XS

You could also find where EXTERN.h is located and add that to your shell by default, e.g. in your .bashrc or .zshrc file:

export CPATH=/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE/
Jolley answered 4/10, 2022 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.