繁体   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