clang++ installed via homebrew (macOS): compilation errors - c++

After installing clang++ (tried v. 6.0.1 and 7.0) with:
brew install --with-toolchain llvm
very trivial programs result to the following error:
In file included from test.cpp:1:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iostream:38:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/ios:215:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iosfwd:90:
/usr/local/Cellar/llvm/7.0.0/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
Command used to compile:
clang++7() {
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
CPPFLAGS="-I/usr/local/opt/llvm/include"
/usr/local/opt/llvm/bin/clang++ -std=c++11 $CPPFLAGS $LDFLAGS $1
}
Is it possible to use the official clang instead of Apple's version?
With Apple's version, we do not even know which version of LLVM it really is...

It appears that as of Mojave (10.14), Xcode doesn't install system headers in /usr/include anymore. There is a compatibility package that does, but it's not recommended.
Instead, the official solution is for tools to search for headers in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk. That path can be obtained from
xcrun --show-sdk-path
The release notes say
The Command Line Tools package installs the macOS system headers inside the macOS SDK. Software that compiles with the installed tools will search for headers within the macOS SDK provided by either Xcode at:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
or the Command Line Tools at:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
depending on which is selected using xcode-select.
If you built clang yourself, this can be achieved by passing the -isysroot option to clang:
clang++ -isysroot "$(xcrun --show-sdk-path)" …
See also: https://github.com/Homebrew/homebrew-core/issues/32765

It works for me to add a -I (minus eye) option to the clang++ command line, pointing to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include

Related

How to find and provide C++ library headers for clang?

I've built LLVM and Clang from sources using following instruction in order to try some of the latest C++ features.
When I try to compile basic C++ program using this clang I get errors about missing basic headers:
% /usr/local/bin/clang++ -std=c++20 main.cpp
In file included from main.cpp:1:
main.cpp:3:10: fatal error: 'array' file not found
#include <array>
I similarly have brew installed clang, which works perfectly.
The instruction mentions providing C++ library headers to clang, but I don't understand:
How to locate those?
How to make sure, that they are also up to date to support latest C++ features?
I have MacOS Monterey 12.4
You should specify a path to the sdk for your target platform.
You can retreive this by relying on xcrun:
/usr/local/bin/clang++ -isysroot $(xcrun --show-sdk-path) -std=c++20 main.cpp
In case you want to target other platforms (in the example bellow, for iphone) you have to specify both the target triple and the path to the appropiate sdk:
/usr/local/bin/clang++ -target arm64-apple-ios -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -std=c++20 main.cpp
There is also an alternative to the -isysroot option. You could set up the SDKROOT environment variable to point to the target sdk path:
export SDKPATH=$(xcrun --show-sdk-path)
/usr/local/bin/clang++ -std=c++20 main.cpp

Issue with Intel compilation on macOS 10.15 Catalina : catastrophic error: cannot open source file "stdlib.h"

I try desperatly to compile with c++ intel compiler (icpc) a simple code that I was used to compile on my previous macBook 10.14.
Now, since Headers don't exist anymore with 10.15 Catalina, the compiler has difficulties to find stdlib.h. Here the output I get :
icpc -std=c++11 -O3 -xHost -fp-model strict -prec-div -prec-sqrt -fma -ftz -mp1 -qopenmp -Wall -c -I/opt/intel/compilers_and_libraries_2020.2.258/mac/mkl/include -mkl=parallel main.cpp -o main.o
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/math.h(301),
from /opt/intel/compilers_and_libraries_2020.2.258/mac/compiler/include/math.h(177),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/cmath(304),
from TSAF.h(1),
from main.cpp(1):
/Library/Developer/CommandLineTools/usr/include/c++/v1/stdlib.h(20): catastrophic error: cannot open source file "stdlib.h"
#include_next <stdlib.h>
^
compilation aborted for main.cpp (code 4)
make: *** [Makefile:13: main.o] Error 4
./main_intel.exe: No such file or directory
I tried a lot of potential workarounds from the net but none works. I would like to avoid to create a directory /usr/include/ by removing the security system of my macBook.
I thought maybe this would work by downloading XCode_12.0.1 but same problems remain.
For the moment, I have the version Catalina OS 10.15.7, all has been updated.
EDIT 1: There is something that I don't understand : on my previous macBook 10.14, I didn't need to use the headers of clang c++ for intel compilation.
If anyone could give me a suggestion/clue/track, this would be fine.
Run the command in the Terminal
sudo xcode-select --reset
And then check if proper developer tools are selected instead of the command line tools
xcode-select -p

