C++编译但在执行时会出错

sun*_*nny 4 c++ linux compilation

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

我安装了g ++

sudo apt-get install build-essential
Run Code Online (Sandbox Code Playgroud)

并在我的主目录中创建了一个目录cpp.然后我在我的cpp目录中编写了一个程序hello.cpp

#include <iostream>
using namespace std;

int main() {
    cout << "Hello !" ; return 0;
}
Run Code Online (Sandbox Code Playgroud)

并使用编译

g++ -W hello.cpp -o hello
Run Code Online (Sandbox Code Playgroud)

程序编译时没有任何错误/警告.当我尝试执行该文件

./hello.cpp
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

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

我试过看了很多帖子但是无法解决这个问题.我在Windows上有MS VisualStudio,但我宁愿在Ubuntu上学习C++.提前致谢.

tem*_*def 19

我认为问题在于您尝试执行.cpp源文件而不是生成的可执行文件.尝试运行./hello而不是./hello.cpp,因为hello是实际的可执行文件.您当前获得的错误是由shell解释器阻塞C++语法引起的,因为它试图将其作为shell脚本运行.

希望这可以帮助!