Fixing C++ compilation bugs for the MacOS Catalina upgrade
<p dir="auto">I recently updated to macOS Catalina and encountered several issues with programs that use a C/C++ compiler.<br />
If you see any of these errors this post might be helpful for you:
<pre><code>/usr/local/Cellar/llvm/7.0.1/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file
not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
<p dir="auto">Or any error indicating that a standard library couldn't be found:
<pre><code>ld: library not found for -ldl
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
<h2>What's the issue?
<p dir="auto">The issue is that macOS Catalina doesn't use the <code>/usr/include and <code>/usr/lib/ directories anymore for all C++ headers and libraries.<br />
A lot of projects rely on these directories to exist, can't find the files and then break.<br />
Instead they are under the xcode path and you need to tell the C/C++ compiler where to find these libraries now.
<h2>How to fix it?
<ol>
<li>Optionally, install a C++ compiler like clang / gcc through <code>brew if one is missing.
<li>Install <strong>Xcode through the App store to get access to the developer command-line tools.<br />
This will install the C++ header files and libraries, albeit, in a different directory. They will be installed in <code>/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk or a similar directory. To get the correct path for your system you can run the <code>xcrun --show-sdk-path command.
<li>The C++ compiler needs to be told to look at this root path instead of <code>/usr/include. This can be done by setting the <code>CPLUS_INCLUDE_PATH env variable. It's important to also <strong>include LLVM's normal include path because this is where it should search first.
<pre><code># adjust your llvm and CLT include paths to match your setup
export CPLUS_INCLUDE_PATH=/usr/local/opt/llvm/include/c++/v1:/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include
This should fix any <code>fatal error: 'xxx.h' file not found errors.
<li>You might still run into linker errors like <code>ld: library not found for -lxxx. The linker also needs to be told to look for libraries in the CommandLineTools/Xcode paths by setting the <code>LIBRARY_PATH env variable.
<pre><code>export LIBRARY_PATH=$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/lib
<li>Make sure to add these <code>export statements to your <code>.bash_rc, <code>.zshrc, <code>.bash_profile, or whatever shell you use, to make these adjusted environment variables available in all terminals.
<h3>Resources
<p dir="auto">Thanks a lot to this <a href="https://github.com/imageworks/OpenShadingLanguage/issues/1055#issuecomment-581920327" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">GitHub post for helping me figure out what's wrong.
<hr />
<p dir="auto">Originally published at <a href="https://cmichel.io/fixing-cpp-compilation-bugs-for-the-mac-os-catalina-upgrade/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://cmichel.io/fixing-cpp-compilation-bugs-for-the-mac-os-catalina-upgrade/
Thank you for sharing this amazing post on HIVE!
non-profit curation initiative!Your content got selected by our fellow curator @tibfox & you just received a little thank you via an upvote from our
You will be featured in one of our recurring curation compilations and on our pinterest boards! Both are aiming to offer you a stage to widen your audience within and outside of the DIY scene of hive.
Join the official DIYHub community on HIVE and show us more of your amazing work and feel free to connect with us and other DIYers via our discord server: https://discord.io/diyhub!
If you want to support our goal to motivate other DIY/art/music/homesteading/... creators just delegate to us and earn 100% of your curation rewards!
Stay creative & hive on!