Clang compiles code using std::stoi in c++98 mode - c++

I need to compile my cpp in C++98, not C++11 for my school project.
So I used -std=c++98 to compile:
CPPFLAGS = -Wall -Werror -Wextra -std=c++98
but I made a mistake and use the C++11 std::stoi function.
i = std::stoi(index);
I tried without the -std=c+=98 flag but it didn't change anything.
I am working on MAC 10.12.6
My code compiles without any warning or any error.
If I am not mistaken, clang should shout at me for using a C++11 function.
Why?
edit clang version:
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix

Related

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.

clang seems to use the gcc libraries

This is the first time I use clang. What I notices is that any error from clang referencing the std library looks like this:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:245:7:
^^^ ^^^ ^^^
So it looks like clang links — or at least includes — the gcc libraries.
The command I used: clang++ -c -Wall -Wextra -Werror -g test.cpp -o test.o. (The program had a intentional error just to prove this).
How is this possible? What can I do to make clang use its own libraries (but not break gcc)?
Additional information:
I am on a Ubuntu 14.04 machine.
clang++ --version
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix
g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
I had previously installed several versions (at the same time, used them with update-alternatives) of gcc with apt-get. Right now I have only 4.8 (I have uninstalled the others). Could I have messed up something then? I have never installed clang (I guess it is default with Ubuntu).
Just to clarify: the correct programs compile and run in clang++.
Further tests: I know that gcc didn’t implement yet types like is_trivially_constructible and move operations on iostream in their standard c++11 library (https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html) and that clang has full c++11 conforming library so I tested those compiling with clang and I got the corresponding gcc errors, which only confirms that clang is using gcc libraries.
A very basic program
#include <iostream>
using namespace std;
int main() {
cout << "Yada Yada" << endl;
return 0;
}
gives this error when compiling with -std=c++1y in clang++:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/iostream:39:
...
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/cstdio:120:11: error: no member named 'gets' in the global namespace
using ::gets;
~~^
So right now I can’t compile anything with c++1y in clang.
You need to install libc++ and make clang use it with -stdlib=libc++
I had similar issue: GCC (g++) already was installed on my LinuxMint (Ubuntu base) so when compile with clang, was getting an " error: no member named 'gets' in the global namespace using ::gets ".
resolved by installing libc++-dev (sudo apt-get install libc++-dev) and compiling with -stdlib++ (clang++ -g -std=c++1y -stdlib=libc++ helloworld.cpp -o helloworld)
Your real problem is that you're using C++14 (c++1y was the informal name used to refer to it when it wasn't yet fully formed), with a C++ library belonging to GCC 4.8. GCC 4.8 has complete C++11 support, but hardly even started on C++14 features.
This is caused by C++14 removing std::gets, and the GNU C library anticipating on that by not defining gets in the global namespace while the C++ library has not yet caught up and is trying to make it available in the std namespace.
The proper way to solve this does not require using libc++, just to use a C++ library with C++14 support. GLIBCXX 4.9 (aka libstdc++) already suffices.

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

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++

clang doesn't know std::atomic_bool, but XCode does

I'm trying to compile C++11 code that declares a variable of type std::atomic_bool. This is on Mac OS 10.8.2 with clang:
clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
clang complains about std::atomic_bool:
clang++ -c -stdlib=libc++ -msse4 -std=c++11 -Wno-unused-parameter -I. -o query.o query.cpp
In file included from query.cpp:1:
[...]
./threadutils.h:33:10: error: no type named 'atomic_bool' in namespace 'std'; did you mean 'atomic_long'?
std::atomic_bool work;
However, the same file compiles fine in an XCode project using the same compiler. So I assume I'm missing something in my manual compiler invocation.
I tried a few variations such as -std=c++0x and -std=gnu++11, to no avail.
I figured it out. Unfortunately I planted a false flag into my question: it didn't work in XCode either, I had a different version of the source file imported there.
The problem was that C++11 defines "a named type atomic_bool corresponding to the specified atomic<bool>", but clang doesn't support that.
Renaming the type from atomic_bool to atomic<bool> fixed it.
I found the same problem. In my case I resolved it by including atomic:
#include <atomic>
static std::atomic_bool varname;
After this, I could call g++ with std=c++11 on a Linux (Ubuntu) as well as compile on XCode.