簡體   English   中英

C ++編譯但在執行時會出錯

[英]C++ compiles but gives error when executed

我是Linux Ubuntu 11.10的新手,並且有基本的C ++曝光。

我安裝了g ++

sudo apt-get install build-essential

並在我的主目錄中創建了一個目錄cpp。 然后我在我的cpp目錄中編寫了一個程序hello.cpp

#include <iostream>
using namespace std;

int main() {
    cout << "Hello !" ; return 0;
}

並使用編譯

g++ -W hello.cpp -o hello

程序編譯時沒有任何錯誤/警告。 當我嘗試執行該文件

./hello.cpp

我收到錯誤消息:

line 3: using: command not found
line 6: syntax error near unexpected token `('
line 6: `int main() {'

我試過看了很多帖子但是無法解決這個問題。 我在Windows上有MS VisualStudio,但我寧願在Ubuntu上學習C ++。 提前致謝。

我認為問題在於您嘗試執行.cpp源文件而不是生成的可執行文件。 嘗試運行./hello而不是./hello.cpp ,因為hello是實際的可執行文件。 您當前獲得的錯誤是由shell解釋器阻塞C ++語法引起的,因為它試圖將其作為shell腳本運行。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM