C ++编译但在执行时给出错误 [英] C++ compiles but gives error when executed

查看:155
本文介绍了C ++编译但在执行时给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Linux Ubuntu 11.10 and have basic C++exposure.

我通过

sudo apt-get install build-essential

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

and created a directory cpp in my home directory. I then wrote a program hello.cpp in my cpp directory

#include <iostream>
using namespace std;

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

并使用

g++ -W hello.cpp -o hello

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

The program compiles without any errors/warnings. When I try to execute the file

./hello.cpp

我收到错误消息:

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

我试图查看很多帖子,但无法解决这个问题。我在Windows上有MS VisualStudio,但我宁愿在Ubuntu上学习C ++。先感谢。

I tried looking at a lot of posts but could not resolve this. I have MS VisualStudio on Windows, but I would rather learn C++ on Ubuntu. Thanks in advance.

推荐答案

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

I think that the problem is that you're trying to execute the .cpp source file rather than the generated executable. Try running ./hello instead of ./hello.cpp, since hello is the actual executable. The errors you're currently getting are caused by the shell interpreter choking on C++ syntax, since it's trying to run it as a shell script.

希望这有助于!

这篇关于C ++编译但在执行时给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