我升级了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中看到输出?
谢谢!!!