Default C++ version SublimeLinter - c++

I am using SublimeLinter-clang with Sublime 3 to lint C++ code on MacOS Mojave. I am getting warnings and errors for code from later versions of C++. How do I set the default linting version to C++17?
For instance, I have the line:
auto game = SpinOut{};
and the editor marks the following:
1 warning: clang++ - 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
clang++: error - expected '(' for function-style cast or type construction
I tried following the answer to this question but nothing changed. Below is my SublimeLinter.sublime.settings, and I still get the same error and warning.

SublimeLinter-clang is now maintained at https://github.com/SublimeLinter/SublimeLinter-clang rather than https://github.com/nirm03/SublimeLinter-clang.
In the newer version there was a pull request in 2018 that added support that gives users the ability to have different settings for the c linter and c++ linter. So the answer from 2017 in https://stackoverflow.com/a/42818098/17034 is currently outdated.
So it looks like you will need to change clang to clang++. It also looks like they changed extra_flags to args in the settings, so you will need to override the default args and add -std=c++11.
"linters":
{
"clang++": {
"args": "-Wall -fsyntax-only -fno-caret-diagnostics -std=c++11"
}
},

Related

Using clang-tidy to check c++17 code

I installed clang-tidy on Ubuntu using:
sudo apt install clang-tidy
I ran it on a simple C++ 17 file, and got a warning and errors:
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:3: warning: 'auto' type specifier is a C++11 extension [clang-diagnostic-c++11-extensions]
auto i = make_unique<int>();
^
/home/erelsgl/Dropbox/ariel/CPLUSPLUS/intro/01-single-file/ptr.cpp:17:12: error: use of undeclared identifier 'make_unique' [clang-diagnostic-error]
auto i = make_unique<int>();
How can I tell clang-tidy to check this file according to c++17 standards?
NOTE: To build the program, I run:
clang++-5.0 --std=c++17 ptr.cpp
Depending on your compiler / clang-tidy version, the default C++ standard version used to compile source files may vary. clang's default std version is gnu++-98 (or gnu++-14 starting with clang 6.0), and typically clang-tidy has the same defaults as clang.
I'm guessing that -std=c++17 (or -std=c++1z) isn't specified in the C++ compiler flags, used for compiling ptr.cpp, so clang-tidy falls back to the default -std=gnu++98, and therefore gives warnings for C++11 code.
For asking clang-tidy to handle C++17, you should specify the -std flag as suggested by #n.m., as parameter to the -extra-arg option, for example:
clang-tidy -p . ptr.cpp -extra-arg=-std=c++17
Edit:
Since clang++-5.0 is used for compiling ptr.cpp, it may be a good idea to use the matching clang-tidy version, 5.0 (on Ubuntu 16.04, the default clang-tidy version installed through apt is 3.8), that is:
clang-tidy-5.0 -p . ptr.cpp -extra-arg=-std=c++17
If not already installed, you could grab it from:
https://www.ubuntuupdates.org/package/xorg-edgers/xenial/main/base/clang-tidy-5.0

ISO C++ forbids declaration of .. with no type with --std=c++0x set

In my c++ code I'm using the auto type, to enable c++11, I add the flag --std=c++0x to my makefile, but I still get error of ISO C++ forbids declaration of .. with no type, if I enable c++11 with -std=gnu++11 or -std=c++11, I got cc1plus: error: unrecognized command line option "...", my gcc version is 4.3.4, how should I solve this? Thank you.
Without enabling c++11, I got error of ISO C++ forbids declaration of wallclock with no type the following line:
auto wallclock = time(nullptr);
Support for auto-typed variables was not implemented in GCC before version 4.4. The version you are using is too old.
See support matrix: https://gcc.gnu.org/projects/cxx0x.html

C++11 string properties and gcc version

I currently use C++ string properties and specifically its pop_back() fonction. As written in the title, it leads to an error (same error seen ini an other topic) :
‘std::string’ has no member named ‘pop_back’
But what's strange is that I already use C++11 specific properties (as "auto" for iterators, etc.) and I never get any error.
For information, I build my code under Ubuntu 12.04 with gcc 4.6.3. For me, this version is good enough. I also put "-std=c++0x" flag so I really don't know where is the point ?!
Moreover, I've seen, in previous topic, that it's better to use "-std=c++11" flag now. But when I try this, the following error appears :
unrecognized command line option ‘-std=c++11’
On gcc 4.6 (which had only partial support for the new standard) the option is -std=c++0x because at that time the release date (i.e., 2011) was still unknown.

-std=c++11 flag unrecognized in gcc/g++ 4.4.6

I have some code written using the C++11 standards, and our g++ version is 4.4.6, so as far as I can tell, C++11 should be supported (from 4.3 onwards).
However upon trying to compile with the flags -std=c++11 -std=gnu++11, I get repetitions of the errors
cc1plus: error: unrecognized command line option `-std=c++11`
cc1plus: error: unrecognized command line option `-std=gnu++11`
Compiling with -std=c++0x produces errors such as
DeviceInfo.cpp:22: error: expected initializer before ‘:’ token
corresponding to this line of code:
for (cl::Platform& plat : platforms)
Is this a C++11 specific bit of Syntax? (it doesn't look like it to me, but all this code has been given as an example so should work as provided with the compiler.)
Any help?
-std=c++11 setting is supported by much later versions of GCC. The initial support for nascent C++11 was enabled by -std=c++0x setting. This is probably what you should try.
And yes, the for syntax you are trying to use is chiefly C++11 syntax.
It appears as if range-based for loops are supported in 4.6 and newer.
This page shows GCC support for C++11 features.
for (cl::Platform& plat : platforms)
Yes, it is C++11 specific usage. GCC 4.7 or later supports C++11 with -std=c++11 option meanwhile.

How does one suppress specific warnings from source code on g++ 4.5 or later?

The first comment on a feature request for g++ says, "Starting with 4.5 you can disable a class of warnings in the source."
I looked through the 4.5.0 manual, but I can't find the syntax.
What is the syntax in g++ 4.5 and later to suppress individual warning classes in the source?
http://gcc.gnu.org/onlinedocs/gcc/Pragmas.html
http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas