How to solve for each loop compiler error in c++? - c++

Hi I am using this as my compiler
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
and then when I try to compile for each loop with
g++ 2dForEach.cpp it throws this error
2dForEach.cpp:9:14: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
It only happened when I try to compile on terminal using g++.
It works completely fine when I try to compile directly on Xcode.
Thanks.

Well, it actually told you.
Compile with
G++ -std=c++11 2dForEach.cpp
EDIT: Sorry, mis-copied arg :)

Related

c++11 warning on MacOS [duplicate]

This question already has answers here:
Compile C++11 code on mac?
(4 answers)
Closed last year.
I have clang 13.0.0 on my Mac. I'm trying to compile some .cpp files using the command
g++ file.cpp -o file
But I get this warning message:
file.cpp:23:26: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
for (const double &e : timestamp) outFile << std::setprecision(10) << e << "\n";
On the clang documentation is said that c++11 is supported by clang 13. On my terminal running man clang I find: The default C++ language standard is gnu++14.
Typing g++ -v I get:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: arm64-apple-darwin21.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Why am I getting this warning?
AppleClang defaults to c++98, whereas llvm-clang defaults to the later gnu++14; to access the c++11 implementation, you must add the flag, --std=c++11 or --std=c++14. cf. https://opensource.apple.com/source/clang/clang-703.0.29/src/tools/clang/www/cxx_status.html
If you are expecting the gnu g++, it must be installed separately, because on macOS g++ aliases to clang, which is the Apple fork of clang known as AppleClang.
If you were expecting llvm-clang, that also must be separately installed. AppleClang 13 ≠ llvm-clang 13.

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)

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

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

How to emit optimisation reports using Apple clang 3.5?

According to the documentation, clang supports options to emit optimisation reports:
When the pass makes a transformation (-Rpass).
When the pass fails to make a transformation (-Rpass-missed).
When the pass determines whether or not to make a transformation (-Rpass-analysis).
They provide the following example command line:
$ clang -O2 -Rpass=inline code.cc -o code
When I try this, I get an error:
$ clang src/test/tests.cpp -Rpass=inline
clang: error: unknown argument: '-Rpass=inline'
scons: *** [build/test/tests.o] Error 1
scons: building terminated because of errors.
My clang version is:
$ clang --version
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
Is this option not available on Mac OS? Is the documentation erroneous? Or am I doing it wrong? If so, how to do it right?
Works for Debian clang-3.5.0-6, so this is probably Apple clang restriction.
% clang++ -O2 -Rpass=inline foo.cpp
foo.cpp:11:2: remark: _ZN1CC2Ev inlined into main [-Rpass=inline]
P* p = new C();
^
% clang --version
Debian clang version 3.5.0-6 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: i386-pc-linux-gnu
Thread model: posix

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