我升級了C/C++的Sublime Text 3,但是我必須在C11和C++ 11等現代版本中編寫代碼。崇高文本中的C11和C++ 11問題3
當我嘗試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;
}
崇高給出了一個錯誤:
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內的輸出?
謝謝!!!
崇高的文本本身不具有編譯器或語法檢查器,它必須是你已經安裝了一個模塊中。檢查該模塊的配置文件。 –
@JoachimPileborg誤導人。這是一個可以在不安裝任何東西的情況下輕鬆解決的問題。 Sublime Text確實具有構建系統(即在文件或項目上運行的命令)。 Luchnik只需要配置它們。在ST3構建系統上的 – Rapptz
[這裏是文檔](http://docs.sublimetext.info/en/latest/reference/build_systems.html)。爲每個包含適當的「-std」標誌的C和C++創建一個新的。 – Praetorian