Issue compiling a piece of open source software written in C++ on MacOS Sierra

I am trying to install a piece of open source software (AB3P) on MacOS Sierra. When I issue the command make I get this error: ld: library not found for -lops. The make command issued before the error is thrown is: g++ -g -o make_wordSet make_wordSet.o -L./Library -lops -D_POSIX_C_SOURCE=199506L -lpthread
I have the latest version of MacOS Sierra and Xcode + the Xcode command line developer tools (as of this writing). In addition gcc is installed, the command gcc --version returns:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I am not sure how to approach the problem, any suggestions?
Get into Library folder, and make it first.
cd Library
make
There it will find the libops.a
On the updated version of the Ab3P as #ridoro1 mentioned, you need to make first NCBITextLib and edit the both Makefiles under Ab3P and also under Library.
First get into Library and make it. Then make Ab3P.
You need to download this library and compile it, then edit Makefile in the folder of this library and the Makefile in the lib folder inside it to point to NCBITextLib-master folder on your disk, after that You can compile the ABP3 library.

C++11 on Mac with Clang or GCC

I have Xcode 4.5.2 on Moutain Lion, and I have install the lastest "Command Line Tools" but when I tried to compile with g++ or clang++ (and the options -std=c++11 -stdlib=libc++) I get an error.
With g++:
cc1plus: error: unrecognized command line option "-std=c++11"
cc1plus: error: unrecognized command line option "-stdlib=libc++"
With clang++:
clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)
It's in a Qt project.
So how can I used the C++11 on my Mac ?
As you found, g++ does not support those command line options.
It sounds like you're using Xcode.
For clang, you should look at the project settings, and make sure that the "Deployment Target" is set to 10.7 (or 10.8)
What the error message is telling you is that libc++ is not available for 10.6 and before.
I installed gcc-4.7 on my Mac to make C++11 work. GCC in its current version is fairly good at supporting C++11, so this should be a fair choice.
The installation can be done by Homebrew and is not that complicated (at least I was able to do it...)
To install Homebrew if you do not already have it:
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
Now run
brew doctor
and fix whatever problems come up (there is something written in the hombrew documentation for that). Finally, install current gcc:
brew install gcc
If everything goes well you should be able to access g++-4.7, which allows -std=c++0x.
Try -std=c++0x if c++11 doesn't work. Support for the -std=c++11 option is relatively new in GCC and you might not have a recent enough version.
I'd trust Marshall on the libc++ issue.

Missing Python.h while trying to compile a C extension module

I'm following this tutorial on how to extend Python with C\C++ code.
The section named "Building the extension module with GCC for Microsoft Windows" fails for me with the following error:
fatal error: Python.h: No such file or directory
The section named "Building the extension module using Microsoft Visual C++" also fails with a similar error:
fatal error C1083: Cannot open include file: 'Python.h': No such file or directory
What should I do to solve this?
For Linux, Ubuntu users to resolve the issue of missing Python.h while compiling, simply run the following command in your terminal to install the development package of python:
In Terminal: sudo apt-get install python-dev
Good luck
Do you have the python dev files so that you can find Python.h?
Do you have the location of Python.h specified to your compiler? with gcc this is usually done through a -I path to include.
Figuring out which of those is failing will solve your problem.
from the article you linked:
gcc -c hellomodule.c -I/PythonXY/include
gcc -shared hellomodule.o -L/PythonXY/libs -lpythonXY -o hello.dll
They assumed you installed python in the default location c:\pythonXY(Where X is the major version number and Y is the minor version number).(in your case Python26) If you put python somewhere else replace /PythonXY with where ever you installed it.
The Python official documentation has already made it clear. Check it out here
The header files are typically installed with Python. On Unix, these are located in the directories prefix/include/pythonversion/ and exec_prefix/include/pythonversion/, where prefix and exec_prefix are defined by the corresponding parameters to Python’s configure script and version is '%d.%d' % sys.version_info[:2]. On Windows, the headers are installed in prefix/include, where prefix is the installation directory specified to the installer.
To include the headers, place both directories (if different) on your compiler’s search path for includes. Do not place the parent directories on the search path and then use #include ; this will break on multi-platform builds since the platform independent headers under prefix include the platform specific headers from exec_prefix.
And they have provided a convenient way to get the correct cflags that we should pass to compiler. here
So for example, here is what I got after running the command
root#36fd2072c90a:/# /usr/bin/python3-config --cflags
-I/usr/include/python3.5m -I/usr/include/python3.5m -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
Pass those flags to the compiler, and it will work.