Luc*_*nik 8 c c++ c++11 sublimetext2 sublimetext3
我升级我的Sublime Text 3 for C/C++,但我必须用C11和C++ 11这样的现代版本编写代码.
当我尝试像这样的C11代码:
#include <stdio.h>
int main( int argc, char ** argv )
{
puts("C99 Version:");
for( int i = 0; argv[i]; i++ ) {
printf("%d: %s\n", i, argv[i]);
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Sublime给出错误:
C:\Users\pc\Desktop\CPPproject\c99.c:7:2: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
for( int i = 0; argv[i]; i++ ) {
^
C:\Users\pc\Desktop\CPPproject\c99.c:7:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
Run Code Online (Sandbox Code Playgroud)
你能解释一下如何使用-std = c99,-std = gnu99,-std = c11或-std = gnu11选项吗?
================================================== ================================
与C++ 11一样.这是代码:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main( int argc, char ** argv ) {
stringstream version;
version << "GCC version: "
<< __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__
<< "\nVersion string: " << __VERSION__;
cout << version.str() << endl;
vector<string> v = { "one", "two", "three" }; // C++11 feature - initializer list
for( string s : v ) { // C++11 feature - range-based for loop
cout << s << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和错误列表:
C:\Users\pc\Desktop\CPPproject\main.cpp: In function 'int main(int, char**)':
C:\Users\pc\Desktop\CPPproject\main.cpp:17:45: error: in C++98 'v' must be initialized by constructor, not by '{...}'
vector<string> v = { "one", "two", "three" }; // C++11 feature - initializer list
^
C:\Users\pc\Desktop\CPPproject\main.cpp:17:45: error: could not convert '{"one", "two", "three"}' from '<brace-enclosed initializer list>' to 'std::vector<std::basic_string<char> >'
C:\Users\pc\Desktop\CPPproject\main.cpp:19:18: error: range-based 'for' loops are not allowed in C++98 mode
for( string s : v ) { // C++11 feature - range-based for loop
^
Run Code Online (Sandbox Code Playgroud)
请帮我解决这些问题!
还有一个问题:当我运行代码时 - .exe文件出现在包含源代码的同一文件夹中,我必须打开它.当我点击"ctrl + b"时,是否有可能在Sublime Text中看到输出?
谢谢 !!!
如评论中所述,这是您需要更改的构建系统.我ST3修改默认包有点麻烦.有插件可以做到这一点,但我还没有使用它们.这是一个插件免费方式.
首先,你需要一个新的.sublime-build.我认为这个应该有效.
{
"cmd": ["g++", "-std=c++11", "-o", "${file_path}/${file_base_name}", "${file}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
},
{
"name": "Build without C++11",
"cmd": ["g++ '${file}' -o '${file_path}/${file_base_name}'"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要导航到存储ST3的默认包的位置.看来你正在使用Windows..sublime-packages 的位置可能类似于C:\ Program Files\Sublime Text 3\Packages.
进入此文件夹后,您应该看到一堆.sublime-package文件,其中包含所有内置支持语言的名称.选择C++.sublime-package并将其复制到其他目录.然后,将其重命名C++.zip并提取.删除该C++.sublime-build文件并将其替换为名称与包含上述代码相同的文件.重新压缩文件并将其重命名为C++.sublime-package并将其放回到您从中获取的文件夹中.
现在: