gpt4 book ai didi

c++ - Sublime Text 2 运行 C++11 代码

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:11 25 4
gpt4 key购买 nike

我在 Sublime Text 2 中有以下代码:

#include <iostream>
#include <string>

using std::cout;
using std::string;

int main()
{
string s("some string");
if (s.begin() != s.end()) // make sure s is not empty
{
auto it = s.begin(); // it denotes the first character in s
*it = toupper(*it); // make that character uppercase
}

cout << s;

return 0;
}

当我将 C++.sublime-build 文件更改为

时,我可以通过按 ctrl-B 在 Sublime 中 构建 C++11 代码
"cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],

但我想同时构建和运行代码,我通常使用ctrl-shift-B 来执行此操作。在编辑我的 sublime-build 文件之前,我从编译器收到一条错误消息:

/home/michael/src/c++-primer/pointers.cpp: In function ‘int main()’:
/home/michael/src/c++-primer/pointers.cpp:12:14: error: ‘it’ does not name a type
auto it = s.begin(); // it denotes the first character in s
^
/home/michael/src/c++-primer/pointers.cpp:13:10: error: ‘it’ was not declared in this scope
*it = toupper(*it); // make that character uppercase

这向我暗示,当我按下 ctrl-shift-B 时,它并没有使用我编辑的 sublime-build 文件。我该如何更改?

最佳答案

哦,我想通了。您需要在构建文件的“变体”部分添加标志:

{
"cmd": ["g++", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",

"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]

关于c++ - Sublime Text 2 运行 C++11 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25480152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com