I can't change C++ version in VS Code - c++

I want to change C++ standard to C++20. I wrote "C_Cpp.default.cppStandard": "c++20" in setting.json and "cppStandard": "c++20",
in c_cpp_properties.json, but when I run this code
#include <iostream>
int main(){
std::cout << __cplusplus;
}
my output is: 201402.
What I need to do to change my C++ version in VS Code?

See this link on configuring the tasks.json file. For your case, you'll need to add the -std=c++2a in the args variable.

Related

std::format errors "no matching function" and "call to consteval function"

Platform: Win 10 64 bit
IDE: CLION 2022.2.4
Toolchain: VS 2022 Community Toolset v17.0 (CMAKE 3.23.2)
Build Tool ninja.exe
C++ compiler: cl.exe
#include <iostream>
#include <string>
#include <format>
int main() {
std::wstring test1 = L"Hällo, ";
std::wstring test2;
std::cout << std::format("Hello {}\n", "world!");
std::cout << std::format("Hello {}\n", "world!");
}
Errors shown in editor:
I suspect this is a CLang error, but I'm not quite sure. I can compile the code just fine and I get an output to the console. But why do I get an error here?
I tried to find anything on Google on it, but I didn't find anything on this specific error. I know from a friend of mine that this is not an isolated issue, or at least he has the same issue.
From what I read the "consteval" was somewhat newly introduced and might still be incompletely implemented in some library functions?
The compiler that your editor uses for highlighting and intellisense doesn't support those features yet.
std::format is a c++20 feature. According to this page it is not yet supported in Clang. This has been discussed here.

Visual Studio Code - Include Path Problems Header Files C++ (MinGW)

I am pretty new to programm in C++ so please don't judge my problems with setting up my "Visual Studio Code" environment. I am trying to use the blaze math packages to solve quadratic programming problems. With the MinGW GCC I can compile the testfiles of blaze successfully via cmd and thus I want to use the GCC for VS Code.
OS: Windows 10.0.19041
GCC: gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0 || (cmd: gcc --version)
VS Code Extensions: C/C++ 0.27.1
Firstly I went through the VS Code tutorial for MinGW:
https://code.visualstudio.com/docs/cpp/config-mingw
This worked fine so I can easily compile my helloworld.cpp. The resulting tasks.json file looks like this
tasks.json.
As my package manager (for blaze or other packages) i use the vspkg-git:
https://learn.microsoft.com/en-us/cpp/build/vcpkg?view=msvc-160
Thus I am programming on Windows I can't use the "integrate" command to add the path to the includepath. So I have to do this manually.
My packages are in the folder with the absolute path
C:\Users\Johannes\Desktop\Masterthesis\vcpkg\vcpkg\packages
So I added the path on the "c_cpp_propertier.json"-file
{
"configurations": [
{
"name": "GCC",
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"C:/Users/Johannes/Desktop/Masterthesis/vcpkg/vcpkg/packages/**"
],
"compilerPath": "C:/Program Files/mingw-w64/x86_64-7.3.0-posix-seh-rt_v5-rev0/mingw64/bin/g++.exe",
"browse": {
"path": []
}
}
],
"version": 4
}
In the folder are several packages therefor I added the "/**" at the end of the path to enable the recursive search for header files.
My 'helloworld.cpp' file looks like this
#include <iostream>
#include <vector>
#include <string>
//#include <blaze/Math.h>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the
C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
My problem is that when I try to include a header-file from this path for example Math.h Visual Studio throws an Error
blaze/Math.h: No such file or directory
But when I right-click on the include and click "Go to Definition" VS Code opens the file. Optionbar and the opened file.
The Log-Diagnostics gives that information.
I guess someone that has experience with MinGW and additional packages in VS Code will solve that problem very simple, but I have read nearly every thread about those problems and didn't find anything matching mine.
Okay, i got the answere. The include path of the "c_cpp_properties.json" file is only for IntelliSense. This means that Visual Studio Code will find this packages and IntelliSense will suggest you the available headers from the pathes. This does not mean that the compiler can find these pathes. Therefor you have to add the pathes to the "tasks.json" file. As you can see above in the photo of the "tasks.json" file, there is a field called "args" which means "arguments". Those are the compiler arguments. You have to add the Path there too in Format "-I","C:/PathYouWishToAdd". This works fine!!

How to setup sublime 3 build for C++ with multiple files

