【发布时间】:2014-06-19 11:59:45
【问题描述】:
我为 C/C++ 升级了我的 Sublime Text 3,但我必须用 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;
}
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
您能解释一下如何使用 -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;
}
以及错误列表:
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
^
请帮我解决这些问题!
还有一个问题:当我运行代码时 - .exe 文件与源代码出现在同一个文件夹中,我必须打开它。当我单击“ctrl+b”时,是否有可能在 Sublime Text 中看到输出???
谢谢!!!
【问题讨论】:
-
Sublime Text 本身没有编译器或语法检查器,它必须在您已安装的模块中。检查该模块的配置文件。
-
@JoachimPileborg 误导。这是一个无需安装任何东西即可轻松解决的问题。 Sublime Text 确实有构建系统(即在文件或项目上运行的命令)。 Luchnik 只需要配置它们。
-
Here's the documentation 在 ST3 构建系统上。为每个包含适当
-std标志的 C 和 C++ 创建一个新的。
标签: c++ c c++11 sublimetext2 sublimetext3