The command line clang/clang++ doesn't work on MacBook Air 2012 Mid with OS X 10.8.5 - c++

Originally, I used clang++ with -std=c++11 compile my C++11 code, everything was OK. Recently, I updated the Xcode in AppStore, I compiled the SAME program in command line again, it doesn't work anymore, it shows errors like below:
/usr/include/c++/4.2.1/bits/stl_construct.h:81:38: error: no matching
constructor for initialization of
'std::basic_string'
::new(static_cast(__p)) T1(_value);
It seems the clang++ is using gcc's header files, apparently, gcc's version is very old. However, if I compile the same program by using Xcode, everything is fine.
It seems, at command line , the clang++ can't find the correct header files anymore.
More information, running command clang++ --version, following output:
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0 Thread model: posix
which clang++
/usr/bin/clang++
RESOLVED with option -stdlib=libc++

Related

Clang MacOs over command line does not work

I am fairly new to both MacOs and C++ and have a problem which is similar to the one described here but also no solution I find in the Internet works.
'fatal error: 'wchar.h' file not found' error with the new macos 11.3 update
If I try to compile the most simple c++ program on my machine via command line it does not work.
#include <iostream>
int main()
{
std::cout << "Hello, World!" << std::endl;
return 0;
}
Since it used to work I probably broke something but don't know what
The simple test command I use is the following
clang -v --target=arm64 helloworld.cpp
Which results in a iostream not found error
if I now include the xcode include directory via
clang -v --target=arm64 -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ helloworld.cpp
The iostream error disappears and I get a wchar.h not found error
I removed xcode completely and reinstalled it but this seems to not help also does it not make a difference if I use clang or clang++.
With Clion and cmake it works but I do not know why
xcode-select version 2392
/usr/bin/clang
Apple clang version 13.0.0 (clang-1300.0.29.30)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
locate iostream.h
`/Library/Frameworks/Mono.framework/Versions/6.12.0/include/glib-2.0/gio/gfileiostream.h
/Library/Frameworks/Mono.framework/Versions/6.12.0/include/glib-2.0/gio/giostream.h
/opt/homebrew/Cellar/boost/1.76.0/include/boost/asio/basic_socket_iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/iostreams/detail/iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/math/cstdfloat/cstdfloat_iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/nowide/iostream.hpp
/opt/homebrew/Cellar/boost/1.76.0/include/boost/typeof/std/iostream.hpp
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/gfileiostream.h
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/giostream.h
/opt/homebrew/Cellar/glib/2.70.2/include/glib-2.0/gio/gsimpleiostream.h
I had similar troubles building clang + llvm, then trying to use the newly built clang via
./build/bin/clang -std=c++20 <my_file>
gave me a
fatal error: 'iostream' file not found
What fixed it was if I compiled with the following instead:
./build/bin/clang -std=c++20 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk <my_file>
The fix largely comes from https://github.com/MaskRay/ccls/issues/191#issuecomment-556983460 and the discussion there

Compile with c++17 mac

I can't compile with -std=c++17, I got :
error: invalid value 'c++17' in '-std=c++17'
However I update Xcode and clang.
My Clang version is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
And I load the newest header like optional, I have to do
#include <experimental/optional>
instead of
#include <optional>
Xcode brings its own complete toolchain, including headers and the actual compiler.
Apple LLVM version 9.0.0 (clang-900.0.39.2) (which ships with Xcode 9.2) does not support the usage of the flag -std=c++17 since its too old. The optional header is only included under the folder experimental/. Which is why you need to #include <experimental/optional>
In order to compile your program with c++17 support using the compiler which comes with Xcode 9.2 you need to use the -std=c++1z flag.
Xcode 9.3 will be shipped with Apple LLVM version 9.1.0 (clang-902.0.30) which has support for the -std=c++17 flag. However the optional header is as of today still under the experimental/ subdirectory. This might change during the betas.
Here is what I get with this tests:
#include <experimental/optional>
int main(int, char* []) {
return 0;
}
g++ -std=c++17 -o test test.cpp
error: invalid value 'c++17' in '-std=c++17'
g++ -std=c++1z -o test test.cpp
Did you try the c++1z argument?
Also of note my test compiles without the -std=c++1z argument provided.
I think I'm on a newer version of OSX than you:
Target: x86_64-apple-darwin17.4.0
You should use -std=c++1z as flag.
libc++ with c++17 support since macos 15
-std=c++1z also works on Apple LLVM version 8.1.0 (clang-802.0.42)

How to use the available C++17 flags from Xcode in Terminal

My Xcode 9.2 has the option to compile C++ using: -std=c++17 or -std=gnu++17.
My C++ code that has some C++17 features compiles successfully.
However, if I try to compile the same code in my Terminal using the same flags, I get the following errors:
clang++ -std=c++17 test.cpp -o test
error: invalid value 'c++17' in '-std=c++17'
and
clang++ -std=gnu++17 test.cpp -o test
error: invalid value 'gnu++17' in '-std=gnu++17'
My OSX version is 10.13.3 and my Clang++ version is:
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
I thought that Terminal would use the same compiler from Xcode. That may be the case, but I can't use the same options. At least I couldn't.
I tried using -std=C++1z, but it didn't compile my code because it doesn't understand some new features from C++17.
So my question is: Is there a way to use -std=c++17 or -std=gnu++17 that are currently available in my Xcode 9.2, in my Terminal app?
This questions is not the same as the others available at stackoverflow because all the answers I found the -std=c++17options was not available in Xcode. Now we have this options, but I can't figure out how to use it in Terminal.
The compiler Apple currently ships with Xcode 9.2 (clang-900.0.39.2) does not support the -std=c++17 flag.
Xcode uses the -std=c++1z flag when you enable c++17 support. If you want to use -std=c++17 you need to manually install clang. You could do that using brew install llvm (assuming you have homebrew installed).
You can compile your program with
/usr/local/Cellar/llvm/5.0.0/bin/clang++ -std=c++17 test.cpp -o test
You change the symlinks in usr/bin/clang++ to point at the new destination if you don't want to use the full path to the compiler.
The compiler which is shipped with Xcode 9.3 will be
Apple LLVM version 9.1.0 (clang-902.0.30)
This can handle the -std=c++17 flag.

os x LLVM supports c++11/14, why the include directory is still gcc 4.2.1?

On my new macbook pro, after install xcode, I found:
Both gcc and clang are linking to LLVM 7.3. I guess gcc is just an alias of clang here, right?
As long as gcc supports -std=c++11 and -std=c++14 parameter, why the /usr/include/c++ only has a directory named "4.1.2"? I know gcc 4.1.2 is quite an old version,

gfortran doesn't work after upgrading to El Capitan

I tried upgrading to El-Capitan and Xcode 7.0 and now gfortran doesn't work. Everytime I run gfortran, I get errors which google search doesn't seem to resolve so I'm asking stack overflow:
I'm trying to compile a minimal example program:
program hello
print *, "Hello World"
end program hello
When I run gfortran test.f
gfortran: warning: couldn’t understand kern.osversion ‘15.0.0
ld: library not found for -lgcc_s.10.4
collect2: error: ld returned 1 exit status
I'm not worried about the warning but the error of the ld is what's really bothering me. There's libgcc_s.10.4.tbd in the /usr/lib folder but it seems not be used by gfortran.
Can anyone help me resolve this as I would like to use gfortran for compiling some libraries?
MORE info: running gfortran -v outputs
gfortran: warning: couldn’t understand kern.osversion ‘15.0.0
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin12.2.0/4.8.0/lto-wrapper
Target: x86_64-apple-darwin12.2.0
Configured with: ../gcc-4.8-20120930/configure --enable languages=c++,fortran
Thread model: posix
gcc version 4.8.0 20120930 (experimental) (GCC)
The version of gfortran you have (4.8 installed into /usr/local) is not a version of GCC that apple would have distributed with Xcode (afaik they never made it to a version of GCC that new before switching to llvm, which has no Fortran front-end. The version of gcc distributed with Xcode 7 on el capitan identifies itself as:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
Your version came from somewhere else and is old enough to not know the x86_64-apple-darwin15.0.0 target. You'll need to update that compiler to get a working gfortran. Both macports and homebrew provide easy installations of various versions of gcc/gfortran on OS X el capitan, and I'd recommend using one of those to install gcc. Alternatively you can track down whatever you used to install gcc 4.8 before and see if they provide an update for el capitan.