First of all, I'm a total noob on SO, so please go easy on me :)
That being said, my question might be really easy to answer:
How would I use sublime text 3 for c++ with multiple files (header/implementation files)
I've been trying some build systems provided by other people,
this is the one I'm currently using and seems to work just fine for compiling single .cpp files but is telling me some errors which I don't understand
here is my build config using g++
{
"cmd": ["g++.exe", "-std=c++14", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
"shell": true,
"selector": "source.c++"}
here is my main.cpp:
#include <iostream>
#include <string>
#include "num.h"
using namespace std;
int main(){
Num n(35);
cout << n.getNum() << endl;
return 0;}
here is my num.h file:
#ifndef NUM_H
#define NUM_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
and here is my num.cpp:
#include "num.h"
#include <iostream>
#include <stdlib.h>
Num::Num(int x){
num = x;
}
int Num::getNum()
{
return num;
}
all files are in the same directory and whenever I'm using g++ on the command like like this: (g++ (tdm64-1) 5.1.0)
g++ main.cpp num.cpp
there is no problem and everything works just fine
but whenever I'm trying to build it with sublime text it throws me this error
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x1a): undefined reference to `Num::Num(int)'
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x26): undefined reference to `Num::getNum()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]
I would really appreciate it if someone could tell me what I'm doing wrong here :)
Once you start getting into the realm of needing to compile multiple files, it's really best to start using make or CMake instead of trying to build the compile commands yourself on the command line or in a build system. This way you wouldn't have to edit your build system every time you added a new file to your project.
There is a Make build system that comes with Sublime, but you will need to generate the Makefile externally, either by hand or using tools like autoconf and automake. The CMakeBuilder package looks like it would be useful for working with CMake (I haven't used it myself), although you can of course use external tools as well.

How do I transfer my C++ solution made in Visual Studio for Windows to Visual Studio for Mac?

I made a multiple solutions with C++ in Visual Studio at a tech camp on a Windows computer. Unfortunately, I do not have a windows computer at home, so I tried using Visual Studio for Mac 2019. This alert shows up whenever I try to run any of my solutions that says "This project type is not supported by Visual Studio Community 2019 for Mac. When I build the solution, it says it the build is successful, but the run with or without debugging options are grayed out. How can I fix this?
I was able to get it to work using a Windows 10 Parallels, and it said it needed to update the solution because I had made it in Visual Studio 2017. I updated it and it worked, but my program runs very slow. After doing this, it still does not work on Visual Studio for Mac.
Here is my main .cpp file named MyFirstProgram.cpp:
#include "pch.h"
#include "Main.h"
#include <string>
using namespace std;
int main()
{
cout << "My header file works!" << endl;
}
The pch.cpp file:
#include "pch.h"
The pch.h file:
#ifndef PCH_H
#define PCH_H
#endif //PCH_H
And the Main.h file:
#pragma once
#include <iostream>
#include <string>
To build your C++ project using Visual Studio code on Mac, ensure you have C/C++ build tools installed.
Go to your Marketplace on Visual studio, Click on view, select Extensions. Type C++ on the search, and select the extension by Microsoft and install
Create a new project
Copy and paste your codes in a new main.cpp and MyFirstProgram.cpp file.
Try rebuilding
Alternatively, you can use Xcode or Jetbrain CLion on Mac
Another trick to build your cpp program on Mac or Linux without using any heavy IDEs is building a simple task file
**Mac has a g++ compiler **
Create a folder yourDirectoryName
create a file main.cpp (you can copy and paste your codes in here)
create a file json file
** go to the top bar on visual studio, select Terminal > configure Default Build Task > select other**
it would create a hidden json file for you. modify the json file to run your main.cpp file
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"main.cpp",
"-o",
"${yourDirectoryName}"
],
},
{
"label": "run",
"type": "shell",
"command": "./${yourDirectoryName}",
"dependsOn": [
"build"
],
}
]
}
The task is called run.
to run it, go to terminal, select run task.
click on the task run > continue without scanning the task output

C++ Visual Studio warn about indirect includes

The following code compiles fine with Visual Studio 2013 (probably because <iostream> includes <limits>), but the "missing" #include <limits> prevents me as C++ newbie sometimes to understand whats going on. For example I realized that std::numeric_limits<int>::max() is in <limits> only after removing #include <iostream>.
So how can I force the compiler to require each include explicit?
#include <iostream>
int main() {
std::cout << std::numeric_limits<int>::max();
}
While this isn't a compiler "Warning" per say, you can have the MSVC compiler output a list of all included files at compile time with the /showIncludes flag MSDN compiler reference