Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling C++14 in clang in Visual Studio

Tags:

c++

c++14

I've installed clang 3.7 and I'm using it with visual studio. When I try to compile:

auto f()
{
return 2;
}

I'm getting error saying that this is future from C++14. I've tried to pass:
-std=c++14 as compiler arguments but then I'm getting error:

Error 1 error : unknown argument: '-std=c++14' C:\Users...\visual studio 2013\Projects\ConsoleApplication8\ConsoleApplication8\clang-cl.exe ConsoleApplication8.

Any ideas how to enable c++14 features in clang under Visual Studio 2013?

Edit:
I have to add that I am able to compile and build c++11 code with this compiler under Visual Studio without any problems.

like image 738
There is nothing we can do Avatar asked Apr 29 '15 18:04

There is nothing we can do


People also ask

Does Clang support C++14?

By default, Clang builds C++ code according to the C++14 standard. You can use Clang in C++14 mode with the -std=c++14 option (use -std=c++1y in Clang 3.4 and earlier).

How do I use Clang in Visual Studio?

To configure a Visual Studio project to use Clang, right-click on the project node in Solution Explorer and choose Properties. Typically, you should first choose All configurations at the top of the dialog. Then, under General > Platform Toolset, choose LLVM (clang-cl) and then OK.

Can I use Clang to compile C++?

To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.

How do I change to C 17 in Visual Studio?

Select the Configuration Properties > C/C++ > Language property page. In C++ Language Standard (or for C, C Language Standard), choose the language standard to support from the dropdown control, then choose OK or Apply to save your changes.


2 Answers

I work on clang-cl. As antiduh says, clang-cl tries to mimic Visual Studio's cl. cl up to and including c++14 didn't have a switch for enabling language modes, it just always enabled all the latest stuff. Hence, clang-cl does too. MSVC gained some C++14 support in MSVC 2015, so if you tell clang-cl that you want it to emulate MSVC 2015 or later, it will automatically enable C++14. clang-cl by default emulates the version of MSVC found on your system. You can explicitly pass -fmsc-version=1900 to force emulation of 2015, which will then implicitly enable C++14.

As of MSVC 2017, cl.exe supports a /std: flag, so clang-cl supports that too. It can be used to enable C++14 (the lowest level), C++17, C++20, or the newest-known version.

The -Xclang flags are internal flags and are not considered a stable interface. So do not use those.

like image 79
thakis Avatar answered Sep 20 '22 05:09

thakis


clang-cl doesn't use the same option syntax as traditional clang - it's supposed to mimic Visual Studio's cl command line, not clang's command line.

For instance, from clang-cl's documentation:

CL.EXE COMPATIBILITY OPTIONS:
  /?                     Display available options
  /arch:<value>          Set architecture for code generation
  /C                     Don't discard comments when preprocessing
  /c                     Compile only
  /D <macro[=value]>     Define macro
  ...

Notice that those options are similar to Microsoft's cl option syntax, not clang's option syntax.

However, they have a little pass-through option to support cases like yours:

OPTIONS:
  ...
  -Xclang <arg>         Pass <arg> to the clang compiler
  -mllvm <value>        Additional arguments to forward to LLVM's option processing

And so it would seem that invoking clang-cl -Xclang -std=c++14 would be your best bet.

like image 28
antiduh Avatar answered Sep 21 '22 05:09

antiduh