With this trivial example, I get a compilation error:
#include <unordered_map>
int main() {
std::unordered_map<int, int> a, b;
a.merge(b);
}
Error:
$ clang++ -std=c++17 merge.cpp
merge.cpp:5:4: error: no member named 'merge' in 'std::__1::unordered_map<int, int, std::__1::hash<int>, std::__1::equal_to<int>, std::__1::allocator<std::__1::pair<const int, int> > >'
a.merge(b);
~ ^
1 error generated.
Versions:
$ clang++ --version
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
According to cppreference, this should be legal since C++17. GCC 7 is happy to compile it.
I had same issue. I was outraged, why I can't compile application which use unordered_map::merge() using macOS 10.14.6
unordered_map::merge() was added in c++17 with proposal P0083R3 "Splicing Maps and Sets (Revision 5)", see github blame.
Documentation presents common compilers' support for new C++ features. Find row "Splicing Maps and Sets" and check you compiler version.
"Splicing Maps and Sets" compilers' support:
+-----------------------+---------------------+------+
| Compiler | Version | Link |
+-----------------------+---------------------+------+
| GCC libstdc++ | 7.1 | [1] |
| Clang libc++ | 8.0 | [2] |
| MSVC Standard Library | 19.12 (VS 2017 15.5)| [3] |
| Apple Clang | - | [4] |
+-----------------------+---------------------+------+
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
https://libcxx.llvm.org/Cxx1zStatus.html#cxx1z-status
https://learn.microsoft.com/cpp/overview/visual-cpp-language-conformance?view=vs-2019
Apple LLVM have special implementation of clang compiler, see description below
if you request a clang version, you get something like this:
➜ ~ clang++ --version
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Apple's clang version number has nothing to with the official's one. Some users try to map apple's and official clang versions here. I think, it's not always possible.
Also I couldn't find exhaustive table of c++ standard support status for different Apple Clang versions, unlike another compilers (share it in comments if you have). All we have is official xcode release notes.
But mac OS users can still take advantage of all the new features of c++17. You should only... install original llvm. Detailed guide read in article "Installing LLVM/Clang on OSX" by Phillip Johnston.
Installing llvm using brew is much safer:
llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another
version in parallel can cause all kinds of trouble.
Install llvm:
// optional "--with-toolchain" from article is deprecated
brew install llvm
Check status of llvm
➜ ~ brew info llvm
llvm: stable 8.0.1 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
/usr/local/Cellar/llvm/8.0.1 (6,807 files, 3.3GB)
Poured from bottle on 2019-09-14 at 14:19:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/llvm.rb
==> Dependencies
Build: cmake ✔
Required: libffi ✔, swig ✔
==> Requirements
Build: xcode ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have llvm first in your PATH run:
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.zshrc
For compilers to find llvm you may need to set:
export LDFLAGS="-L/usr/local/opt/llvm/lib"
export CPPFLAGS="-I/usr/local/opt/llvm/include"
Check clang version:
➜ ~ /usr/local/Cellar/llvm/8.0.1/bin/clang++ --version
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
Configure project
Now you can define compiler, I use CMake and add lines in CMakeLists.txt:
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/8.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/8.0.1/bin/clang++")
set(CMAKE_CXX_STANDARD 17)
or pass cmake command's options:
➜ ~ cmake \
-D CMAKE_C_COMPILER="/usr/local/Cellar/llvm/8.0.1/bin/clang" \
-D CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/8.0.1/bin/clang++" \
/path/to/CMakeLists.txt
or define env variables:
➜ ~ export CC=/usr/local/Cellar/llvm/8.0.1/bin/clang
➜ ~ export CXX=/usr/local/Cellar/llvm/8.0.1/bin/clang++
➜ ~ cmake /path/to/CMakeLists.txt
Related
I am trying to install hpctoolkit using spack. In order to do that, I executed :
git clone https://github.com/spack/spack.git
cd spack/share/spack
source setup-env.sh
spack fetch -D hpctoolkit
spack install hpctoolkit
I can't execute the last command because I get the following error:
Error: ProcessError: Command exited with status 1:
'./bootstrap.sh' '--prefix=/home/hakim/spack/opt/spack/linux-ubuntu20.04-haswell/gcc-10.2.0/boost-1.76.0-oc2u6jxritfsbci4xkhr5lov3i4o4riq' '--with-toolset=gcc' '--with-libraries=serialization,atomic,log,exception,regex,math,random,program_options,wave,iostreams,chrono,system,test,graph,locale,timer,filesystem,date_time,thread' '--without-icu'
It recommended me to take a look at build log by displaying the following message:
See build log for details:
/tmp/hakim/spack-stage/spack-stage-boost-1.76.0-oc2u6jxritfsbci4xkhr5lov3i4o4riq/spack-build-out.txt
and the previous file contains:
A C++11 capable compiler is required for building the B2 engine.
Toolset 'gcc' does not appear to support C++11.
> g++ -x c++ -std=c++11 check_cxx11.cpp
ERROR: Compiler 'gcc#10.2.0' does not support compiling C++ programs.
While reading a little bit the Spack notation, I learned that ’#’ specifies the package version so I guess that the version of gcc I'm using does not support compiling C++ programs.
How should I do to make it support compiling C++ programs ?
Any help, please ?
As you can see in the error, compiler 'gcc#10.2.0' does not support compiling C++ programs.
In order to display the compilers, use the command:
spack compiler list
Before retiring the misleading version, I had the following result for the previous command:
-- clang ubuntu20.04-x86_64 -------------------------------------
clang#10.0.0 clang#7.0.1
-- gcc ubuntu20.04-x86_64 ---------------------------------------
gcc#9.3.0 gcc#10.2.0
In order to get rid of the version 'gcc#10.2.0', I modified compilers.yaml which is a separate file to store information about available compilers. This file is normally in your home directory at ~/.spack/platform where ‘platform’ is normally ‘linux’ (or else ‘cray’ or ‘bgq’).
In my case, I did:
cd ~/.spack/linux
emacs compilers.yaml &
and found (I'm displaying only the part related to the gcc compiler):
compilers:
- compiler:
spec: gcc#10.2.0
paths:
cc: /usr/bin/gcc-10
cxx: null
f77: /usr/bin/gfortran-10
fc: /usr/bin/gfortran-10
flags: {}
operating_system: ubuntu20.04
target: x86_64
modules: []
environment: {}
extra_rpaths: []
- compiler:
spec: gcc#9.3.0
paths:
cc: /usr/bin/gcc-9
cxx: null
f77: /usr/bin/gfortran-9
fc: /usr/bin/gfortran-9
flags: {}
operating_system: ubuntu20.04
target: x86_64
modules: []
environment: {}
extra_rpaths: []
In order to get rid of gcc#10.2.0, just delete its part.
Verify now the list of compilers and you should find:
-- clang ubuntu20.04-x86_64 -------------------------------------
clang#10.0.0 clang#7.0.1
-- gcc ubuntu20.04-x86_64 ---------------------------------------
gcc#9.3.0
Last step:
spack install hpctoolkit
Now, Everything is OK.
I'm trying to install Riak from source on macOS (https://docs.riak.com/riak/kv/2.2.3/setup/installing/mac-osx.1.html#installing-from-source).
There is a note:
Riak will not compile with Clang. Please make sure that your default
C/C++ compiler is GCC
How do I find out which compiler is the default and how to change it?
macOS Catalina (10.15.4), which command prints:
$ which clang
/usr/bin/clang
$ which gcc
/usr/bin/gcc
On macOS Catalina (and prior versions, and most likely subsequent versions too), there are two aspects to the problem and some suggested solutions.
What is the name of the compiler used by make by default?
$ mkdir junk
$ cd junk
$ > x.cpp
$ > y.c
$ make x y
c++ x.cpp -o x
cc y.c -o y
$ cd ..
$ rm -fr junk
This shows that the names used by make are cc and c++. Those are not obviously clang or clang++, but neither are they obviously gcc and g++.
$ which cc c++
/usr/bin/cc
/usr/bin/c++
$
Which compiler is it really?
Which compiler really lives behind the names cc, c++, gcc, g++, clang, and clang++? We can check which compiler these really are by getting them to identify their version:
$ for compiler in cc c++ gcc g++ clang clang++
> do
> which $compiler
> $compiler --version
> done
/usr/bin/cc
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/c++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/gcc
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/g++
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$
As you can see, the versions installed in /usr/bin are all the same compiler, and that compiler is clang or clang++.
This was run on a machine with macOS Mojave 10.14.6 and XCode 11.3.1. The latest version of XCode — 11.4.1 — is only available on Catalina. However, the general conclusion is the same — all the C and C++ compilers are really clang and clang++ in disguise.
How do you get GNU GCC onto your machine?
How do you get a real GNU GCC — a real GCC, not clang in disguise — onto your machine?
Use Brew to install GCC (I've not checked which version of GCC is current).
Or use MacPorts (again, I've not checked which version of GCC is current).
If you're adventuresome, do it yourself (but I've not yet succeeded at building GCC 9.3.0 on Catalina; I have a GCC 9.2.0 built on macOS Mojave 10.14.x that works OK on Catalina though — with one environment variable needed to locate the headers).
Maybe Fink — it lists GCC 8.4 as being made available in 2020; I don't know about newer versions.
Be aware that Apple has taken to hiding the system header files miles out of the way (not in /usr/include — and you can't modify that part of the file system to add a symlink to where they've hidden them):
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
(You mean you couldn't guess that? Me neither!)
How do you change the default compiler?
Once you have GCC installed somewhere appropriate, you need to ensure you use the 'real' GCC and not the 'fake' in /usr/bin. You do that in part by ensuring that the bin directory for the 'real' GCC occurs on your PATH before /usr/bin. I have GCC 9.3.0 installed under /opt/gcc/v9.3.0, so /opt/gcc/v9.3.0/bin appears on my PATH long before /usr/bin does.
You also need to ensure that the configuration for riak (the software you're installing) uses the correct compilers. If there's a ./configure script, run it with the correct path specified for the compilers. For example, I might use:
./configure CC=/opt/gcc/v9.3.0/bin/gcc CXX=/opt/gcc/v9.3.0/bin/g++
You can also set these values as environment variables.
If it uses cmake or some other configuration package, you'll need to consult the installation instructions. That's usually README or sometimes INSTALL.
See also (increasingly older posts):
Can't compile a C program on a Mac after upgrade to Catalina
Can't compile a C program on a Mac after upgrade to Mojave
Install GNU GCC on a Mac
What version of LLVM does the latest emscripten use?
I googled for this quite a while but could not find any info. The background is that I've got some C++ code that requires clang-5.0 - what are the chances that it would compile with emscripten? Or at least code that needs clang-4.0?
The latest version of emscripten uses clang version 4:
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.37.21
clang version 4.0.0 (https://github.com/kripken/emscripten-fastcomp-clang.git 974b55fd84ca447c4297fc3b00cefb6394571d18) (https://github.com/kripken/emscripten-fastcomp.git 087c6b7b18b7b769d4ad8f2ac3e0dd0ae6b924c2) (emscripten 1.37.21 : 1.37.21)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
However, you can build WebAssembly without emscripten, by using the clang, llc, s2wasm and wasm tools separately as documented in this gist.
Another good alternative is the wasm-toolchain project:
https://github.com/tpimh/wasm-toolchain
With this installed, I have clang version 6:
$ ./clang --version
clang version 6.0.0 (http://llvm.org/git/clang.git 9411957410813aeefc1bb299abcb856ce1ef8aae) (http://llvm.org/git/llvm.git 68b21d6108df63d65d6735e9686d53cca844e37a)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
My OS is OS X 10.10.2 and the default compiler for C is clang.
But this version of clang does not support ubsan (undefined sanitizer) which comes in the 3.4 release of clang. I also want to use KLEE to do some analysis. AFAIK KLEE works well with LLVM-<=3.4. I decided to install
llvm-3.4 and clang-3.4 in my laptop.
After installing clang-3.4 in my system, I encountered a issue that the compiler can not locate the c++ header file. I installed clang-3.4 in /usr/local and I can find the c++ header file in /usr/local/include/c++/4.8.4. How can I add this directory to the search path of clang-3.4 and also the c++ library?
for the following demo code:
#include <iostream>
using namespace std;
int main(){
cout<<"Hellow World\n";
return 0;
}
When I compile it using command clang++ test.cpp, I got the error
test1.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Below is the version of clang I used
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
I used the following shell command to install llvm-3.4 and clang-3.4:
wget http://llvm.org/releases/3.4/llvm-3.4.src.tar.gz \
http://llvm.org/releases/3.4/clang-3.4.src.tar.gz \
http://llvm.org/releases/3.4/clang-tools-extra-3.4.src.tar.gz \
http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz
tar zxf llvm-3.4.src.tar.gz
tar zxf clang-3.4.src.tar.gz -C llvm-3.4/tools
mv llvm-3.4/tools/clang{-3.4,}
tar zxf clang-tools-extra-3.4.src.tar.gz -C llvm-3.4/tools/clang/tools
mv llvm-3.4/tools/clang/tools/{clang-tools-extra-3.4,extra}
tar zxf compiler-rt-3.4.src.tar.gz -C llvm-3.4/projects
mv llvm-3.4/projects/compiler-rt{-3.4,}
cd llvm-3.4
./configure --enable-cxx11 \
--enable-bindings=none --enable-shared \
--enable-debug-symbols --enable-optimized
make
make install
Now I have two versions of clang in my OS, one is the default one shipped with OSX located in /usr/bin and the other is clang-3.4 located in /usr/local/bin. The previous one can find the C++ header file while the latter can not.
Did you read the user documentation of clang notably the section on command line options ? BTW, current (march 2015) version of clang is 3.6!
For C++ code you should use the clang++ command, not the clang command.
You might pass -I and -L options to clang++ but you might perhaps have misinstalled your clang compiler.
You should be aware of the -v and -H options of clang or clang++ ; they could be useful, at least to understand more your issue.
addenda
BTW, a program reported to work with Clang 3.4 is extremely likely to work with a more modern version, like Clang 3.5 or 3.6
You probably have a PATH issue; you should have configure -d your Clang-3.4 & LLvm-3.4 programs with --program-suffix=-my-3.4 (if you do that, repeat your entire compiler build and installation) and you probably should run /usr/local/bin/clang++-my-3.4 ; maybe you also need some --with-gcc-toolchain additional configure option.
I'm pretty sure that you should be able to try to compile or use your mysterious software requiring Clang-3.4 with a more modern version like 3.5 or 3.6 ; your MacOSX 10.10.2 is rumored to have a Clang-3.5 based system compiler, it very probably is able to compile and work with your mysterious software.
I'm trying to compile a CMake project which uses
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-Wall -std=gnu++0x")
in the CMakeLists.txt file under MacOS X Lion. I have installed XCode 4.2.1. but the compiler fails with this:
cd something/src/lib && /usr/bin/c++ -Dlib_ginacra_EXPORTS -Wall -std=gnu++0x -fPIC -o CMakeFiles/lib_ginacra.dir/utilities.cpp.o -c something/src/lib/utilities.cpp
cc1plus: error: unrecognized command line option "-std=gnu++0x"
The compiler's verion is:
c++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
GCC 4.2 is ancient, but Apple don't ship a newer version.
You can either install a modern GCC from somewhere like Mac Ports (which is probably simpler and quicker) or build it yourself following the instructions at http://gcc.gnu.org/wiki/InstallingGCC
For Lion users facing this issue:
Download and Install the MacPorts-2.2.1-10.7-Lion.pkg MacPorts
installer package from here
in a terminal, search for newer GCC versions:
$ port search --name --glob 'gcc*'
install a newer version (I went with gcc5)
$ sudo port install gcc5
get the name of your new version, and set it as default
$ port select --list gcc
Available versions for gcc:
llvm-gcc42
mp-gcc5
none (active)
$ sudo port select --set gcc mp-gcc5
open a new terminal and validate you're updated:
$ c++ --version
c++ (MacPorts gcc5 5.2.0_0) 5.2.0
Most of you getting that error "cc1plus: error: unrecognized command line option -std=gnu++0x" while installing nodejs extension which requires C++ compilation with node-gyp.
So how to solve this error so here is the solution. Basically you get these errors because of Nodejs different version as many node libraries requires C or C++ compilation while installing. So Nodejs older version uses python 2.7 with gcc compiler less than version 4.2 but Nodejs newer version uses gcc44 compiler that's why you get above error while installing any nodejs library.
So you need to degrade your nodejs and node-gyp version and specify the python version if you have multiple python versions installed on your system and then you will not get above error anymore.
Click here to see full tutorial