在 Visual Studio Code 中编译 C++11

2022-01-23 00:00:00 visual-studio-code g++ c++ c++11

我正在使用 Visual Studio Code 编译 C++ 程序,它适用于大多数 C++ 程序,因为它使用 g++ 命令进行编译.但是,我在使用它编译 c++11 程序时遇到了困难.

I'm using Visual Studio Code to compile C++ programs and it works for most C++ programs as it compiles it using g++ command. However, I am facing difficulty in compiling c++11 programs using it.

当我尝试编译 C++11 程序时,编译器命令 g++ 会尝试使用默认的 C++98 标准对其进行编译,这会导致错误.

When I try compiling C++11 programs, the compiler command g++ tries to compile it using default C++98 Standard and this results in errors.

我知道使用 g++ -std=c++11,我们可以使用 g++ 编译 C++11 程序,当我在我的 cmd 为:

I am aware that using g++ -std=c++11, we can compile C++11 programs using g++ and it works fine when I use it in my cmd as:

g++ -std=c++11 some_program.cpp

g++ -std=c++11 some_program.cpp

我希望我可以调整 Visual Studio Code 中的一些设置并将编译器命令从 g++ 更改为 g++ -std=c++11 以便我可以编译程序只需点击 run code 按钮.但是,我找不到一个.如果有其他方法可以编译我的程序,请帮助我.

I wish I could tweak some setting in Visual Studio Code and change the compiler command from g++ to g++ -std=c++11 so that I could compile programs by just hitting the run code button. However, I'm not able to find one. Please help me out if there are alternate ways to get my program compiled.

目前,我收到以下错误:

Currently, I'm getting errors as:

some_program.cpp:在函数'int main()'中:

some_program.cpp: In function 'int main()':

some_program.cpp:12:33: 错误:在 C++98 中,'A' 必须由构造函数初始化,而不是由 '{...}'向量 A = { 11,2,3,14 };

some_program.cpp:12:33: error: in C++98 'A' must be initialized by constructor, not by '{...}' vector A = { 11,2,3,14 };

这些片段是正确的,并且已经通过使用 C++11 的在线编译器进行了测试.在这里,它正在尝试使用 C++98 进行编译,如错误所示.

The snippets are correct and have been tested with online compilers using C++11. Here, it is trying to compile using C++98 as seen in the error.

推荐答案

转到设置 > 用户设置在这里,搜索运行代码配置:

Go to Settings > User Settings In here, search for Run Code Configuration:

在此菜单下,找到:code-runner.executorMap"

通过将其添加到用户设置来编辑此设置,如下所示以支持 C++11:

Edit this Setting by adding it to User Setting as below for C++11 support:

"code-runner.executorMap":{
    "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},

通过将其添加到用户设置来编辑此设置,如下所示以支持 C++17:

Edit this Setting by adding it to User Setting as below for C++17 support:

"code-runner.executorMap":{
    "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},

希望这会有所帮助!

相关文章